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