iOS 8和9(感谢此帖子)
注意:如果您正在使用现有的iOS 7项目,则需要将目标更新到iOS 8才能获得此功能。还要记得设置UITableviewDelegate。
所有魔法现在都发生在这里(想要多少按钮都可以!!!):
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button 1");
}];
button.backgroundColor = [UIColor greenColor];
UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button2!");
}];
button2.backgroundColor = [UIColor blueColor];
return @[button, button2];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
(iOS 7)
**activate the delete button on swipe**
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"You hit the delete button.");
}
设置自定义文本标签而不是删除。
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Your Label";
}
设置自定义颜色的按钮部分1-警告,这在技术上涉及到篡改私有的苹果 API。但是,您可以使用 UIKIT 的公共方法搜索修改子视图。
创建一个uitableviewcell类(也可以参考https://dev59.com/LWfWa4cB1Zd3GeqPjrUq#22350817)
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
for(UIView *subview2 in subview.subviews){
if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
((UIView*)[subview2.subviews firstObject]).backgroundColor=[UIColor blueColor];
}
}
}
}
另外需要注意的是,并不能保证这种方法在未来的更新中仍能正常使用。同时请注意,提及或使用私有的UITableViewCellDeleteConfirmationView
类可能会导致AppStore拒绝。
为按钮设置自定义颜色-第二部分
回到你的UITableViewController
中
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
[YourTableView reloadData];
}
在下一次调用tablecell的layoutSubviews之前,备用颜色不会被调用,因此我们通过重新加载所有内容来确保这一点。