Objective c UITableViewController分组属性不工作
我有两个UITableViewController。在第一个UITableView中,一旦用户选择一个单元格,就会推送一个新的UITableViewController。我已将两个UITableView都设置为按IB分组。但是,当按下第二个UITableViewController时,它显示为普通UITableView。有办法解决这个问题吗 作为一个健全性检查,我更改了代码,以便第二个UITableViewController不是从第一个UITableViewController推送的,它看起来确实是分组的。这有什么原因吗 推送第二个UITableViewController的UITableViewController中的代码:Objective c UITableViewController分组属性不工作,objective-c,uitableview,Objective C,Uitableview,我有两个UITableViewController。在第一个UITableView中,一旦用户选择一个单元格,就会推送一个新的UITableViewController。我已将两个UITableView都设置为按IB分组。但是,当按下第二个UITableViewController时,它显示为普通UITableView。有办法解决这个问题吗 作为一个健全性检查,我更改了代码,以便第二个UITableViewController不是从第一个UITableViewController推送的,它看起来
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.text isEqualToString:@"Long Term Disability"]) {
LongDisabilityTableView *ldvc = [LongDisabilityTableView alloc];
[self.navigationController pushViewController:ldvc animated:YES];
}
if ([cell.textLabel.text isEqualToString:@"Short Term Disability"]) {
ShortDisabilityTableView *sdvc = [ShortDisabilityTableView alloc];
[self.navigationController pushViewController:sdvc animated:YES];
}
}
如果要推送到UITableViewController,可以通过执行以下操作之一强制对表进行分组:
初始化
超级的
这需要添加到UITableViewController中
复核
确保未将UITableView放置在UIViewController上
确保从didSelectRowAtIndexPath调用代码中的正确控制器:
更新,在代码之后添加
有一个原因,你没有使用init。请参见上面的第一个示例
您还应将代码更改为:
/* This assumes that your Long Term Disability cell is at index 0
and that your Short Term Disability cell is at index 1.
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch ( indexPath.row )
{
case 0: /* @"Long Term Disability" */
LongDisabilityTableView *ldvc = [[LongDisabilityTableView alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:ldvc animated:YES];
break;
case 1: /* @"Short Term Disability" */
ShortDisabilityTableView *sdvc = [[ShortDisabilityTableView alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:sdvc animated:YES];
break;
}
}
显示用于推送到这些控制器的代码,以及是否从代码创建UITableView。UITableView是在IB中创建的。正在加载的数据在代码中,但我认为这不是问题所在。无论如何,发布代码时,您可能会做一些简单的触发操作,而不是分组。
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:UITableViewStyleGrouped]; /* This is where the magic happens */
if (self) {
self.title = @"My Grouped Table";
}
return self;
}
/* This assumes that your Long Term Disability cell is at index 0
and that your Short Term Disability cell is at index 1.
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch ( indexPath.row )
{
case 0: /* @"Long Term Disability" */
LongDisabilityTableView *ldvc = [[LongDisabilityTableView alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:ldvc animated:YES];
break;
case 1: /* @"Short Term Disability" */
ShortDisabilityTableView *sdvc = [[ShortDisabilityTableView alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:sdvc animated:YES];
break;
}
}