【发布时间】:2011-09-07 19:16:05
【问题描述】:
在我的应用程序中,我需要删除表格中的多行,编辑表格并在表格旁边获得一个复选框。选中后,表格单元格将被删除。它就像 iPhone 消息应用程序。我该怎么做,请帮助我。
【问题讨论】:
标签: iphone objective-c ios uitableview ios4
在我的应用程序中,我需要删除表格中的多行,编辑表格并在表格旁边获得一个复选框。选中后,表格单元格将被删除。它就像 iPhone 消息应用程序。我该怎么做,请帮助我。
【问题讨论】:
标签: iphone objective-c ios uitableview ios4
如果我正确理解了您的问题,您基本上想以某种方式标记 UITableViewCells(复选标记);然后,当用户点击主“删除”按钮时,所有标记的UITableViewCells 都会从UITableView 连同其相应的数据源对象一起删除。
要实现 复选标记 部分,您可以考虑在 UITableViewCell 的 accessory 属性之间切换 UITableViewCellAccessoryCheckmark 和 UITableViewCellAccessoryNone。在以下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 自定义UITableViewCells。
您可以通过两种方式设置主“删除”按钮:
在任何一种情况下,最终都必须在按下主“删除”按钮时调用一个方法。该方法只需要遍历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 个 UIButton 称为“EDIT”并将其连接到 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
}
希望对你有帮助。
【讨论】:
你想寻找deleteRowsAtIndexPath,你的所有代码都挤在[yourTable beginUpdates]和[yourTable endUpdates];之间
【讨论】:
我在这里找到了非常好的代码 请使用此代码来实现某些功能 Tutorials Link
【讨论】: