1

I have a checkmark button inside of my custom tableview cell. When that button is tapped in my tableview, I want the row to delete.

Right now, my code below deletes the row if the cell itself is tapped, but not if the button inside the cell is tapped. How can I make it so that if the button inside the cell is tapped, the row deletes?

ViewController.m

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

    if (tableView == self.sidetableView) {

        NSMutableDictionary *nodeData = [[self.myFriendData objectAtIndex:indexPath.row] mutableCopy];

        NSString *nid = [nodeData objectForKey:@"nid"];
        [nodeData setObject:nid forKey:@"nid"];

        [self.myFriendData removeObjectAtIndex:indexPath.row];

        [DIOSNode nodeDelete:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {

            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

            NSLog(@"node deleted!");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"could not delete node!");
        }];


    }

CustomTableViewCell.m

-(void)accepted {

    NSString *friendAccepted = @"friendAccepted";
    [[NSUserDefaults standardUserDefaults] setObject:friendAccepted forKey:@"friendStatus"];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

-(void)alreadyAccepted {

    NSString *savedValue = [[NSUserDefaults standardUserDefaults]
                            stringForKey:@"friendStatus"];
    NSString *accepted = savedValue;

    if (savedValue == NULL){


    } else {



        self.acceptFriend.userInteractionEnabled = NO;
        [self.acceptFriend setImage:[UIImage imageNamed:@"checkedgreen.png"] forState:UIControlStateNormal];


    }
}


- (IBAction)acceptFriend:(id)sender {

    [self alreadyAccepted];
    [self accepted];

        [self.acceptFriend setImage:[UIImage imageNamed:@"checkedgreen.png"] forState:UIControlStateNormal];

}
Brittany
  • 1,359
  • 4
  • 24
  • 63

2 Answers2

1

Well I can give a general idea of how this can be achieved in one way.

Declare an Outlet of the button in the cell class

@property (nonatomic, retain) IBOutlet UIButton *deleteButton;

Next in the cellForRowAtIndexPath datasource assign the target to your button to a function within the ViewController. Also assign the tag to this button.

In the action of this function put in the code and logic for deleting the data from your model/datasource and reload the tableView.

Please refer to this link to assign action to your button in the ViewController. Hope this helps.

Code Snippet

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.myButton.tag = indexPath.row
    [cell.myButton addTarget:self 
             action:@selector(myAction) 
   forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
  • I understand the first part, but the second part is confusing me: '...assign the target to your button to a function within the viewcontroller - also assign the tag to this button'. How do I go about doing this? – Brittany Nov 04 '17 at 17:59
  • try the updated answer if it doesn't I will update the answer with the code snippet to do this. – Md. Ibrahim Hassan Nov 04 '17 at 18:00
  • Please update code snippet - lol I'm totally turned around :/ ha ha sorry, and thank you! – Brittany Nov 04 '17 at 18:05
  • 2 mins please I can't find it writing it down – Md. Ibrahim Hassan Nov 04 '17 at 18:15
  • Hey! I think I got it, but my issue now is that 'tableView' isn't found (e.g. this line [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; ) if I place it inside my action? I tried self.sidetableView instead, but it causes my app to crash with: -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]' – Brittany Nov 04 '17 at 18:18
  • take an outlet of the tableview in your VC and instead of tableView use that outlet name I think this would make the error go away please try and let me know – Md. Ibrahim Hassan Nov 04 '17 at 18:21
  • That's what I tried, and it threw me the error I mentioned above? Hence self.sidetableView (that's the outlet name). – Brittany Nov 04 '17 at 18:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158225/discussion-between-brittany-and-md-ibrahim-hassan). – Brittany Nov 04 '17 at 18:25
  • sorry there is one more thing to it the indexpath the button just gives you the tag thats is the indexPath.row make an indexPath and pass it on – Md. Ibrahim Hassan Nov 04 '17 at 18:25
  • @Brittany please check if your `indexPath` is nil or not for this statement `@[indexPath]` – 3stud1ant3 Nov 04 '17 at 18:25
0

I think one way can be to use delegate pattern to solve this problem.

First make a delegate in your UITableViewCell subclass.

@protocol CellCalssDelegate <NSObject>

- (void)deleteCellForIndexPath:(NSIndexPath *)indexPath;

@end

@interface MyCellSubclass: UITableViewCell
@property (nonatomic, weak) id <CellCalssDelegate > delegate;

@end

Second add the IBAction from the button inside this cell from the storyboard.

-(IBAction) deleteButtonTapped: (UIButton *)button; {

             [self.delegate deleteCellForIndexPath: self.indexPath];

}

Then Make a property of indexPath in your cell class

Then in the class(ViewController) where you are implementing the UITableViewDataSource and UITableViewDelegate methods.

ViewContrller.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      ....  
      cell.delegate = self;

      cell.indexPath = indexPath;

     ...
     return cell;
}

//CellDelegate Method...
-(void) deleteCellForIndexPath:(NSIndexPath)indexPath; {

        //write your logic for deleting the cell

}
3stud1ant3
  • 3,586
  • 2
  • 12
  • 15