ios - 当上面有其他内容且大小灵活时,objc xcode 设置 uitableview 单元格的 y 起始位置

标签 ios objective-c xcode uitableview layout

我对 UITableView 的用法有疑问。我在 UITableView 的单元格上方添加了一个 UIView(见图)。

view stack of ui elements in the table view - you can see I have a view above the table cells with some buttons and labels and an image

这非常好,因为我可以在那里添加一些图像和标签,它会随着表格 View 的单元格滚动。现在我正在调用一些 REST API 来获取我想要添加到单元格上方的 View 中的图像。现在的问题是我不知道图像的高度,所以我必须根据已经可以正常工作的纵横比来计算它。当我添加图像时,我可以正确更改其高度并向下移动标签和按钮,但图像与一些可见单元格重叠。

image is placed on a button which is in the middle of the pink area which is the View

我的问题:如何向下移动容器的框架?的细胞?动态地基于我的图像各自的 View 高度? 我试图在 TableView 中设置 View 的高度,但没有效果。所以我想我必须设置单元格的 y 起始位置,但我不知道如何设置。 我是否需要在委托(delegate)方法 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中设置 y 偏移量? 有什么想法吗?

最佳答案

我认为关键是在更改 View 大小后将 View 设置为 TableView 的 tableHeaderView 。我在测试应用程序中是这样做的,

-(void)layoutHeader {
    self.label.text = @"This is a long text to see if it expands to take up multple lines. The quick red fox jumped over the lazy brown dog.";
    [self.label setPreferredMaxLayoutWidth:self.tableView.frame.size.width];
    CGRect stringRect = [self.label.text boundingRectWithSize:CGSizeMake(self.tableView.bounds.size.width - 40,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.label.font} context:nil];
    CGRect headerFrame = self.header.frame;
    headerFrame.size.height = stringRect.size.height + 40;
    self.header.frame = headerFrame;
    [self.tableView beginUpdates];
    self.tableView.tableHeaderView = self.header;
    [self.tableView endUpdates];
}

我通过 viewDidLoad 的延迟来调用此函数作为测试。如果您不想看到行向下移动以适应新的 View 大小,则不需要 beginUpdates、endUpdates 代码。 header 属性是我添加到 IB 中 TableView 顶部的 View 的 IBOutlet,“label”是该 View 的 subview 。

关于ios - 当上面有其他内容且大小灵活时,objc xcode 设置 uitableview 单元格的 y 起始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24536544/

相关文章:

ios - 在解除父 View 时隐藏登录 uiViewController

ios - 触摸对象本身时拖动图像

xcode - 每次启动时都会验证 Xcode beta 6.2

ios - UICollectionView 使用自定义布局抛出未捕获的异常

ios - 如何在三天后触发 uilocalnotification?

ios - 使用 Apple 登录 : Possibility To Create Duplicate Account Issue

android - 在 Dart 中读取一个 "TXT"文件

ios - UICollectionViewCell 的动态高度取决于内容

ios - 约束不能正常工作

swift - 下载图像后重新加载 View 中的单元格