从具有多个部分的UITableView中删除行

时间:2011-06-24 14:33:44

标签: iphone ios uitableview syntax

我正在开发一个iPad应用程序,其中一个屏幕具有包含多个部分的嵌入式tableview。每个部分都由自己的数组(array1和array2)填充。

我创建了一个按钮,可以将此表置于编辑模式。但是,我需要改变我的

$tableView:commitEditingStyle:forRowAtIndexPath

以某种方式确定所选行所在的部分,并从关联的数组中删除该条目。有没有人有任何想法?

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle == UITableViewCellEditingStyleDelete){     
//This is the line i need to change...
        [array1 removeObjectAtIndex:indexPath.row];
        [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

6 个答案:

答案 0 :(得分:4)

看起来你需要从正确的数组中删除该行......在这些部分上使用switch语句可能会起作用(但只有在有一定数量的部分时,你似乎表明存在)

因此,您要从数组中删除对象,请确保根据该部分将其从正确的数组中删除。

  switch (indexPath.section) {
    case 0
      [array0 removeObjectAtIndex:indexPath.row];
      break;
    case 1
      [array1 removeObjectAtIndex:indexPath.row];
      break;
  }

答案 1 :(得分:2)

这样做......使用

获取所需的部分
 NSInteger section = [indexPath section];
 if (section == 0)
     {
       // write your code for deleting rows in your 1st section
      }

 if (section == 1)
     {
       // write your code for deleting rows in your 2nd section
      }

  // This is the idea...

答案 2 :(得分:2)

indexPath变量具有属性行和节。所以,你可以这样做:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if(editingStyle == UITableViewCellEditingStyleDelete){

    if (indexPath.section == 1) {
        // do something
    }
}

答案 3 :(得分:1)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

是确定选择了哪个单元格所需的方法。

如果您只想确定显示的实际部分,可以使用indexPath.section

执行此操作
  NSInteger section = [indexPath section];

答案 4 :(得分:1)

试试这个:

[[self.mainArrayList objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];

答案 5 :(得分:0)

单元格的commitEditingStyle:...方法has the section and row的indexPath参数。也许我误解了这个问题,你能澄清你的意思是'从相关数组中删除它' - 你的表中每个部分都有一个数组吗?