简体   繁体   English

objc xcode设置uitableview单元格的y起始位置

[英]objc xcode setting y start position of uitableview cells when there is other content above with flexible size

I have a question about the usage of UITableView. 我对UITableView的用法有疑问。 I have added a UIView above the cells of my UITableView (see image). 我在UITableView的单元格上方添加了一个UIView(见图)。

在表格视图中查看ui元素的堆栈-您可以看到我在表格单元格上方有一个带有一些按钮和标签以及一个图像的视图

This is very nice because I can add some images and labels there and it will scroll with the cells of the table view. 这非常好,因为我可以在其中添加一些图像和标签,它会随着表格视图的单元格滚动。 Now I am calling some REST API to get an image which I want to add in this view above the cells. 现在,我正在调用一些REST API来获取图像,该图像要在此视图中的单元格上方添加。 The problem now is that I dont know the height of the image, so I have to calculate it based on the aspect ratio which already works fine. 现在的问题是我不知道图像的高度,因此我必须根据已经可以正常工作的纵横比来计算它。 When I add the image I can change its height correctly and move down labels and buttons BUT the image overlaps some of the visible cells. 添加图像时,我可以正确更改其高度并向下移动标签和按钮,但是图像与某些可见单元格重叠。

图像放置在粉色区域(即“视图”)中间的按钮上

My question: How can I move down the frame of the container? 我的问题:如何下移容器的框架? of the cells? 的细胞? dynamically based on my image respective View height? 动态基于我的图像各自的视图高度? I have tried to set the height of the View in the TableView but it has no effect. 我试图在TableView中设置View的高度,但没有任何效果。 So I suppose that I have to set the y start position of the cells but I dont know how. 所以我想我必须设置单元格的y起始位置,但我不知道如何。 Do I need to set an y offset in the delegate method -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ? 我是否需要在委托方法-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中设置y偏移量? Any ideas? 有任何想法吗?

I think the key to this is setting your view to be the table view's tableHeaderView after you change the size of the view. 我认为关键是在更改视图大小后将视图设置为表视图的tableHeaderView。 I did it like this in a test app, 我是在测试应用中这样做的,

-(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];
}

I called this with a delay from viewDidLoad as a test. 我称此为viewDidLoad的测试延迟。 The beginUpdates, endUpdates code isn't necessary if you don't want to see the rows move down to accommodate the new view size. 如果您不想看到行向下移动以适应新的视图大小,则无需beginUpdates,endUpdates代码。 The property, header, is an IBOutlet to the view I added to the top of the table view in IB, and "label" is a subview of that view. 属性标题是我在IB中添加到表格视图顶部的视图的IBOutlet,“ label”是该视图的子视图。

I would personally just use tableView:viewForHeaderInSection: to build the view out, and in tableView:heightForHeaderInSection: calculate the new height and return that. 我个人将只使用tableView:viewForHeaderInSection:来构建视图,然后在tableView:heightForHeaderInSection:计算新的高度并将其返回。 That way you don't have to worry about bumping things down within the tableView since UITableView will handle the rest for you once you. 这样一来,您不必担心在tableView中碰到东西的麻烦,因为UITableView会在您处理完后为您处理其余的事情。 Just make sure to call [_tableView reloadData]; 只要确保调用[_tableView reloadData]; on your tableView after you get the image. 在获取图像后,在tableView上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM