2

我是一个新手程序员。已经晚上试图解决问题,但没有奏效。我正在尝试根据信息内容动态更改单元格的大小。举个例子,但是我的程序启动失败了。出现错误:

 'Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[__NSDictionaryI sizeWithFont:constrainedToSize:lineBreakMode:]"     

我究竟做错了什么?这是我的代码:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
    }

    NSDictionary *newsItem = [news objectAtIndex:indexPath.row];

    cell.textLabel.text = [newsItem objectForKey:@"title"];

    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;

    cell.detailTextLabel.text = [newsItem objectForKey:@"pubDate"];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = [news objectAtIndex:indexPath.row];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
    CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);

    CGSize labelSize = [cellText sizeWithFont:cellFont
                            constrainedToSize:constraintSize
                                lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20.0f;
}
4

1 回答 1

5

看来您的news数组中有字典,而不是字符串。所以在tableView:heightForRowAtIndexPath:NSString* cellText的实际上是一个NSDictionary*.

你只需要像这样添加一个objectForKey:调用objectAtIndex:

NSString *cellText = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
于 2013-01-15T21:12:18.580 回答