表视图单元格事件用nib和storyboard的两种写法总结
一.代码的写法比较
ViewController.m
nib写法
#pragma mark - 实现表视图委托方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
CitiesViewController *citiesViewController = [[CitiesViewController alloc] initWithStyle:UITableViewStylePlain];
NSString *selectName = [self.listData objectAtIndex:row];
citiesViewController.listData = [self.dictData objectForKey:selectName];
citiesViewController.title = selectName;
[self.navigationController pushViewController:citiesViewController animated:YES];
}
storyboard写法
//选择表视图行时候触发
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"ShowSelectedProvince"])
{
CitiesViewController *citiesViewController = segue.destinationViewController;
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
NSString *selectName = [self.listData objectAtIndex:selectedIndex];
citiesViewController.listData = [self.dictData objectForKey:selectName];
citiesViewController.title = selectName;
}
}
CitiesViewController.m
nib写法
#pragma mark - 实现表视图委托方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
NSDictionary *dict = [self.listData objectAtIndex:row];
detailViewController.url = [dict objectForKey:@"url"];
NSString *name = [dict objectForKey:@"name"];
detailViewController.title = name;
[self.navigationController pushViewController:detailViewController animated:YES];
}
storyboard写法
//选择表视图行时候触发
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"ShowSelectedCity"])
{
DetailViewController *detailViewController = segue.destinationViewController;
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
NSDictionary *dict = [self.listData objectAtIndex:selectedIndex];
detailViewController.url = [dict objectForKey:@"url"];
NSString *name = [dict objectForKey:@"name"];
detailViewController.title = name;
}
}