如何使用单元格中的自定义按钮删除UITableView中的单元格?

问题描述:

我有UITableView。我定制了细胞高度(80.0f)。我有4个标签(UILabel)和1个图像(UIImage)和3个按钮(UIButton)。其中一个按钮是删除按钮。通过触摸图像上的单元格或按钮(播放按钮),可以加载和播放视频。如何使用单元格中的自定义按钮删除UITableView中的单元格?

我需要通过触摸删除按钮来删除单元格。 我写了一个删除按钮选择器。我可以从库中删除视频。但是如何从表中删除单元格?

谢谢。 下图显示了我的程序的删除按钮。

alt text http://www.freeimagehosting.net/uploads/c2036dee19.png

当我这样做时,我得到了解决方案。

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

每一件事情是罚款该单元被删除,在plist中的数据被删除的文档文件夹中的视频和图像将被删除。

这是当我删除第二行时的图像。
谢谢。

alt text http://www.freeimagehosting.net/uploads/f3c96ae3a7.png

电话:

[self.tableView reloadData]; 

你需要做一些研究的MVC成语。

如果模型更新正如你所说,使用表格视图这种方法来删除与动画单元:

- (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

表视图将清理电池为你。

请注意,commitEditingStyle:方法仅在桌面视图的删除按钮(通过编辑模式或刷卡删除方式获取的按钮)被触摸时调用。从你的问题,这听起来像你有一个自定义按钮,这就是为什么这个方法没有被调用。你可以自己调用它,但我不会推荐它,因为它只能由tableview自己调用。