【发布时间】:2011-03-02 13:05:10
【问题描述】:
在我的 iphone 应用程序中,我有一个处于编辑模式的 UITableView,其中只允许用户对行重新排序,而没有授予删除权限。
那么有什么方法可以隐藏 TableView 中的“-”红色按钮。请告诉我。
谢谢
【问题讨论】:
标签: ios iphone uitableview cocoa
在我的 iphone 应用程序中,我有一个处于编辑模式的 UITableView,其中只允许用户对行重新排序,而没有授予删除权限。
那么有什么方法可以隐藏 TableView 中的“-”红色按钮。请告诉我。
谢谢
【问题讨论】:
标签: ios iphone uitableview cocoa
当您只想在编辑时隐藏 (-) 点,但您可能希望为您在 UITableViewDelegate 协议符合类中实现它的用户保留删除功能
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.editing) return UITableViewCellEditingStyleNone;
return UITableViewCellEditingStyleDelete;
}
【讨论】:
Swift 3 等同于仅具有所需功能的已接受答案:
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
【讨论】:
我遇到了类似的问题,我希望自定义复选框出现在编辑模式中,而不是“(-)”删除按钮。
Stefan's answer 引导我朝着正确的方向前进。
我创建了一个切换按钮并将其作为editingAccessoryView 添加到单元格并将其连接到一个方法。
- (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;
}
实现了这些委托方法
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
【讨论】:
这会停止缩进:
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
【讨论】:
这是我的完整解决方案,没有单元格的缩进(0左对齐)!
- (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;
}
【讨论】: