FirstApp,iphone开发学习总结11,表操作(移动、删除)
nav添加左按钮:
- (void)viewDidLoad
{
//...
UIBarButtonItem *tableEditBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(toggleEdit:)];
[[self navigationItem] setLeftBarButtonItem:tableEditBtn];
[tableEditBtn release];
}
{
//...
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];//设置编辑状态
}
}
{
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];
}
}
{
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]];//将数据插入此空缺
}
{
NSArray *moveRow = [data objectAtIndex:[sourceIndexPath row]];//保存移动的行
[data removeObjectAtIndex:[sourceIndexPath row]];//将移动行数据清除,此时移动至的行空缺
[data insertObject:moveRow atIndex:[destinationIndexPath row]];//将数据插入此空缺
}
其他未做修改。
求指点。