使UITableviewCells可删除

时间:2010-07-14 21:54:04

标签: iphone uitableview

如何使UITableView单元能够被用户删除?

2 个答案:

答案 0 :(得分:2)

一个简单的例子是:

// create a mutable array to manage your table data
NSMutableArray *tableList;

@property (nonatomic, retain) NSMutableArray *tableList;

在您的viewDidLoad方法中,您可以使用数据对其进行初始化(您应该先检查tableList是否为零)

NSString *path = [[NSBundle mainBundle] pathForResource:@"TableData" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];

self.tableList = array; 
[array release];

然后在实现文件中,实现此委托方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row]; 
    [self.tableList removeObjectAtIndex:row]; 

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

然后您可以通过

切换编辑模式
[self.tableView setEditing:!self.tableView.editing animated:YES];

答案 1 :(得分:1)

查看表视图数据源委托方法-tableView:commitEditingStyle:forRowAtIndexPath:

您在表视图委托(通常是表视图控制器)中覆盖此方法,并将删除逻辑放在此处。

例如,您可以调用UIAlertView来要求用户确认删除,或者只是直接操作数据模型,从表视图的数据源中删除对象。

相关问题