我是新手程序员。已经晚上试图解决问题,但没有奏效。我正在尝试根据信息内容动态更改单元格的大小。举个例子,但我的程序启动死了。出现错误:
'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;
}
答案 0 :(得分:5)
您的news
数组似乎包含字典,而不是字符串。因此,tableView:heightForRowAtIndexPath:
NSString* cellText
NSDictionary*
实际上是objectForKey:
。
您只需在objectAtIndex:
之后添加NSString *cellText = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
电话:
{{1}}