我在UITableView中使用自动布局和大小类,单元格根据其内容自行调整大小.为此,我使用的方法对于每种类型的单元格,您保留该单元格的屏幕外实例并使用systemLayoutSizeFittingSize它来确定正确的行高 - 此方法在此StackOverflow帖子和其他地方进行了很好的解释.
这很有效,直到我开始使用大小类.具体来说,我在常规宽度布局中为文本的边距约束定义了不同的常量,因此iPad上的文本周围有更多的空白.这给了我以下结果.

似乎新的约束集合得到了尊重(存在更多的空白),但行高度计算仍然返回与不应用特定于大小类的约束的单元格相同的值.屏幕外单元格中的部分布局过程不考虑窗口的大小等级.
现在我想,这可能是因为离屏幕视图有没有上海华盈或窗口,因此它不具有任何尺寸类性状是指在点systemLayoutSizeFittingSize调用时(即使它似乎使用的调整限制边距).我现在通过在创建UIWindow后添加屏幕外的大小调整单元作为子视图来解决这个问题,从而得到所需的结果:

这是我在代码中所做的事情:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let contentItem = content[indexPath.item]
if let contentType = contentItem["type"] {
// Get or create the cached layout cell for this cell type.
if layoutCellCache.indexForKey(contentType) == nil {
if let cellIdentifier = CellIdentifiers[contentType] {
if var cachedLayoutCell = dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell {
UIApplication.sharedApplication().keyWindow?.addSubview(cachedLayoutCell)
cachedLayoutCell.hidden = true
layoutCellCache[contentType] = cachedLayoutCell
}
}
}
if …Run Code Online (Sandbox Code Playgroud) 我有一个UITableview,懒惰加载所有不同大小的图像.加载图像时,我需要更新特定的单元格,所以我想出了我需要使用reloadRowsAtIndexPaths.但是当我使用这个方法时,它仍然为每个单元格调用heightForRowAtIndexPath方法.我认为reloadRowsAtIndexPaths的全部目的是它只会为你指定的特定行调用heightForRowAtIndexPath?
知道为什么吗?
[self.messageTableView beginUpdates];
[self.messageTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.messageTableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个UITableView,它由JSON文件中的文本和图像填充.TableView单元格当前正在为文本中没有包含很多换行符的"帖子"正确调整大小,但我无法通过4或5个换行符来计算"帖子"的正确高度.
获得身高的代码:
-(float)height :(NSMutableAttributedString*)string
{
NSString *stringToSize = [NSString stringWithFormat:@"%@", string];
CGSize constraint = CGSizeMake(LABEL_WIDTH - (LABEL_MARGIN *2), 2000.f);
CGSize size = [stringToSize sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:contraint lineBreakMode:NSLineBreakByWordWrapping];
return size.height;
}
Run Code Online (Sandbox Code Playgroud)
如何在允许换行和空白的同时计算正确的尺寸?
其余的方法,
在TableView CellForRow中:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *row = [NSString stringWithFormat:@"%i", indexPath.row];
float postTextHeight = [self height:postText];
NSString *height = [NSString stringWithFormat:@"%f", heightOfPostText + 70];
[_cellSizes setObject:height forKey:row];
}
Run Code Online (Sandbox Code Playgroud)
和表格单元格的高度:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *imageHeightString = [NSString stringWithFormat:@"%@", [_cellSizes objectForKey:indexPath.row]];
float heightOfCell = [imageHeightString …Run Code Online (Sandbox Code Playgroud) 我有一个带有三个不同原型单元的UICollectionView,每个原型单元通过Storyboard设置不同的高度.在运行时,Collection View使用自己的单元格大小,忽略我的Storyboard.
我目前正在使用collectionView:layout:sizeForItemAtIndexPath:使用几个条件来直接设置每个CGSize.
有没有更好的方法来设置单元格大小?我似乎无法检索每个单元格的Storyboard大小,而CGSizeMake似乎太硬编码而且不够灵活.
在SO和其他地方有很多关于如何根据子视图的大小计算UITableView行的必要高度的好信息.典型的例子和我在这里应用的例子是一个包含带有可变长度文本的标签的单元格.
通常的建议是使用sizeWithFont:constrainedToSize:lineBreakMode:tableview中的heightForRowAtIndexPath:.但是我见过的大多数例子都是根据标签的已知宽度使用"魔术"数字.
CGSize constraint = CGSizeMake(224.0, MAXFLOAT); //224 is known to be the width available
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
Run Code Online (Sandbox Code Playgroud)
一些更好的示例可能会使用tableview的边界,然后为单元格中的其他视图减去幻数或常量,这至少会更清晰一些(如果边界在旋转上发生变化等,效果会更好).
CGSize constraint = CGSizeMake(self.tableview.bounds.size.width - 20 - 10 - 36 - 20, MAXFLOAT); //each non-label width subtracted
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
Run Code Online (Sandbox Code Playgroud)
这是常见的方法,但似乎缺乏.谁能说出其他单元格子视图的宽度,甚至是字体,都保证不会改变(或者两个地方都会一直在努力改变)?
我决定让细胞负责提供这个高度.单元格始终知道其属性.为了实现这一点,我首先在单元格中创建了所有布局代码,引用了一些局部常量.然后我在我的自定义单元格类中添加了一个类方法,它可以根据自己的字体和大小调整常量返回任何给定文本的所需高度.
+ (CGFloat)heightRequiredForText:(NSString *)text usingWidth:(CGFloat)width
{
//a class method that tells the caller how much height is needed for the provided text, based on …Run Code Online (Sandbox Code Playgroud)