iOS 7中的自动调整表格视图单元格
我知道这个问题已经被问过很多次了,但我正在努力找到正确的iOS7实现 我有一个tableview,它由一个从服务器检索JSON数据的数组填充。这些数据可以是任意长度。现在我只能写一两行,没别的了 是否有人对rowatinexpath的正确iOS 7中的自动调整表格视图单元格,ios,objective-c,Ios,Objective C,我知道这个问题已经被问过很多次了,但我正在努力找到正确的iOS7实现 我有一个tableview,它由一个从服务器检索JSON数据的数组填充。这些数据可以是任意长度。现在我只能写一两行,没别的了 是否有人对rowatinexpath的正确高度有任何建议 谢谢 编辑(当前代码): 您只需要正确地实现heightforrowtaindexpath,如下所示 -(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSI
高度有任何建议
谢谢
编辑(当前代码):
您只需要正确地实现heightforrowtaindexpath
,如下所示
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
// Retrieve the line for this index (from JSON)
NSString* line = ...
// Measure it with UIKit
// font is a UIFont* and should be prepared once when the view initialized
// cellSize is a CGSize like (320, 10000), where 320 is the width of the cell, 10000 means we want it as high as needed
CGSize sz = [line sizeWithFont:font constrainedToSize:cellSize lineBreakMode: NSLineBreakByWordWrapping];
return sz.height;
}
对于单元格FORROWATINDEXPATH
,只需使标签适合整个单元格即可
有关NSString UIKit添加的更多信息,请参阅。请注意,iOS 7中不推荐使用方法[sizeWithFont:constrainedToSize:lineBreakMode:
,但我不确定如何使用它:)目前这是我的代码:-(CGFloat)tableView:(UITableView*)tableView高度为RowAtIndexPath:(NSIndexPath*)indexPath{//检索此索引的行(来自JSON)NSString*line=[myArray objectAtIndex:indexath.row];CGSize size=CGSizeZero;size=[line boundingRectWithSize:(CGSize){640,cgfloatmax}选项:NSStringDrawingUserLineFragmentOrigin属性:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}上下文:nil].size;返回ceilf(size.height);}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
// Retrieve the line for this index (from JSON)
NSString* line = ...
// Measure it with UIKit
// font is a UIFont* and should be prepared once when the view initialized
// cellSize is a CGSize like (320, 10000), where 320 is the width of the cell, 10000 means we want it as high as needed
CGSize sz = [line sizeWithFont:font constrainedToSize:cellSize lineBreakMode: NSLineBreakByWordWrapping];
return sz.height;
}