简体   繁体   中英

How Do I Delete A Cell From My TableView By Tapping A Button On The Cell?

(I manage the notes and accounts with parse)I have made a notes app that uses UITableView to display notes that you can create as well as edit, which anyone who creates an account and signs in can view.(Don't ask me why I just made it for fun) But my problem is that I want to be able to delete a note by tapping the "Delete Post" button on the note: http://imgur.com/rB4y7WB and I have spent the last two days googling away trying to find the answer and all I am getting is sites or videos with tutorials on how to swipe the cell to delete, which is not what I need.

Any help would be great!

Hope this is work for you.

NSMutableArray *arrColor = [[NSMutableArray alloc] initWithObjects:@"White",@"Blue",@"Green",@"Yellow",@"Purple",@"Black", nil];

// UITableView DataSource Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [arrColor count]; 
}

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

    CustomTableViewCell *objCustomCell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifer];

    if (!objCustomCell) {
        objCustomCell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
    }

    objCustomCell.btnDelete.tag = indexPath.row;
    [objCustomCell.btnDelete addTarget:self action:@selector(actionDeleteBtn:) forControlEvents:UIControlEventTouchUpInside];
    return objCustomCell;
}

-(void)actionDeleteBtn:(id)sender
{
    UIButton *btn = (UIButton *)sender;

    [arrColor removeObjectAtIndex:btn.tag];
    [tblColorList reloadData];
}

Thanks :)

There are two issues you must consider, deleting the note and deleting the cell from the UITableView. It is likely that the best place to put the delete method is in the view controller. The only problem is how to tell the view controller which note to delete.

You could make the delete method an IBAction, attach all your delete buttons to it and tag each button with the index of the note its cell is displaying. Then in the method, you examine the button's tag and delete the correct note.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM