【问题标题】:Automatically cell selected UITableView自动选择单元格 UITableView
【发布时间】:2011-10-05 20:11:28
【问题描述】:

我的 UITableView 通过 PopOverViewController 打开,所以如何在应用加载后自动加载这些单元格之一,

MainViewController 上的单元格选择过程

- (void)setDetailItem:(id)newDetailItem {

    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];

        //---update the view---
        label.text = [detailItem description];
    }

}

在 TableViewController 中选择单元格:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    myAppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate];
    appDelegate.viewController.detailItem = [list objectAtIndex:indexPath.row];  

}

我在 TableViewController 中使用此代码但不起作用!这意味着按下 popOver 按钮后,代码只会突出显示单元格!

 [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];

我在不同的方法中使用了上面的代码,例如 viewDidAppearviewWillAppeardidSelectRowAtIndexPath 和 ...

谢谢

【问题讨论】:

  • 如果您希望调用 selectRowAtIndexPath:animated:scrollPosition: 会导致您的 tableView:didSelectRowAtIndexPath: 被调用,那么根据文档不会发生这种情况:“调用此方法 -selectRowAtIndexPath:animated:scrollPosition:- 不会导致代表接收tableView:willSelectRowAtIndexPath:tableView:didSelectRowAtIndexPath: 消息,也不会向观察者发送UITableViewSelectionDidChangeNotification 通知。”
  • 那么有什么解决办法吗?
  • 您可以自己做this 或致电tableView:didSelectRowAtIndexPath:(在代表上)。

标签: ios xcode uitableview


【解决方案1】:

当您调用 selectRowAtIndexPath:animated:scrollPosition: 时,tableView:didSelectRowAtIndexPath:不会在委托上调用。

来自selectRowAtIndexPath:animated:scrollPosition: 参考:

调用此方法不会导致委托接收到 tableView:willSelectRowAtIndexPath: 或 tableView:didSelectRowAtIndexPath: 消息,也不会发送 UITableViewSelectionDidChangeNotification 通知观察者。

所以,不要只打电话给selectRowAtIndexPath:animated:scrollPosition:

 [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];

您可以手动调用委托方法:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

if ([myTableView.delegate respondsToSelector:@selector(tableView:willSelectRowAtIndexPath:)]) {
    [myTableView.delegate tableView:self.tableView willSelectRowAtIndexPath:indexPath];
}

[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition: UITableViewScrollPositionNone];    

if ([myTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
    [myTableView.delegate tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}

【讨论】:

  • 谢谢你这个代码工作正常!但在我的应用加载后,label.text 不会改变,直到用户按下 popOverView 按钮
  • 您更新setDetailItem: 中的label.text,它在tableView:didSelectRowAtIndexPath: 中调用,对吗?现在tableView:didSelectRowAtIndexPath: 确实被调用了。因此,我的建议是在 tableView:didSelectRowAtIndexPath: 运行时检查某个变量是否为零(list?、label? 等)。
  • 没有任何 var 作为 nill ,不幸的是这不是我所期望的,用户必须按下 popOver 按钮来更改标签,另一个问题是当用户选择不同的单元格时如果他们按下再次将标签字符串按回 CELL 0 !
  • 那么我建议您发布一个新问题,其中包含这些其他问题的详细信息。
猜你喜欢
  • 2021-10-20
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多