1

在我的应用程序执行期间的各个时间点,需要以编程方式使用 UITableView 选择一行

[self.tableView selectRowAtIndexPath:selectedCellIndexPath animated:false scrollPosition:UITableViewScrollPositionMiddle];

这工作正常,该行在后端突出显示并标记为选中。该表被配置为允许多项选择。

但是,如果用户随后取消选择该行,则不会调用“didSelectRowAtIndexPath”函数。奇怪的是,如果他们重新选择它,该函数会按预期再次调用。

知道可能出了什么问题吗?选择行时是否有其他属性或需要重置的东西?

4

4 回答 4

3

实现UITableViewDelegate协议.h:

@interface YourView : UIView <UITableViewDataSource,UITableViewDelegate>

然后在 .m 中:

self.tableView.delegate = self;

然后写:

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

你的方法将被调用

于 2013-10-20T21:41:46.310 回答
1

如果用户随后取消选择该行,UITableViewDelegate则将调用关联的方法:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
于 2013-10-20T21:38:35.747 回答
1

有两种选择/取消选择行的方法UITableViewDelegate

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

`- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath`
于 2013-10-20T21:38:44.837 回答
0

一般来说,如果您从代码触发操作,则不应期望调用委托方法。它们只会由用户交互触发。如果您以编程方式将单元格设置为选中,那么取消选择的用户交互将触发关联的委托方法是很自然的。

于 2013-10-20T21:55:13.230 回答