在我的应用程序中,我需要删除表中的多行,编辑表并获取表旁边的复选框。当选中时,表格单元格将被删除。这就像iPhone的消息应用程序。我该怎么做,请帮助我。在UITableView中同时编辑和删除多行
6
A
回答
18
如果我正确地理解你的问题,你基本上想以某种方式(复选标记)标记UITableViewCell
;那么当用户点击一个主“删除”按钮时,将从UITableView
中删除标记为的所有及其对应的数据源对象。
为了实现对号部分,你可能会考虑UITableViewCellAccessoryCheckmark
和UITableViewCellAccessoryNone
之间切换为UITableViewCell
的accessory
财产。作如下处理UITableViewController
委托方法亮点:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
if (c.accessoryType == UITableViewCellAccessoryCheckmark) {
[c setAccessoryType:UITableViewCellAccessoryNone];
}
//else do the opposite
}
也look at this post可能关于定制UITableViewCell
■如果你想要一个更复杂的对号。
你可以设置一个主“删除”按钮两种方式:
在这两种情况下,最终的方法必须被调用,当主“删除”按钮。该方法只需要遍历UITableView
中的UITableViewCells
并确定哪些标记已标记。如果标记,删除它们。假设只有一个部分:
NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
NSIndexPath *p = [NSIndexPath indexPathWithIndex:i];
if ([[tableView cellForRowAtIndexPath:p] accessoryType] ==
UITableViewCellAccessoryCheckmark) {
[cellIndicesToBeDeleted addObject:p];
/*
perform deletion on data source
object here with i as the index
for whatever array-like structure
you're using to house the data
objects behind your UITableViewCells
*/
}
}
[tableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted
withRowAnimation:UITableViewRowAnimationLeft];
[cellIndicesToBeDeleted release];
通过“编辑”假设你的意思是“删除单个UITableViewCell
”或“移动单个UITableViewCell
,”你可以实现在UITableViewController
以下方法:
- (void)viewDidLoad {
[super viewDidLoad];
// This line gives you the Edit button that automatically comes with a UITableView
// You'll need to make sure you are showing the UINavigationBar for this button to appear
// Of course, you could use other buttons/@selectors to handle this too
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//perform similar delete action as above but for one cell
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//handle movement of UITableViewCells here
//UITableView cells don't just swap places; one moves directly to an index, others shift by 1 position.
}
1
要寻找deleteRowsAtIndexPath
,与所有你的代码挤[yourTable beginUpdates]
之间& [yourTable endUpdates];
2
你可以把1的UIButton可以称之为“编辑”来包装它IBAction为。在IBAction写,所以你将能够按照您的要求。
-(IBAction)editTableForDeletingRow
{
[yourUITableViewNmae setEditing:editing animated:YES];
}
这将在左上角添加圆形红色按钮,您可以点击该按钮将出现点击,行将被删除。
您可以按如下方式实现UITableView的委托方法。
-(UITableViewCellEditingStyle)tableView: (UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
{
//Do needed stuff here. Like removing values from stored NSMutableArray or UITableView datasource
}
希望它有帮助。
1
我发现很好代码请使用此代码来实现某些功能 Tutorials Link
相关问题
- 1. 同时插入行和删除行。 UITableView
- 2. iPhone:在UITableView中添加和删除行与编辑模式
- 3. 从UITableView中删除编辑按钮?
- 4. 如何在UITableView中同时插入和删除一行
- 5. 在UITableView中进行多次编辑....
- 6. 编辑模式下的Uitableview:如何同时添加“+”(添加)和“ - ”(删除)按钮?
- 7. UITableView的编辑风格 - 添加/删除
- 8. 在uitableview中删除和添加行
- 9. 左滑动删除和编辑,删除
- 10. Laravel Datatables多行为编辑/删除,删除显示为文本
- 11. UITableView didSelectRow在编辑时?
- 12. 编辑UITableView时,单击红点不能显示'删除'按钮
- 13. 在textmate中同时在同一行上编辑多个单词
- 14. MVC CRUD编辑和删除
- 15. 限制编辑和删除
- 16. MKITView在UITableView和编辑
- 17. 从UITableview中删除行
- 18. 删除UITableView中的一行
- 19. MySQL多对多。删除或编辑?
- 20. 删除SQLITE行UITABLEVIEW
- 21. 从UITableView删除行
- 22. 在点击magento中的编辑链接时删除特定行
- 23. UITableView禁用滑动删除,但仍然在编辑模式下删除?
- 24. 在Silverlight数据网格中编辑,添加和删除行?
- 25. 我想在reactjs中编辑和删除表格行
- 26. 如何在Android中编辑和删除闹钟时间?
- 27. UITableView:我可以删除多行吗?
- 28. 在UITableview编辑模式下不显示左删除图标
- 29. UITableView添加/删除部分,而不在编辑模式?
- 30. UITableView在编辑模式 - 按删除使我的应用崩溃
可能重复的[从uitableview和删除选择多行](http://stackoverflow.com/questi ons/4954393/select-multiple-rows-from-uitableview-and-delete) – 2011-06-03 18:05:29
其他重复项:[1](http://stackoverflow.com/questions/6222661/how-to-delete-multiple-rows-in- table-view)[2](http://stackoverflow.com/questions/2727302/)[3](http://stackoverflow.com/questions/2949488/)[4](http://stackoverflow.com/问题/ 5973756 /) – 2011-06-03 18:06:24