开发者

UITableViewCell not appearing/reusing correctly from NIB

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 *)i开发者_JS百科ndexPath
{   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


Remove the else clause


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜