1

Objective:

  • Add a custom button as titled Delete in a row whenever it selected
  • And remove it whenever cell selection changes and so on add 'Delete' to last selection.

    (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];
    }
    

But, its not worked all time.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Mohd Sadham
  • 435
  • 3
  • 19
  • What happens if I select cell A, then cell B (thus self.myIndexPath = the index path of cell B), then tap the Delete button of cell A? It'll delete cell B. – Cyrille Mar 27 '14 at 13:34
  • Are you ask question or solve my solution. Please clear me. Am weak in english. – Mohd Sadham Mar 27 '14 at 13:53
  • I am raising a potential problem of your implementation. If you do what I said in the order I wrote it, your app will have the wrong behaviour. – Cyrille Mar 27 '14 at 13:54
  • Yes, i want to delete cell in cell B through cell A because `indexpath` ivar not visible in my custom method. So only i copy that to my public property `self.myIndexPath`. And @ismailgulek gave solution. But, he/she not explain it. – Mohd Sadham Mar 27 '14 at 13:59

1 Answers1

1

You are right. You should add button as a subview to your actual UITableViewCell object, which you can achieve using tableView:cellForRowAtIndexPath: data source method.

So, your implementation can be something like (after creating your btnCustomDelete):

UITableViewCell * myCell = [tableView cellForRowAtIndexPath:indexPath]
[myCell.contentView addSubview:btnCustomDelete];

Please keep reading.

Your implementation is not a healthy solution to delete a row from a table. You can have delete action easily with implementing some UITableView data source and delegate methods, without adding a custom button, like below:

- (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];
}
ismailgulek
  • 1,029
  • 1
  • 11
  • 8
  • For which one? Your custom button or the other one? – ismailgulek Mar 27 '14 at 14:20
  • You were updating your `myIndexPath` property on selection, but you were not updating `tblCellForContactTable`. So that property could not be actual cell which is selected. It is probably forgotten. I just figured it out. By the way, i still advice you to use the second solution with `UITableView` datasource and delegate. – ismailgulek Mar 27 '14 at 14:26
  • However, i removed `arrForMyContacts` object and reload data. Then why i want to update? – Mohd Sadham Mar 27 '14 at 14:29
  • Your custom method will update the whole table and of course all cells (visible ones at first). This is an expensive way, especially if your `tableView:cellForRowAtIndexPath:` method do a lot of works. Also the documentation states: `UITableView defers any insertions of rows or sections until after it has handled the deletions of rows or sections. This happens regardless of ordering of the insertion and deletion method calls. This is unlike inserting or removing an item in a mutable array, where the operation can affect the array index used for the successive insertion or removal operation.` – ismailgulek Mar 27 '14 at 14:47