我正在尝试向表格 View 中的一行添加一个 Action (通过滑动)。由于缺陷,我可以使用此方法删除:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
但我不知道如何进行自定义操作。我读到他们在 iOS 11 中引入了它。如果我也需要它用于以前的版本怎么办?
这是 iOS 11 的方法:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
如您所见,我使用的是 iOS 11。所以,为了清楚起见,我希望用户滑动一行以显示一个 Action (具有不同颜色的背景和文本)。我不关心它出现的形式(尾随或前导)。
最佳答案
So, to make it clear, I want the user to swipe a row to show an action (with different color background and a text). I don't care form where it appears (trailing or leading).
这样做
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" Delete " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// your delete mechanism here
}];
deleteAction.backgroundColor = [UIColor redColor];
return @[deleteAction];
}
关于ios - iOS 11 之前的行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48784515/