1
我有一個tableView有三個部分。 Section 0
有一組固定的單元格,Section 1
可以有不同數量的單元格,並且Section 2
也是固定的。當用戶按下Section 2
中的最後一個單元格時,我想在Section 1
中添加一行。UITableView - 在索引路徑插入和刪除行問題
我的問題是當我點擊Section 2
中的第一行,它似乎正確插入行。但是當我以後刪除一行時,我不再可以插入項目(doSelectRowAtIndexPath),當我在Section 2
中的第一行時不會被調用。我想這可能是我的reuseIdentifiers問題,但我看不到在哪裏。有任何想法嗎?
我tabelView委託方法是這樣的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = [self reuseIdentifierForIndexPath:indexPath];
id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
[self configureCell:cell atIndexPath:indexPath];
}
// Update cell content..
[self updateContentOfCell:cell atIndexPath:indexPath];
return cell;
}
//Helper method to return a reuseIdentifier for TBV cells.
- (NSString *)reuseIdentifierForIndexPath:(NSIndexPath *)indexPath {
NSString *reuseIdentifier;
switch (indexPath.section) {
case 0:
reuseIdentifier = @"StandardCell";
break;
case 1:
reuseIdentifier = @"SectionCellInWhichIWantToAddMoreCells";
break;
case 2:
switch (indexPath.row) {
case 0:
reuseIdentifier = @"WantToAddCellsToSectionOneByPressingAtThisRow";
break;
case1 :
reuseIdentifier = @"OtheerCellInSectionTwo";
break;
}
break;
default:
reuseIdentifier = @"StandardCell";
break;
}
return reuseIdentifier;
}
// Configure cell with type
- (void)configureCell:()cell atIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0:
[self configureFirstSectionCell:cell atIndexPath:indexPath];
break;
case 1:
[self configureFirstSectionCell:cell atIndexPath:indexPath];
break;
case 2:
[self configureSecondSectionCell:cell atIndexPath:indexPath];
break;
default:
break;
}
}
// Update cell content after reload
- (void)updateContentOfCell:(id)cell atIndexPath:(NSIndexPath *)indexPath {
// exactly the same structure as configureCell but calls different (updateSecondSectionCell) methods in it's switch cases.
}
- (void)configureFirstSectionCell:(id)cell atIndexPath:(NSIndexPath *)indexPath {
// Sets initial values of each cell
}
// Where I want to add things...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 2 && indexPath.row == 0) {
[self.view endEditing:YES];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:1] inSection:1];
[tableView beginUpdates];
[self.dataSource addObject:@""];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
我刪除這樣的:
[self.view endEditing:YES];
[self.tableView beginUpdates];
[self.dataSource removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
現在會發生什麼是相當陌生的。當我點擊Section 2
中的第一行時,它似乎正確插入該行。但是當我以後刪除一行時,我不再可以插入項目(doSelectRowAtIndexPath),當我在Section 2
中的第一行時不會被調用。我想這可能是我的reuseIdentifiers問題,但我看不到在哪裏。有任何想法嗎?
是您的問題解決 –
@CharanGiri沒有。 – Anders
您在哪個方法中寫刪除操作 –