开发者

How to use tableView:editingStyleForRowAtIndexPath?

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 开发者_如何学Crow=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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜