sim*_*com 6 iphone objective-c uitableview ios4 ios
在我的应用程序中,我需要删除表中的多行,编辑表并在表旁边的复选框.选中后,表格单元格将被删除.它就像iPhone消息应用程序.我怎么能这样做,请帮助我.
Jac*_*ard 18
如果我理解你的问题,你基本上想要以某种方式标记 UITableViewCell s(复选标记); 然后,当用户点击主"删除"按钮时,所有标记的 UITableViewCell s 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
}
Run Code Online (Sandbox Code Playgroud)
如果您想要更复杂的复选标记,也可以查看有关自定义UITableViewCell的帖子.
您可以通过两种方式设置主"删除"按钮:
在任何一种情况下,最终必须在按下主"删除"按钮时调用方法.该方法只需循环UITableViewCells中UITableView并确定哪些标记.如果已标记,请将其删除.假设只有一个部分:
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];
Run Code Online (Sandbox Code Playgroud)
假设通过"编辑"表示"删除单个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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23206 次 |
| 最近记录: |