3

在我的代码的一部分中,我调用

UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];

得到一些单元格,然后我将它们的 alphas 更改为 0.35。这行得通,细胞看起来褪色了。

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

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

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

它返回 .35

但是,滚动后单元格不褪色?看起来好像它的 alpha 在显示之前神奇地变回了 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;

}

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

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

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

4

3 回答 3

14

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

cell.contentView.alpha = {needed_value};

会为你工作。

于 2012-10-29T10:05:24.537 回答
2

此问题可能是由于在表格视图中重用了单元格。有一个更好的方法可以做到这一点:

您应该在视图控制器中创建一个 NSMutableDictionary 并为应该褪色的索引设置键。

然后,在 cellForRowAtIndexPath 中,当您设置单元格时,根据索引是否作为字典中的键存在来设置 alpha。

此解决方案将在没有任何奇怪行为的情况下工作。

于 2012-06-29T22:37:12.193 回答
1

确保您也设置了 alpha 值cell.contentView。还要设置 alpha,tableView:willDisplayCell:forRowAtIndexPath:因为它在 iOS 7 中可能不起作用

于 2015-12-18T11:00:04.883 回答