0

我正在使用 JSON 解析为表格视图。我正在使用标题和副标题。我不希望文本以“...”离开屏幕,所以我添加了

cell.textLabel.numberOfLines = 0;
cell.detailTextLabel.numberOfLines = 0;

我可以看到它进入了一个新行,但单元格的高度保持不变并与其他单元格重叠,看起来非常糟糕。我怎样才能使它自动换行并且高度扩展其中的内容量?

先谢谢了。

回答第一个答案:我输入了 UITableViewDelegate 方法的代码,我得到了这个:

2013-08-27 15:29:40.940 jsonParse[2392:c07] -[__NSCFDictionary 

sizeWithFont:constrainedToSize:lineBreakMode:]: unrecognized selector sent to instance 0x7580130
2013-08-27 15:29:40.942 jsonParse[2392:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary sizeWithFont:constrainedToSize:lineBreakMode:]: unrecognized selector sent to instance 0x7580130'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x333a 0x1f9c4e 0x1fc224 0xc0952 0xc02dc 0x2927 0xbd6589 0xbd4652 0xbd589a 0xbd460d 0xbd4785 0xb21a68 0x4615911 0x4614bb3 0x4652cda 0x1c358fd 0x465335c 0x46532d5 0x453d250 0x1c16f3f 0x1c1696f 0x1c39734 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x1dad 0x1cd5 0x1)
libc++abi.dylib: terminate called throwing an exception
4

1 回答 1

1

实现此 UITableViewDelegate 方法以在运行时查找每行的高度并相应地更改代码

    NSString *object = [DataArray objectAtIndex:indexPath.row];
    CGSize size = [object sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] constrainedToSize:CGSizeMake(300, 9999) lineBreakMode:NSLineBreakByWordWrapping];//set the textLable Frame width according to your layout width.
    CGFloat height = size.height+20;//you can set the padding accordingly
    return height;
}

针对具体答案进行了编辑

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title = [[articles objectAtIndex:indexPath.row] objectForKey:@"title"];
    NSString *desc = [[articles objectAtIndex:indexPath.row] objectForKey:@"desc"];

    CGSize titleSize = [title sizeWithFont:[UIFont boldSystemFontOfSize:18.0] constrainedToSize:CGSizeMake(300, 99999) lineBreakMode:NSLineBreakByWordWrapping];
    CGSize descSize = [title sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(300, 99999) lineBreakMode:NSLineBreakByWordWrapping];
    return titleSize.height+descSize.height+10;
}
于 2013-08-27T14:23:42.773 回答