Ale*_*der 2 cell uitableview ios
我是新手程序员.已经晚上试图解决问题,但没有奏效.我正在尝试根据信息内容动态更改单元格的大小.举个例子,但我的程序启动死了.有错误:
'Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSDictionaryI sizeWithFont:constrainedToSize:lineBreakMode:]"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?这是我的代码:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
看来您的news数组中包含字典,而不是字符串.所以在tableView:heightForRowAtIndexPath:你NSString* cellText的实际上是一个NSDictionary*.
您只需要像这样添加一个objectForKey:电话objectAtIndex::
NSString *cellText = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2678 次 |
| 最近记录: |