UINavigationBar 手动实现编辑和完成按钮

来源:互联网 发布:c语言打印字母图形 编辑:程序博客网 时间:2024/05/01 07:10

editButton = [[UIBarButtonItem alloc] initWithTitle:@”编辑” style:UIBarButtonItemStyleBordered target:self action:@selector(editAction)];

self.navigationItem.rightBarButtonItem = editButton;

}

-(void)ToReturn

{

[self.navigationController popViewControllerAnimated:YES];

}

- (void)editAction{

if (editButton.title == @”编辑”) {

[editButton setTitle:@"确定"];

[editButton setStyle:UIBarButtonItemStyleDone];

[self.roadTable setEditing:YES animated:YES];

}

else {

[editButton setTitle:@"编辑"];

[editButton setStyle:UIBarButtonItemStylePlain];

[self.roadTable setEditing:NO animated:YES];

}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [self.titleArray count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @”Cell”;

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];

return cell;

}

//删除一行

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete)

{

//删除数组中的一条记录

[roadArray removeObjectAtIndex:indexPath.row -1];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

else if (editingStyle == UITableViewCellEditingStyleInsert)

{

// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.

}

}