Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在cellForRow中设置一个浮动。。然后把它放在高处_Ios_Objective C_Uitableview_Custom Cell_Heightforrowatindexpath - Fatal编程技术网

Ios 在cellForRow中设置一个浮动。。然后把它放在高处

Ios 在cellForRow中设置一个浮动。。然后把它放在高处,ios,objective-c,uitableview,custom-cell,heightforrowatindexpath,Ios,Objective C,Uitableview,Custom Cell,Heightforrowatindexpath,我有一个带有标签的自定义单元格。我想做的是使标签大小适合cellForRow。。。并将标签高度传递给名为labelHeight的变量。然后在高高的福罗。。。我想返回标签高度 问题是,它返回的高度为0。这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... [customCell.label sizeToF

我有一个带有标签的自定义单元格。我想做的是使标签大小适合cellForRow。。。并将标签高度传递给名为labelHeight的变量。然后在高高的福罗。。。我想返回标签高度

问题是,它返回的高度为0。这是我的密码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    [customCell.label sizeToFit];
    self.labelHeight = customCell.label.frame.size.height;
    ...
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return self.labelHeight;
}

我认为没有很好的解决方案,因为heightForRowAtIndexPath在cellForRowAtIndexPath之前被调用

你可以:

通过计算具有特定字体样式和大小的文本量将采用的大小,计算heightForRowAtIndexPath上单元格的高度:

CGSize size = [string sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];    
CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
或者,您可以模拟整个视图,以获得更精确的结果,但计算成本会更高,或者

在计算cellForRowAtIndexPath上的单元格高度后重新加载tableview
如果您需要计算HeightForRowatineXpath,最简单的方法是使用heightForRow从调用CellForRowatineXpath,尽管它感觉效率很低。。。然后在表上获取调用重载,该表将再次调用cellForRow。。。由于单元格的双重创建,这感觉相当脏,如果您能够简化高度计算,将已知常数与动态常数分解,这可能会更有效,但代码更多。例如

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath(NSIndexPath *)indexPath{
  NSString *string = self.objects[indexPath.row];
  NSDictionary *textAttributes = nil; //attributes here

  return self.someButton.frame.size.height +
        self.someSwitch.frame.size.height +
        [string boundingRectWithSize:CGSizeMake(LABEL_WIDTH, CGFLOAT_MAX)
                       options:NSStringDrawingUsesLineFragmentOrigin
                    attributes:textAttributes
                       context:nil].size.height;
} 

如果您能够支持iOS 8+并使用autolayout,更好的解决方案是将tableView上的-estimatedRowHeight属性设置为非零值,那么单元格上的UILayoutConstraints将能够为您计算该值。这是iOS8中TableView的一个非常受欢迎的新添加,在WWDC的一个视频中进行了描述。但是,如果没有单元格上的自动布局,它将无法工作。

这将永远无法工作。HeightForRowatineXpath:在调用CellForRowatineXpath之前很久就会被调用。那么我可以做些什么来解决这个问题?您必须在不首先为行创建单元格的情况下确定HeightForRowatineXpath中的高度。那么,我如何在heigtForRow中设置大小?这是一个自定义单元格我可以做第一个解决方案,但唯一的问题是,这是一个自定义单元格,我不知道如何从HeightForRow访问单元格属性添加了更多关于如何计算单元格中字符串大小的解释。我制作了一个调整后的Heigh的nsLog,它继续循环一定的数值。然后我返回Adjusted height,所选单元格及其下的所有单元格都被删除。您应该使用SizeWithat属性来计算单元格的最大高度。你现在是怎么做到的?我不明白。我为单元格设置了一个高度,然后我想添加标签高度