2013-01-15 51 views
2

我是一个新手程序员。已经晚上试图解决问题,但没有奏效。我试图根据信息内容动态更改单元格的大小。做例子,但我的程序启动死亡。随着错误:由于未捕获异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSDictionaryI sizeWithFont:constrainedToSize:lineBreakMode:]

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

回答

5

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

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

NSString *cellText = [[news objectAtIndex:indexPath.row] objectForKey:@"title"]; 
相关问题