Ios UITableViewCell,显示无滑动的滑动样式删除按钮
我搜索了几篇文章,但没有找到我要找的。基本上,我想在每一行上显示“删除”按钮,但我不想使用UITableView.editing属性。 因为它看起来像这样 将有“编辑”按钮。当用户点击它时,删除按钮看起来像是刷卡式的 是否有机会显示这样的删除按钮 也许有办法解决这个问题。否则,我将为此创建自定义视图 谢谢你的建议Ios UITableViewCell,显示无滑动的滑动样式删除按钮,ios,objective-c,uitableview,cocoa-touch,delete-row,Ios,Objective C,Uitableview,Cocoa Touch,Delete Row,我搜索了几篇文章,但没有找到我要找的。基本上,我想在每一行上显示“删除”按钮,但我不想使用UITableView.editing属性。 因为它看起来像这样 将有“编辑”按钮。当用户点击它时,删除按钮看起来像是刷卡式的 是否有机会显示这样的删除按钮 也许有办法解决这个问题。否则,我将为此创建自定义视图 谢谢你的建议 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Do something...
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- 添加布尔属性:
@property BOOL didPressEdit代码>
- 将
ui按钮添加到
UITableViewCell
- 按编辑时,
变为didPressEdit
并TRUE
重新加载,从而UITableView
cell.deleteButton.hidden=!编辑代码>使所有删除按钮可用
- 按delete键时,从数据源数组中删除对象,重新加载tableview
希望这有帮助您可以使用UITableView委托方法来请求这些操作。按如下方式实施此方法:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *modifyAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// Respond to the action.
}];
modifyAction.backgroundColor = [UIColor blueColor];
return @[modifyAction];
}
当然,您可以返回多个操作并自定义文本和背景颜色
要使行可编辑,还需要实现此方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[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.
}
}
您需要在单击edit按钮时调用EditActionsErrorWatIndeXPath方法
-(void)buttonTouched:(id)sender{
UIButton *btn = (UIButton *)sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0];
[self tableView:self.tableView editActionsForRowAtIndexPath:indexPath];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
你想在tableview中删除像image2一样的按钮吗?@bhavinramani是的,我想删除像image2一样的按钮。将有“编辑”按钮,当您单击它时,所有行都应该看起来像image2。自定义tableview单元格是您唯一的选择,因为单元格末尾的删除按钮不会一次对tableview中的所有单元格都可见(如您在屏幕截图中所述),我不确定您是否理解这个问题。OP特别需要使“删除”按钮无需滑动即可使用,并且只需点击“编辑”按钮即可使用tableview中的所有行。我看不到你提供的答案能实现这些目标。
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *moreAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
[self.heartCartTabel setEditing:NO];
[self editButtonClicked:indexPath.row];
}];
moreAction2.backgroundColor = [UIColor blueColor];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
[self tableView:self.heartCartTabel commitEditingStyle: UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath];
// [self.heartCartTabel deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
return @[deleteAction, moreAction2];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (IBAction)editButtonClicked:(int)indexNumber {
}