开发者

heightForRowAtIndexPath crashes

开发者 https://www.devze.com 2023-02-12 12:01 出处:网络
I have the following code: - (CGFloat)tableView:(UITableView *) tab开发者_运维问答leView heightForRowAtIndexPath:(NSIndexPath *) indexPath{

I have the following code:

- (CGFloat)tableView:(UITableView *) tab开发者_运维问答leView heightForRowAtIndexPath:(NSIndexPath *) indexPath{
    TestCell* cell = (ConvoreCell *) [tableView cellForRowAtIndexPath:indexPath];
    CGSize textSize = [[cell text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width - 20, 500)];
    return 80;
}

It crashes on the cellForRowAtIndexPath, why is this? The log doesn't tell me anything. Here's my cellForRowAtIndexPath method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"ConvoreCell";
    AsyncImageView *asyncImageView = nil;
    TestCell * cell = (TestCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TestCell" owner:nil options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (ConvoreCell *) currentObject;
                break;
            }
        }
        asyncImageView = [[[AsyncImageView alloc] init] autorelease];
        asyncImageView.imageview = cell.icon;
    }
    else {
        asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
    }

    NSMutableDictionary *cellValue = [results objectAtIndex:indexPath.row];

    NSString *picURL = [[cellValue objectForKey:@"user"] objectForKey:@"img"];
    if ([picURL isEqualToString:@"https://secure.gravatar.com/avatar/1f043010eb1652b3fab3678167dc0487/?default=https%3A%2F%2Fconvore.com%2Fmedia%2Fimages%2Feric.png&s=80"])
        picURL = @"https://test.com/media/images/eric.png";

    [asyncImageView loadImageFromURL:[NSURL URLWithString:picURL]];

    cell.title.text = [cellValue objectForKey:@"message"];
    cell.info.text = [NSString stringWithFormat:@"%@ %@ days ago", [[cellValue objectForKey:@"user"] objectForKey:@"username"], [cellValue objectForKey:@"date_created"] ];

    return cell;
}

All I want to do is to resize the height of the cell according to the cell.title.text....


It's wrong to call cellForRowAtIndexPath: manually. You have to store those strings separately in array and obtain them from there.

A rule of thumb: don't call framework callbacks manually.

0

精彩评论

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