简体   繁体   English

UITableView背景图片的内容

[英]UITableView background image for content

I have UITableView with height = 1000px, I need to set scrollable background. 我有高度= 1000px的UITableView,我需要设置可滚动的背景。 I know how to set background image for tableview but in this case that background image will not be scrollable. 我知道如何为tableview设置背景图像,但是在这种情况下背景图像将无法滚动。

Right now the only idea how to do that is: 1) create scroll view with proper height (about 1000 px), and set background image for that scroll view. 现在唯一的想法是:1)创建具有适当高度(大约1000 px)的滚动视图,并为该滚动视图设置背景图像。 then set frame of UITableView as the bounds of scroll view (and disable scrolling for UITableView). 然后将UITableView的框架设置为滚动视图的边界(并禁用UITableView的滚动)。

But my idea is rather bad, because I have many cells with many images on them, and in my approach all the cells will stay in memory. 但是我的想法很糟糕,因为我有许多单元格,上面有很多图像,按照我的方法,所有单元格都将保留在内存中。

What is the best way how to implement scrollable background inside tableview? 在tableview中实现可滚动背景的最佳方法是什么?

PS 聚苯乙烯

I have tableview header of unknown height (depends on the response from server) 我有未知高度的tableview标头(取决于服务器的响应)

If it's a repeatable pattern, one way would be to split the image, repeat the middle part and show the top/bottom only when the contentOffset reaches the boundaries of contentSize . 如果是可重复的模式,则一种方法是分割图像,重复中间部分,仅在contentOffset到达contentSize的边界时显示顶部/底部。 Or set the top/bottom parts only for first/last cell and the center (repeatable part) for every other. 或仅将第一个/最后一个单元格的顶部/底部部分设置为彼此之间的中间(可重复部分)。

Edit: 编辑:

As I said, for a repeatable pattern: 如我所说,对于可重复的模式:

样本背景图片

You crop the elements you'd use for header, footer and middle 裁剪要用于页眉,页脚和中间的元素

样品裁切线

样本裁剪的“组件”

Of course, for header & footers with information in them you'd probably need bigger header/footer images: 当然,对于其中包含信息的页眉和页脚,您可能需要更大的页眉/页脚图像:

具有较大页眉和页脚的样本

Get the parts and assign them as backgrounds in your cellForRowAtIndexPath: method. 获取零件并将它们分配为cellForRowAtIndexPath:方法中的背景。 You can do the whole thing with a single image without separating the cropped parts in different files, that's already discussed here . 您可以使用单个图像完成整个操作,而无需将裁切后的部分分离到不同的文件中,这已经在此处进行了讨论

There is some example code in the official docs on how to make a "synchronized" scroll view. 官方文档中有一些有关如何制作“同步”滚动视图的示例代码。 Since a table view is a scroll view, it should work to synchronize with a table view as well. 由于表视图是滚动视图,因此它也应该与表视图同步。 So make your background be a scroll view behind the table view, and synchronize it. 因此,使您的背景成为表格视图后面的滚动视图,并进行同步。

The critical step is this: 关键步骤是:

[[NSNotificationCenter defaultCenter] addObserver:self
                     selector:@selector(synchronizedViewContentBoundsDidChange:)
                     name:NSViewBoundsDidChangeNotification
                       object:synchronizedContentView];

Then it's simple in your handler to match up the content offset of your background vs. the table view. 然后,在处理程序中将背景与表格视图的内容偏移进行匹配很简单。

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

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