我已经为这个问题挣扎了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];
发布于 2012-01-30 01:34:57
在http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW19我自己找到了答案
显然,-tableView:cellForRowAtIndexPath:在调用-setEditing:animated:时不会自动调用
因此,为了让一个新的单元格“出现”,我在调用setEditing之后将alpha设置为0.0,然后设置为1.0 .
https://stackoverflow.com/questions/9058388
复制相似问题