Objective c 每当选中时在一行中添加自定义按钮的情况

Objective c 每当选中时在一行中添加自定义按钮的情况,objective-c,ios6.0,Objective C,Ios6.0,目标: 无论何时选择,在一行中添加一个标题为Delete的自定义按钮 并在单元格选择更改时将其删除,等等,将“删除”添加到最后一个选择中 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ self.myIndexPath=indexPath; UIButton *btnCustomDelete=[[UIButton alloc] initWithFr

目标:

  • 无论何时选择,在一行中添加一个标题为
    Delete
    的自定义按钮
  • 并在单元格选择更改时将其删除,等等,将“删除”添加到最后一个选择中

    (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
        self.myIndexPath=indexPath;
        UIButton *btnCustomDelete=[[UIButton alloc] initWithFrame:CGRectMake(260, 10, 60, 7)];
        [btnCustomDelete setTitle:@"Delete" forState:UIControlStateNormal];
        [tblCellForContactTable.contentView addSubview: btnCustomDelete];  //I think correction wants here  
    
        [btnCustomDelete addTarget:self action:@selector(actionCustomDelete:)  forControlEvents:UIControlEventTouchUpInside];
    }
    
    -(IBAction) actionCustomDelete:(id)sender{
        [arrForMyContacts removeObject:[arrForMyContacts objectAtIndex:myIndexPath.row]];
        [tblForContacts reloadData];
    }
    

但是,它并不是一直有效。

你是对的。您应该将按钮作为子视图添加到实际的
UITableViewCell
对象中,您可以使用
tableView:cellforrowatinexpath:
数据源方法来实现

因此,您的实现可以是(在创建
btnCustomDelete
之后):

请继续读

您的实现不是从表中删除行的健康解决方案。通过实现一些
UITableView
数据源和委托方法,您可以轻松执行删除操作,而无需添加自定义按钮,如下所示:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [arrForMyContacts removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

如果选择单元格A,然后选择单元格B(因此self.myindexath=单元格B的索引路径),然后点击单元格A的删除按钮,会发生什么情况?它将删除单元格B。您是问问题还是解决我的问题。请澄清我的问题。我的英语很差。我提出了一个你实施的潜在问题。如果您按照我编写的顺序执行我所说的操作,您的应用程序将出现错误行为。是的,我想通过单元格A删除单元格B中的单元格,因为
indepath
ivar在我的自定义方法中不可见。因此,只有我将其复制到我的公共属性
self.myIndexPath
。而@ismailgulek给出了解决方案。但是,他/她没有解释。哪一个?您的自定义按钮还是另一个按钮?您正在更新所选内容的
myindexath
属性,但没有更新
tblCellForContactTable
。因此,该属性不能是所选的实际单元格。它可能被遗忘了。我刚想出来。顺便说一句,我仍然建议您使用第二个解决方案,使用
UITableView
datasource和delegate。但是,我删除了
arrForMyContacts
对象并重新加载数据。那么我为什么要更新呢?您的自定义方法将更新整个表,当然还有所有单元格(首先是可见的单元格)。这是一种昂贵的方法,尤其是如果您的
tableView:cellforrowatinexpath:
方法做了大量工作。文档中还指出:
UITableView将推迟行或节的任何插入,直到它处理了行或节的删除。无论插入和删除方法调用的顺序如何,都会发生这种情况。这与在可变数组中插入或删除项不同,在可变数组中,操作可能会影响用于后续插入或删除操作的数组索引。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [arrForMyContacts removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}