我有一个Tableview,其中包含许多单元格。我实现了以下代码以允许用户编辑表。
如何在用户启动编辑时使单元格淡出?
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
selectedCell.alpha = 0.1;
if ( editingStyle == UITableViewCellEditingStyleDelete)
{[XC1 removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
我尝试使细胞淡入时添加的当前代码不起作用!
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
selectedCell.alpha = 0.1;
我希望有人可以帮助我!
答案 0 :(得分:0)
首先,我不确定您是否可以像这样设置单元格的alpha。请记住,单元格是循环使用的,并不“属于”一个特定的索引路径。也许你应该首先在编辑模式之外进行一些实验。
其次,在调用commitEditingStyle
之前更改deleteRowsAtIndexPaths:
方法中的单元格几乎肯定无能为力。只有在确认编辑模式下对表格视图的更改时才会调用此方法,而不仅仅是在选择单元格时。
另请注意,设置视图的alpha不会为此更改设置动画。您可以在调用deleteRowsAtIndexPaths
之前尝试添加动画。
[UIView animateWithDuration:0.25 animations:^{
cell.alpha = 0.0;
} completion:^(BOOL finished) {
// remove the cell
}];
也许您想要将效果链接到选择单元格。最好通过覆盖setSelected:animated:
来完成。