【发布时间】:2011-04-06 01:40:52
【问题描述】:
我知道这听起来可能是个愚蠢的问题,但我到处都看过。我该怎么做?
我知道如何使用 swype-to-delete 方法执行此操作,但我如何在该功能之外执行此操作?
请发布一些代码示例。
谢谢!
库尔顿
【问题讨论】:
标签: objective-c ios4 uitableview
我知道这听起来可能是个愚蠢的问题,但我到处都看过。我该怎么做?
我知道如何使用 swype-to-delete 方法执行此操作,但我如何在该功能之外执行此操作?
请发布一些代码示例。
谢谢!
库尔顿
【问题讨论】:
标签: objective-c ios4 uitableview
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
insertIndexPaths 是要插入到表中的 NSIndexPaths 数组。
deleteIndexPaths 是要从表中删除的 NSIndexPaths 数组。
索引路径的示例数组格式:
NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
nil];
【讨论】:
theTableView 部分有关。尝试self.tableView 时,它告诉我它不是结构或联合...谢谢!
self.yourTableViewOutlet 就可以了。
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)
在 Swift 2.1+ 中
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([YourIndexPathYouWantToInsertTo], withRowAnimation: .Fade)
tableView.endUpdates()
你可以像这样轻松地创建一个 NSIndexPath:
NSIndexPath(forRow: 0, inSection: 0)
【讨论】:
你想要这两种方法:insertRowsAtIndexPaths:withRowAnimation:和deleteSections:withRowAnimation:它们都在UITableView documentation中有详细说明。
【讨论】:
斯威夫特 4.0
tableView.beginUpdates()
tableView.deleteRows(at: [YourIndexPathYouWantToDeleteFrom], with: .fade)
tableView.insertRows(at: [YourIndexPathYouWantToInsertTo], with: .fade)
tableView.endUpdates()
要创建 IndexPath,请使用:
IndexPath(row: 0, section: 0)
【讨论】: