1

客观的:

  • 选择时添加一个自定义按钮,标题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];
    }
    

但是,它并非一直有效。

4

1 回答 1

1

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

因此,您的实现可能类似于(在创建您的 之后btnCustomDelete):

UITableViewCell * myCell = [tableView cellForRowAtIndexPath:indexPath]
[myCell.contentView addSubview: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];
}
于 2014-03-27T13:31:16.407 回答