【问题标题】:selectRowAtIndexPath only works when allowsMultipleSelection is YESselectRowAtIndexPath 仅在 allowedMultipleSelection 为 YES 时有效
【发布时间】:2012-08-17 11:56:16
【问题描述】:

我有一个UITableViewController,它根据用户之前的选择动态填充。在某些情况下,用户应该能够选择表中的多行,但在其他情况下,他不应该。无论哪种情况,我都想自动选择第一行,但我发现它只有在我有 allowsMultipleSelection = YES 时才有效。

以下代码在-(void)viewDidLoad期间调用

switch(self.field)
{
    case people:
       self.options = (NSArray *)[data objectForKey:@"People"];
       self.tableView.allowsMultipleSelection = YES;
       enter code here break;
    case place:
        self.options = (NSArray *)[data objectForKey:@"Places"];
        self.tableView.allowsMultipleSelection = NO;
        break;
    case reason:
        self.options = (NSArray *)[data objectForKey:@"Reasons"];
        self.tableView.allowsMultipleSelection = NO;
        break;
}

[self.tableView reloadData];
NSIndexPath *zeroPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:zeroPath animated:NO scrollPosition:UITableViewScrollPositionNone];

这适用于“人物”案例,但不适用于其他案例。我试过将allowMultipleSelection设置为YES,这样就解决了选择的问题,但是在应用逻辑上是不可接受的。

我是否遗漏了有关这些功能如何工作的一些信息?如何以编程方式在单个选择表中选择一行?

【问题讨论】:

  • 您是否尝试在禁用self.tableview.allowMultipleSelection 后添加self.tableview.allowSelection = YES; ?

标签: iphone ios uitableview ios5


【解决方案1】:

修复了模板中的以下内容:

// Uncomment the following line to preserve selection between presentations.
self.clearsSelectionOnViewWillAppear = NO;

仍然不确定为什么它以前不起作用。

【讨论】:

    【解决方案2】:
    for (NSIndexPath *indexPath in [self.tableView indexPathsForSelectedRows]) {
        [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
    [self.tableView selectRowAtIndexPath:zeroPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 2013-02-11
      • 2021-01-11
      相关资源
      最近更新 更多