相关文章推荐
近视的豆浆  ·  java 数组过滤_java ...·  1 年前    · 
强悍的书签  ·  unknown error: ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a very simple application with a UITableViewController. Upon EDITING, I am trying to slide a row into position 0 of the first section. The new row should have an INSERT editing style while the existing row should have a DELETE style.

I've overridden the following 4 methods:

// Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (self.editing && section == 0) { return2; return 1; - (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath { int section = indexPath.section; int row = indexPath.row; if (self.editing && section == 0 && row == 0) { return UITableViewCellEditingStyleInsert; return UITableViewCellEditingStyleDelete; - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView beginUpdates]; if (editing) { [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationLeft]; } else { [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates]; // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ClientsControllerCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleValue1reuseIdentifier:CellIdentifier] autorelease]; int section = indexPath.section; int row = indexPath.row; if (self.editing && section == 0 && row == 0) { cell.textLabel.text = @"Add Me"; cell.detailTextLabel.text = @"Detail text"; } else { cell.textLabel.text = @"Test me"; cell.detailTextLabel.text = @"Detail text"; return cell;

But as soon as I go into EDIT mode, "both" cells end up with an editing style of UITableViewCellEditingStyleInsert.

If I change my logic and append the new cell to the END - then it correctly draws the cells with a DELETE style and the new cell get's an INSERT.

Either way, tableView:editingStyleForRowAtIndexPath gets invoked 4 times. In fact, if I insert the new cell into section:0 row:0, this method gets called with section:row 0:0, 0:1, 0:0, 0:0. Whereas if I append the new cell into section: row:1, this method gets called with section:row 0:0, 0:1, 0:0, 0:1.

What am I missing? I should be able to insert a row and catch it right? For some reason, I can't see section=0 row=1 come through a second time.

-Luther

There's another question on StackOverflow that appears to ask essentially the same thing: SO 1508066 .

The answer there claims it's nonstandard to put the Insert row at the top; it should go at the bottom instead. I'm not sure I agree with that contention, but it's certainly the path of least resistance.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .