我已经在这个问题上苦苦挣扎了2个小时了,我无法让它发挥作用。
我有一个带有表视图的视图控制器,其中包含一个项目列表。当用户触摸导航栏中的“编辑”按钮时,表格视图应进入“编辑模式”,并且项目列表一个新单元格应该出现,只有在处于“编辑模式”时才可见”。
因此,在我的-tableView:cellForRowAtIndexPath:
方法中,我正在检查if (tableView.isEditing)
以设置单元格。我尝试覆盖-setEditing:animated:
并进行[tableView reloadData]
来电,但我无法让它发挥作用。
这就是我-setEditing:animated:
的样子:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.groupDetailsTableView setEditing:editing animated:animated];
}
我的-tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (tableView.isEditing) {
// Other tableview code has been omitted
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
// Set the background view of the cell to be transparent
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setFrame:[cell.contentView frame]];
[deleteButton setFrame:CGRectMake(0, 0, cell.bounds.size.width-20, 44)];
[deleteButton setBackgroundImage:[UIImage imageNamed:@"redbutton.png"] forState:UIControlStateNormal];
[deleteButton setTitle:@"Delete Group" forState:UIControlStateNormal];
[deleteButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[deleteButton.titleLabel setShadowColor:[UIColor lightGrayColor]];
[deleteButton.titleLabel setShadowOffset:CGSizeMake(0, -1)];
[deleteButton addTarget:self action:@selector(deleteGroup) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteButton];
return cell;
}
}
当用户触摸“编辑”按钮时,我正在从方法中调用[self setEditing:YES animated:YES];
。我也尝试过调用[super setEditing:YES animated:YES];
和[self.groupDetailsTableView setEditing:YES animated:YES];
答案 0 :(得分:0)
显然-tableView:cellForRowAtIndexPath:
在调用-setEditing:animated:
所以要显示一个新单元格我将alpha设置为0.0,然后在调用setEditing后将其设置为1.0 ...