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