编辑和删除按钮UITableView [英] Edit and Delete Button UITableView
问题描述
我可以将UITableView置于编辑模式并显示删除按钮。我如何在删除按钮旁边添加一个蓝色的编辑按钮?
I can put a UITableView into edit mode and show the delete button. How would I add a blue "Edit" button beside the delete button?
就像在ios6中向左滑动邮件一样,除了邮件应用程序显示更多之外,我想要一个编辑按钮。
Just like swiping left in the mail in ios6, except the mail app shows "More", I want a "Edit" button.
推荐答案
这不是Apple的 UITableViewCell
的标准功能。 -您将需要使用自己的滑动识别器来创建自己的 UITableViewCell
子类。
This isn't a feature that comes standard with Apple's UITableViewCell
- you're going to need to make your own subclass of UITableViewCell
with your own swipe recognizer.
此GitHub项目是一个不错的开始-使用它,您应该可以使用以下代码:
This GitHub project is a great start - using it, you should be able to use this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
title:@"More"];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
title:@"Delete"];
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier
containingTableView:_tableView // For row height and selection
leftUtilityButtons:nil
rightUtilityButtons:rightUtilityButtons];
cell.delegate = self;
}
...
return cell;
然后为该单元实现委托方法:
You then implement the delegate method for the cell:
- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
switch (index) {
case 0:
NSLog(@"More button was pressed");
break;
case 1:
{
// Delete button was pressed
NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
[_testArray removeObjectAtIndex:cellIndexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[cellIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
default:
break;
}
}
这篇关于编辑和删除按钮UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!