我正在回答我自己的问题,因为我能够解决它。
首先,我尝试将 alpha 更改放入cellForRowAtIndexPath
,但这也不起作用。经过大量修改后,我得出的结论是UITableViewCell
的 alpha 设置在某种程度上很特殊,因为它不断被覆盖或设置为 1.0。
我发现了两个修复:
First,而不是进行更改cellForRowAtIndexPath
,在UITableViewDelegate
method willDisplayCell
。无论出于何种原因,在此方法中更改单元格的 alpha 值实际上都会持续下去。当然,如果你这样做,你必须重新安排你的逻辑,以便更改是在逐个单元格的基础上完成的,即:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell == [self otherCell]) {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OtherCellEnabled"]) {
cell.alpha = 1.0;
cell.userInteractionEnabled = YES;
}
else {
NSLog(@"Changing alpha to 0.3");
cell.alpha = 0.3;
cell.userInteractionEnabled = NO;
}
}
}
正如我所说,我不确定为什么这会起作用willDisplayCell
但不在cellForRowAtIndexPath
。其他人似乎也不确定:
-[UITableViewDelegate willDisplayCell:forRowAtIndexPath:] 是什么? https://stackoverflow.com/questions/1890265/what-is-uitableviewdelegate-willdisplaycellforrowatindexpath-for
UITableView 背景的 alpha 颜色导致 UITableViewCell 出现问题 https://stackoverflow.com/questions/3916348/uitableview-background-with-alpha-color-causing-problem-with-uitableviewcell
另一种解决方案就是不使用有问题的 alpha,而是使用另一个可以达到相同效果的设置。就我而言,那就是contentView.alpha
和backgroundColor
。无论出于何种原因,这些设置都会保留,您甚至可以将它们设置为viewWillAppear
它会按预期工作:
- (void) viewWillAppear:(BOOL)animated {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OtherCellEnabled"]) {
[self otherCell].backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
[self otherCell].contentView.alpha = 1.0;
[self otherCell].userInteractionEnabled = YES;
}
else {
NSLog(@"Changing alpha to 0.3");
[self otherCell].backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.3];
[self otherCell].contentView.alpha = 0.3;
[self otherCell].userInteractionEnabled = NO;
}
}
第二种方法的缺点是,现在您正在覆盖故事板的单元格颜色设置,但如果您关心的话,您可以通过向故事板询问颜色来解决这个问题。
我不知道为什么cell.alpha
被区别对待。也许与静态单元的实现方式有关。