1
我想使UITableView像手風琴一樣工作。當一個行被點擊時,它應該在被點擊的行的正下方插入一個特殊行,然後從以前的點擊中刪除任何其他特殊行。我已經嘗試了很多東西,但下面的代碼是我最近的嘗試。如何在UITableView中同時插入和刪除一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *removeIndex;
for (int i = 0; i < [players count]; i++) {
NSString *player = [players objectAtIndex:i];
if ([player isEqualToString:@"ADJUST_SCORE_ROW"]) {
removeIndex = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
[players replaceObjectAtIndex:i withObject:@"DELETE_ME"];
break;
}
}
[scoreTableView beginUpdates];
NSIndexPath *insertPath;
if (removeIndex && [removeIndex row] < indexPath.row) {
insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
} else {
insertPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
}
[players insertObject:@"ADJUST_SCORE_ROW" atIndex:insertPath.row];
[scoreTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop];
for (int i = 0; i < [players count]; i++) {
NSString *player = [players objectAtIndex:i];
if ([player isEqualToString:@"DELETE_ME"]) {
[players removeObject:player];
break;
}
}
if (removeIndex) {
[scoreTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:removeIndex] withRowAnimation:UITableViewRowAnimationTop];
}
[scoreTableView endUpdates];
}
我認爲正在處理當我更新支持表的數組。我很高興錯了,但我認爲問題中包含的代碼處理了段和相應行的數量。 – 2012-02-01 16:40:37