ios 有什么办法可以在编辑 UITableView 时隐藏“-”(删除)按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3020922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any way to hide "-" (Delete) button while editing UITableView
提问by iPhoneDev
On my iphone app, I have a UITableView in edit mode, where user is allowed only to reorder the rows no delete permission is given.
在我的 iphone 应用程序上,我有一个处于编辑模式的 UITableView,在该模式下,用户只能重新排序行,没有删除权限。
So is there any way where I can hide "-" red button from TableView. Please let me know.
那么有什么方法可以隐藏TableView中的“-”红色按钮。请告诉我。
Thanks
谢谢
回答by Stefan von Chossy
Here is my complete solution, without indentation (0left align) of the cell!
这是我的完整解决方案,没有单元格的缩进(0left align)!
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
回答by antoine
Swift 3equivalent to accepted answer with just the needed funcs:
Swift 3等价于接受的答案,只需要所需的函数:
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
回答by sethtc
This stops indentation:
这将停止缩进:
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
回答by srik
I faced a similar problem where I wanted custom checkboxes to appear in Edit mode but not the '(-)' delete button.
我遇到了类似的问题,我希望自定义复选框出现在编辑模式下,而不是“(-)”删除按钮。
Stefan's answersteered me in the correct direction.
Stefan 的回答将我引向了正确的方向。
I created a toggle button and added it as an editingAccessoryView to the Cell and wired it to a method.
我创建了一个切换按钮并将其作为editingAccessoryView 添加到Cell 并将其连接到一个方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
// Configure the cell...
UIButton *checkBoxButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 32.0f)];
[checkBoxButton setTitle:@"O" forState:UIControlStateNormal];
[checkBoxButton setTitle:@"√" forState:UIControlStateSelected];
[checkBoxButton addTarget:self action:@selector(checkBoxButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
cell.editingAccessoryView = checkBoxButton;
return cell;
}
- (void)checkBoxButtonPressed:(UIButton *)sender {
sender.selected = !sender.selected;
}
Implemented these delegate methods
实现了这些委托方法
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
回答by Ol Sen
When you only want to hide the (-) dot while editing but you may want to keep the deleting functionality for the users you implement it like so in your UITableViewDelegate
protocol conforming class
当您只想在编辑时隐藏 (-) 点,但您可能希望在UITableViewDelegate
符合协议的类中为实现它的用户保留删除功能
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.editing) return UITableViewCellEditingStyleNone;
return UITableViewCellEditingStyleDelete;
}