Randall Schmidt
Randall Schmidt

Reputation: 93

Cannot set cell alpha in cellForRowAtIndexPath

In one part of my code, i call

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

to get a number of cells, and then I change their alphas to .35. This works, and the cells look faded.

However, when I scroll the tableview, the cells that move out of the window and then move back are no longer are faded. This happens even though in cellForRowAtIndexPath the alpha for each disabled row is set to .35.

Right before returning the cells in cellForRowAtIndexPath I log

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

And it returns .35

However, the cell is not faded after scrolling? It appears as if it's alpha magically gets changed back to 1 before being displayed.

Following is my implementation of cellForRowAtIndexPath in my Section class

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

}

And the view controller that coordinates these sections gets cells from them like so:

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

(and the NSLog is logging appropriate alpha values)

Upvotes: 3

Views: 4020

Answers (3)

Ali Amin
Ali Amin

Reputation: 687

Make sure you are setting alpha value for cell.contentView too. Also set alpha in tableView:willDisplayCell:forRowAtIndexPath: as it might not work in iOS 7

Upvotes: 1

Max Ivashkevich
Max Ivashkevich

Reputation: 366

Adding all subviews to cell's contentView and use the following string in -cellForRowAtIndexPath:

cell.contentView.alpha = {needed_value};

will works for you.

Upvotes: 15

Dima
Dima

Reputation: 23634

This issue is probably due to cells being reused in the table view. There is a better way to do this:

You should create an NSMutableDictionary in your view controller and just set keys for the indexes that should be faded.

Then, in cellForRowAtIndexPath, while you are setting up the cell, set the alpha based on whether or not the index exists as a key in the dictionary.

This solution will work without any weird behavior.

Upvotes: 2

Related Questions