我想让用户能够删除一个表格单元格,目前我有删除滑动操作,但我不知道如何实际删除选定的单元格!如您所知,我正在使用Swift和Xcode 6.3.1进行编程。
var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -> Void in
tableView.editing = false
println("deleteAction")
}下面是dropbox上的截图:删除滑动动作的截图
发布于 2015-06-06 10:29:58
如果您想要从tableView中完全删除一个单元格,那么您也必须从您的表数组中删除它。考虑以下代码:
var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -> Void in
tableView.editing = true
// Remove it from your TableArray and If it is stored into any local storage then you have to remove it from there too because if you doesn't remove it from your local storage then when you reload your tableview it will appears back
self.tableData.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
return [deleteAction]
}发布于 2015-06-06 10:19:58
这可能对你有帮助。
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from your array and updating the tableview)
if let tv=tableView
{
items.removeAtIndex(indexPath!.row)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}发布于 2015-06-06 10:25:34
您必须实现editActionsForRowAtIndexPath和commitEditingStyle
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
__weak SampleViewController *weakSelf = self;
UITableViewRowAction *actionRed =
[UITableViewRowAction
rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"Delete"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"Delete!");
[weakSelf.itemsList removeObjectAtIndex:indexPath.row];
[weakSelf.tableView setEditing:NO animated:YES];
[weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}];
actionRed.backgroundColor = [UIColor colorWithRed:0.844 green:0.242 blue:0.292 alpha:1.000];
return @[actionRed];
}
/*
* Must implement this method to make 'UITableViewRowAction' work.
*
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
}下面是一个示例ActionRowTest
https://stackoverflow.com/questions/30681559
复制相似问题