Brittany
Brittany

Reputation: 1439

Obj-C - Delete tableview row if button in custon tableview cell is tapped

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

}

Upvotes: 1

Views: 572

Answers (2)

3stud1ant3
3stud1ant3

Reputation: 3606

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

}

Upvotes: 0

Md. Ibrahim Hassan
Md. Ibrahim Hassan

Reputation: 5467

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

Upvotes: 1

Related Questions