didDeselectRowAtIndexPath问题

时间:2015-06-15 10:42:53

标签: ios uitableview

我在popover的滚动视图中有一个tableview。显示视图时,tableview中的底部单元格对用户不可见。如果我选择所有单元格然后取消选择第一个单元格,则取消选择视图单元格。有没有人遇到过这种行为?如果是这样,如何处理它?<​​/ p>

3 个答案:

答案 0 :(得分:1)

现在你的工作是找到tableview中的所有可见单元格,然后对其应用select / deselect。

UITableView *tableView = self.tableView; 
// Or however you get your table view

NSArray *paths = [tableView indexPathsForVisibleRows];

//  For getting the cells themselves

NSMutableSet *visibleCells = [[NSMutableSet alloc] init];

for (NSIndexPath *path in paths) 
{
[visibleCells addObject:[tableView cellForRowAtIndexPath:path]];
}

// Now visibleCells contains all of the cells you care about.

答案 1 :(得分:0)

-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:
    (NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
    (NSIndexPath *)indexPath {
    //stuff
    //as last line:
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

就此而言,可以随时随地从任何地方调用deselectRowAtIndexPath。

[self.myTableView deselectRowAtIndexPath:[self.myTableView 
    indexPathForSelectedRow] animated: YES];

答案 2 :(得分:0)

如果您使用的是dequeueReusableCellWithIdentifier:,请更改cellForRowAtIndexPath:以使用dequeueReusableCellWithIdentifier:forIndexPath:

UITableView个单元格中重复使用。这意味着它只生产绝对需要的数量。一旦新屏幕进入屏幕,最后一个屏幕就会被“回收”,而不是初始化一个全新的实例。

这使您的应用程序运行得更快。这也意味着您必须撤消在回收时所做的任何更改。

选择状态就是其中之一。 UITableView如果与相关的indexPath一起出列,则应自动为您管理。如果没有,它将不知道是否应该选择该特定单元格。