views:

55

answers:

2

I have created a custom UITableViewCell from a NIB. Everything appears when the rowHeight is not set (albeit, everything is squished). I am not sure if I am reusing and creating the cells properly:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"ARCell";
    ARObject *myARObject;
    myARObject = [items objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"ARCell" owner:self options:nil];
        cell = arCell;
        self.arCell = nil;
    }

    UILabel *label;
    label = (UILabel *)[cell viewWithTag:0];
    label.text = [NSString stringWithFormat:@"%@", myARObject.username];

    label = (UILabel *)[cell viewWithTag:1];
    label.text = [NSString stringWithFormat:@"%@", myARObject.created_at];

    label = (UILabel *)[cell viewWithTag:2];
    label.text = [NSString stringWithFormat:@"%@", myARObject.articleTitle];

    label = (UILabel *)[cell viewWithTag:3];
    label.text = [NSString stringWithFormat:@"%@", myARObject.intro_text];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{   CGFloat rowHeight = 105;
    return rowHeight;
}

Problems:

  1. Not all labels appear when rowHeight is set. If rowHeight isn't set, labels are squished
  2. When rowHeight is not set, selecting an item shows all of the labels
A: 

Remove the else clause

dc
problem still exists
Sheehan Alam
A: 

You call loadNibNamed, but don't store result anywhere! Can't understand why your code works at all... Anyway, writing blind I would try something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"ARCell";
    ARObject *myARObject = [items objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ARCell" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
   }

    UILabel *label;
    label = (UILabel *)[cell viewWithTag:0];
    label.text = [NSString stringWithFormat:@"%@", myARObject.username];

    label = (UILabel *)[cell viewWithTag:1];
    label.text = [NSString stringWithFormat:@"%@", myARObject.created_at];

    label = (UILabel *)[cell viewWithTag:2];
    label.text = [NSString stringWithFormat:@"%@", myARObject.articleTitle];

    label = (UILabel *)[cell viewWithTag:3];
    label.text = [NSString stringWithFormat:@"%@", myARObject.intro_text];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 105.0;
}
JOM
i get the same result. instead of cell = (UITableViewCell *)[nib objectAtIndex:0]; I am assigning it to arCell which is a UITableViewCell property in my h file
Sheehan Alam
I am following the example in the Apple TableView Programming Guide: http://developer.apple.com/iphone/library/documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1
Sheehan Alam
Checked and the sample looks like your code. Then just check your arCell IBOutlet "because of the outlet connection you made, the tvCell outlet now has a reference to the cell loaded from the nib file."
JOM
all outlets connected
Sheehan Alam
Just to check: is the problem with cell reuse or size? You can verify reuse with NSLog(@"") calls, but resizing reused cells is something totally different. Your code uses hardcoded value 105, which I thought is correct value for all cells?
JOM