您的位置:首页 > 移动开发

FirstApp,iphone开发学习总结11,表操作(移动、删除)

2012-05-10 09:27 615 查看
nav添加左按钮:

- (void)viewDidLoad
{
//...
UIBarButtonItem *tableEditBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(toggleEdit:)];
[[self navigationItem] setLeftBarButtonItem:tableEditBtn];
[tableEditBtn release];
}edit按钮的实现:

- (void)toggleEdit:(id)sender
{
if ([self isEditing]) {
[self setEditing:NO animated:YES];
}else{
[self setEditing:YES animated:YES];//设置编辑状态
}
}表删除:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[data removeObjectAtIndex:[indexPath row]];//先移除数据,再移除表里的显示
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}表移动:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSArray *moveRow = [data objectAtIndex:[sourceIndexPath row]];//保存移动的行
[data removeObjectAtIndex:[sourceIndexPath row]];//将移动行数据清除,此时移动至的行空缺
[data insertObject:moveRow atIndex:[destinationIndexPath row]];//将数据插入此空缺
}其他未做修改。

求指点。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: