1

I'm using a table for showing a book catalog to the user. Some books are free others are premium books. I want to inform the user, based on their account status (free or premium), the books that they can download. Still, the user can check all books (cover and summary but not download).

For that purpose, I'm trying to dim cells that contain premium books for free users.

How can I dim cells? I have already tried previous answers like How do I make a UITableViewCell appear disabled? and UITableview: How to Disable Selection for Some Rows but Not Others but no luck. Why is alpha not working?

My tries have gone in this direction with no result:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"SelectBookCell";

    SelectBookCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[SelectBookCell alloc] init];
    }

    // Config images, title, author... information to show
    Do stuff

    // Dimmed premium books
    if([[userAccount level] intValue] <= (NSInteger)[book objectForKey:@"level"])
    {
        [cell setAlpha: 0.2f];

        cell.textLabel.alpha = 0.2f;
        cell.detailTextLabel.alpha = 0.2f;
    }
    return cell;
}
Community
  • 1
  • 1
kitimenpolku
  • 2,604
  • 4
  • 36
  • 51

2 Answers2

3

EDIT: Try this first!

cell.contentView.alpha = 0.2

instead of

cell.alpha = 0.2

I tested on my project, and the only latter one works fine.

If it does not working, then try the below too.

I think this lines has problem

if (cell == nil) {
    cell = [[SelectBookCell alloc] init];
}

Change it to

if (cell == nil) {
    cell = [[SelectBookCell alloc] initWithNibName:@"SelectBookCell" bundle:nil]
}

Note that the xib filename @"SelectBookCell" may be different for your project. I don't know your cell is in a storyboard or xib file.

Wonjung Kim
  • 1,873
  • 15
  • 18
1

Please look/read about Type Casting in objective C.
Difference between casting NSNumber(class) and NSInteger(primitive type).

here an sample:

NSLog(@"Wrong#1 : %@, Value: %d", (1 == ((int)@"1")) ? @"YES" : @"NO", ((int)@"1"));

NSLog(@"Wrong#2 : %@, Value: %d", (1 == ((NSInteger)@"1")) ? @"YES" : @"NO", ((NSInteger)@"1"));

NSLog(@"Wrong#3 : %@, Value: %d", (1 == ((NSInteger)@1)) ? @"YES" : @"NO", ((NSInteger)@1));

NSLog(@"Wrong#4 : %@, Value: %d", (1 == ((int)@1)) ? @"YES" : @"NO", ((int)@1));

NSLog(@"Correct#1 : %@, Value: %d", (1 == [@"1" intValue]) ? @"YES" : @"NO", [@"1" intValue]);

NSLog(@"Correct#2 : %@, Value: %d", (1 == [@"1" integerValue]) ? @"YES" : @"NO", [@"1" integerValue]);

About your problem:

Since you are using the default views .textLabel & .detailTextLabel i'm sure you must use cell.contentView.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"SelectBookCell";

    SelectBookCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) 
    {
        // this is wrong
        // cell = [[SelectBookCell alloc] init];
        // --
        // must be
        cell = [[SelectBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    ...

    // this statement looks correct but if you check it, it's not
    // try logging this:
    NSLog(@"Wrong: %@", (1 < (NSInteger)@"1") ? @"YES" : @"NO");
    // this will return `YES` and that is wrong

    // if you try this:
    NSLog(@"Correct: %@", (1 < [@"1" intValue]) ? @"YES" : @"NO");
    // it will log the correct one

    // your condition 
    // `if([[userAccount level] intValue] <= (NSInteger)[book objectForKey:@"level"])` 
    // was never true
    // try this is instead:
    if([[userAccount level] intValue] <= [[book objectForKey:@"level"] intValue])
    {
        [cell setAlpha: 0.2f];
        // or 
        [cell.contentView setAlpha: 0.2f];
    }
    else
    {
        // dont for get this else statement to avoid unwanted behaviour
        // on cell reuse
        [cell setAlpha:1.0f];
        // or 
        [cell.contentView setAlpha:1.0f];
    }
    return cell;
}

Hope this is helpful... Cheers.

0yeoj
  • 4,500
  • 3
  • 23
  • 41