无法在cellForRowAtIndexPath中设置单元格alpha

Ran*_*idt 3 xcode uitableview ios

在我的代码的一部分中,我打电话

UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

得到一些单元格,然后我将它们的alpha变为.35.这有效,细胞看起来褪色.

但是,当我滚动桌面视图时,移出窗口然后向后移动的单元格不再褪色.即使在cellForRowAtIndexPath中,每个禁用行的alpha设置为.35也会发生这种情况.

在返回cellForRowAtIndexPath中的单元格之前,我记录

NSLog(@"%f", cell.alpha);
Run Code Online (Sandbox Code Playgroud)

它返回.35

但是,滚动后单元格不会褪色?在显示之前,它似乎神奇地变为1.

以下是我在Section类中实现的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;

if ([self disabled]) 
{
    cell = [SectionedTableViewController tableView:tableView dequeueReusableCellWithIdentifier:@"CellDisabled" style:UITableViewCellStyleDefault];
    cell.alpha = .35;
}
else
{
    cell = [SectionedTableViewController tableView:tableView dequeueReusableCellWithIdentifier:@"CellEnabled" style:UITableViewCellStyleDefault];
    cell.alpha = 1;
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

//if-else block that only sets labels is here omitted

if (cell.textLabel.text == [[cs objectAtIndex:[self c]] type])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

NSLog(@"%f", cell.alpha);

return cell;
Run Code Online (Sandbox Code Playgroud)

}

并且协调这些部分的视图控制器从它们获取单元格,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Alpha: %f", [[[self sections] objectAtIndex:indexPath.section] tableView:tableView cellForRowAtIndexPath:indexPath].alpha);
    return [[[self sections] objectAtIndex:indexPath.section] tableView:tableView cellForRowAtIndexPath:indexPath];
}
Run Code Online (Sandbox Code Playgroud)

(并且NSLog正在记录适当的alpha值)

Max*_*ich 13

将所有子视图添加到单元格的contentView并使用以下字符串-cellForRowAtIndexPath:

cell.contentView.alpha = {needed_value};

会对你有用.

  • 非常重要的细节,请确保将所有子视图添加到contentView的子视图,而不是单元格.我花了很多时间试图找出为什么我的子视图在单元格没有改变的时候. (3认同)