iOS 表格 View 自动换行重叠

标签 ios json uitableview word-wrap

我正在使用 JSON 解析为 TableView 。我正在使用标题和副标题。我不希望文本以“...”从屏幕上消失,所以我添加了

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

最佳答案

实现此 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;
}

关于iOS 表格 View 自动换行重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18467541/

相关文章:

ios - 滚动 UICollectionView 时动画 NSLayoutConstraint

java - GSON 已弃用。我们可以使用哪一个来序列化 Java 对象和 JSON 之间的反序列化?

JQuery Ajax 检索 JSON

ios - 当 segue 完成时,NSString 被删除

ios - UISwitch 双击

javascript - 使用 $ 和 { 时将 JSON 解析为 YAML 失败

ios - 按下 UITableView "cell"时在文档中加载音频

ios - 单击时 UITableViewCell 背景颜色变为白色

uitableview - UITableView在批量更新中删除,插入和移动顺序

ios - TableView 中的 Exc 错误访问 - iOS