【发布时间】:2013-03-13 20:14:20
【问题描述】:
我有一个UITableView 有两个部分。基于用户交互(选择和取消选择),我的数据源和UITableView 被更新以在部分之间移动数据。最初它们是第 0 节中的唯一数据。当我点击一个单元格时,willSelectCellAtIndexPath 和 didSelectCellAtIndexPath 被调用。正如预期的那样,当我再次点击同一个单元格时,didDeselectCellAtIndexPath 被调用。
即使在我开始将数据向下移动到第 1 部分并选择和取消选择之后,UITableView's 委托方法也会被适当地调用。
一旦将所有数据移至第 1 节,UITableView 就会开始表现出奇怪的行为。我最初可以选择一个呼叫,然后调用didSelectCellAtIndexPath。但是,当我再次点击它时,永远不会调用 didDeselectCellAtIndexPath。相反,对选定单元格的任何点击(我已经确认它确实是通过 [tableView indexPathsForSelectedCells] 或第 1 节中的任何其他单元格选择的,只会导致 willSelectIndexPath 和 didSelectIndexPath 被调用。
我在这些委托方法中有相当多的代码是不相关的(我相信)......我没有在任何地方显式更改单元格的选定状态。我已经发布了willSelect 方法,如有需要可以发布更多。
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (remainingItemIsSelected && indexPath.section == 0) {
//other cells in the remaining items section are selected and a cell from that section is being selected
NSMutableIndexSet *arrayIndexesToBeDeleted = [[NSMutableIndexSet alloc] init];
for (NSIndexPath *previouslySelectedIndexPath in [tableView indexPathsForSelectedRows]) {
if (((ReceiptItem *)[self.remainingReceiptItems objectAtIndex:previouslySelectedIndexPath.row]).allocated == YES) {
//update data sources
NSLog(@"%@%i%@,%i",@"Section #:",previouslySelectedIndexPath.section,@" Row #:",previouslySelectedIndexPath.row);
[self.assignedReceiptItems addObject:[self.remainingReceiptItems objectAtIndex:previouslySelectedIndexPath.row]];
[arrayIndexesToBeDeleted addIndex:previouslySelectedIndexPath.row];
//update index path arrays
[self.receiptItemsToDeleteIndexPaths addObject:previouslySelectedIndexPath];
[self.receiptItemsToAddIndexPaths addObject:[NSIndexPath indexPathForRow:self.assignedReceiptItems.count-1 inSection:1]];
//update the pressed indexpath to equal to resulting indexpath to pass on to the didSelect method
if (previouslySelectedIndexPath.row < indexPath.row) {
indexPath = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0];
}
}
}
//Delete assigned items from the remaining receipt items
[self.remainingReceiptItems removeObjectsAtIndexes:arrayIndexesToBeDeleted];
//update table (move allocated item down)
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:self.receiptItemsToDeleteIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView insertRowsAtIndexPaths:self.receiptItemsToAddIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
if (self.remainingReceiptItems.count == 0) {
[tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
//other cells in the remaining items section are selected and a cell from assigned items is being selected
return nil;
}
return indexPath;
}
【问题讨论】:
-
你是否设置了断点来检查方法是否被调用?还是您只考虑您的
NSLog()声明? -
我设置了断点并确认没有调用
didDeselectRowAtIndexPath。此外,我确认willDeselectRowAtIndexPath也没有被调用。
标签: ios objective-c uitableview