Nerrolken
Nerrolken

Reputation: 2005

UITableViewCell not showing subview UITextView

I've combed through dozens of questions/answers on Stack Overflow and other sites about this, but I can't seem to get it working. It looks like a fairly common problem, but none of the answers from the other questions I found have applied. Basically, I'm trying to add a simple UITextView to a table cell, and it isn't showing up. Here's what I've got:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    UITextView *textviewFactoid = nil;

    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:@"cell"];

        textviewFactoid = [[UITextView alloc]initWithFrame:CGRectMake(0, -3, 300, 25)];
        [textviewFactoid setFont:[UIFont systemFontOfSize:12.0f]];
        [textviewFactoid setBackgroundColor:[UIColor blueColor]];
        [textviewFactoid setTextColor:[UIColor grayColor]];
        [textviewFactoid setScrollEnabled:NO];
        [textviewFactoid setEditable:NO];
        [textviewFactoid setTag:98];
        [[cell contentView] addSubview:textviewFactoid];
    } else {
        textviewFactoid = (UITextView*)[cell.contentView viewWithTag:98];
    }

    NSString *textviewFactoidText = @"Here is the factoid";
    [textviewFactoid setText:textviewFactoidText];

    return cell;
}

Does anyone know why the cell would be blank? I set the backgroundColor to blue so I could see if the textview exists at all, and it doesn't seem to. I'm not getting any errors or warnings, either, to help me pinpoint what's wrong.

I've copied this code almost verbatim from another app I wrote, and it works fine over there... I'm sure it's something simple that I'm missing or that I changed, but I'm kind of stumped.

Anyone have a sharper eye than I do?

Upvotes: 0

Views: 784

Answers (1)

Alex
Alex

Reputation: 1625

If you are building for iOS 6 and have registed a nib or class for your cell identifier dequeueReusableCellWithIdentifier will always return a cell. Your if (cell == nil) { will never evaluate as true and run the code within that block.

Upvotes: 2

Related Questions