Câu trả lời:
Đây là giải pháp hoàn chỉnh của tôi, không có thụt lề (0left align) của ô!
- (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;
}
Swift 3 tương đương với câu trả lời được chấp nhận chỉ với các chức năng cần thiết:
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
Tôi gặp phải sự cố tương tự trong đó tôi muốn các hộp kiểm tùy chỉnh xuất hiện trong chế độ Chỉnh sửa nhưng không phải nút xóa '(-)'.
Câu trả lời của Stefan đã hướng tôi đi đúng hướng.
Tôi đã tạo một nút bật tắt và thêm nó dưới dạng một editAccessoryView vào Ô và kết nối nó với một phương thức.
- (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;
}
Đã triển khai các phương thức ủy quyền này
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
Khi bạn chỉ muốn ẩn dấu chấm (-) trong khi chỉnh sửa nhưng bạn có thể muốn giữ chức năng xóa cho những người dùng mà bạn triển khai nó như vậy trong UITableViewDelegate
lớp tuân thủ giao thức của bạn
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.editing) return UITableViewCellEditingStyleNone;
return UITableViewCellEditingStyleDelete;
}