How to delete a cell in UITableView by using custom button in cell?
I 开发者_JAVA百科have UITableView. I customized the cell height (80.0f).
I have 4 labels(UILabel) and 1 image(UIImage) and 3 buttons (UIButton). one of the button is delete button. By touching the cell or button(play button) on the image a video is loaded and played.
I need to delete the cell by touching delete button. I wrote a selector for delete button. I can delete the video from the library. But how to delete the cell from the table ?
Thank you. The following image shows the delete button of my program.
alt text http://www.freeimagehosting.net/uploads/c2036dee19.png
If your model is updated as you say, use this method on the table view to remove the cell with animation:
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/deleteRowsAtIndexPaths:withRowAnimation:
The table view will clean up the cell for you.
Note that the commitEditingStyle:
method is only called when the tableview's delete button (the one you get via editing mode or swipe-to-delete) is touched. From your question it sounds like you have a custom button which is why this method is not called. You could call it yourself but I wouldn't recommend it since it should only be called by the tableview itself.
I got the solution when I did this.
-(void)deleteClicked:(id)sender
{
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
NSArray *arrayDelete = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryD = [arrayDelete objectAtIndex:0];
NSDictionary *dictOfplist1 = [contentArray objectAtIndex:clickedButtonPath.row];
//To remove the videos from Documents folder
NSString *pathTemp1 = [documentsDirectoryD stringByAppendingString:[dictOfplist1 objectForKey:@"fileVideoE"]];
BOOL succeed1 = [[NSFileManager defaultManager] removeItemAtPath:pathTemp1 error:nil];
//To remove the images shown in cell from Documents folder.
NSString *pathTemp2 = [documentsDirectoryD stringByAppendingString:[dictOfplist1 objectForKey:@"fileImageE"]];
BOOL succeed2 = [[NSFileManager defaultManager] removeItemAtPath:pathTemp2 error:nil];
//To remove the data from the plist which is in Documents folder.
NSString *pathDelete = [documentsDirectoryD stringByAppendingString:@"/details.plist"];
[contentArray removeObjectAtIndex:[clickedButtonPath row]];
[contentArray writeToFile:pathDelete atomically: YES];
//To reload the data of the plist after deleting the items from it.
[self.tableView reloadData];
}
Every thing was fine the cell is removed, the data in the plist is removed the videos and images in Documents folder are removed.
Here is the image when I delete the 2nd row.
Thank You.
alt text http://www.freeimagehosting.net/uploads/f3c96ae3a7.png
Call:
[self.tableView reloadData];
You need to do some research on the MVC idiom.
精彩评论