ViewWillAppear期间静态UITableViewCell更改未反映在显示中

编程入门 行业动态 更新时间:2024-10-09 12:34:43
本文介绍了ViewWillAppear期间静态UITableViewCell更改未反映在显示中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在故事板中使用一些静态 UITableViewCell 来显示一些设置信息。

I am using some static UITableViewCell's configured in the Storyboard to display some setting information.

如果关闭其他设置之一,则应禁用其他一些单元格。

Some of the other cells should be disabled if one of the other settings is toggled off.

按顺序在 viewWillAppear 期间将单元格设置为正确的状态我从NSUserDefaults读取设置,然后相应地更改单元格。

In order to put the cells into the proper state, during viewWillAppear I read the settings from NSUserDefaults and then change the cells accordingly.

- (void) viewWillAppear:(BOOL)animated if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OtherCellEnabled"]) { [self otherCell].alpha = 1.0; [self otherCell].userInteractionEnabled = YES; } else { NSLog(@"Changing alpha to 0.3"); [self otherCell].alpha = 0.3; [self otherCell].userInteractionEnabled = NO; }

问题在于,当我实际运行程序时,即使它在更改alpha的日志,alpha实际上不会更改。 userInteractionEnabled 似乎确实存在,但alpha保持为1.0。

The problem is that when I actually run the program, even though it says in the log that the alpha is changed, the alpha doesn't actually change. The userInteractionEnabled does seem to stick, but the alpha is left at 1.0.

这不是细胞重用的问题,或者单元格没有及时实例化,因为其他设置可以很好地改变。

It's not a problem of cell reuse, or cell's not being instantiated in time, because the other settings can be changed just fine.

将它从cell.alpha更改为cell.contentView.alpha有效,但这是一个不同的设置。

Changing it from cell.alpha to cell.contentView.alpha works, but that is a different setting.

似乎所有的设置都坚持,除了 alpha 设置,它以某种方式被覆盖。

It seems like all of the settings "stick" except for the alpha setting, which somehow is getting overwritten.

推荐答案

我正在回答我自己的问题,因为我能够解决它。

I am answering my own question because I was able to solve it.

首先,我尝试将alpha更改放在 cellForRowAtIndexPath 中,但这也不起作用。经过大量的修补,我得出的结论是 UITableViewCell 的alpha设置在某种程度上是特别的,因为它会被覆盖或设置为1.0。

First, I tried putting the alpha change in cellForRowAtIndexPath, but that didn't work either. After a lot of tinkering, I've come to the conclusion that UITableViewCell's alpha setting is somehow special in that it keeps getting overwritten or set to 1.0.

我发现了两个修正:

第一次,而不是在 cellForRowAtIndexPath ,在 UITableViewDelegate 方法 willDisplayCell 中执行。无论出于何种原因,在这种方法中更改单元格的alpha实际上会坚持下去。当然,如果你这样做,你必须重新安排你的逻辑,以便在逐个单元格的基础上完成更改,即:

First, instead of doing the change in cellForRowAtIndexPath, do it in the UITableViewDelegate method willDisplayCell. For whatever reason, changing the cell's alpha in this method will actually stick. Of course, if you do it this way you have to re-arrange your logic so that the changes are done on a cell-by-cell basis, i.e.:

- (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 中。其他人似乎也不确定:

As I said, I'm not sure exactly why this works in willDisplayCell but not in cellForRowAtIndexPath. Others seem uncertain also:

什么是 - [UITableViewDelegate willDisplayCell:forRowAtIndexPath:] for?

带有alpha颜色的UITableView背景导致UITableViewCell出现问题

其他解决方案是,而不是使用有问题的alpha,使用另一个将实现相同效果的设置。在我的例子中,那是 contentView.alpha 和 backgroundColor 。无论出于何种原因,这些设置都会坚持,您甚至可以在 viewWillAppear 中设置它们,它将按预期工作:

The other solution is to, instead of using the problematic alpha, use another setting which will achieve the same effect. In my case, that was the contentView.alpha and the backgroundColor. For whatever reason, these settings will stick, and you can even set them in viewWillAppear and it will work as expected:

- (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; } }

第二种方法的缺点是现在你是覆盖故事板的单元格颜色设置,但是你可以通过询问故事板的颜色来解决这个问题。

The disadvantage to the second approach is that now you are overwriting the Storyboard's cell color settings, but you could work around that by asking the storyboard for the color if you care about that.

我不知道为什么 cell.alpha 的处理方式不同。也许关于静态单元的实现方式。

I'm not sure why cell.alpha is treated differently. Maybe something about the way static cells are implemented.

更多推荐

ViewWillAppear期间静态UITableViewCell更改未反映在显示中

本文发布于:2023-11-01 11:04:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   ViewWillAppear   UITableViewCell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!