2013-01-15 66 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"]; 
相關問題