UITableViewCell 中的 UIScrollview 和维护滚动视图页面 [英] UIScrollview within UITableViewCell and maintaining scrollview page

查看:26
本文介绍了UITableViewCell 中的 UIScrollview 和维护滚动视图页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableViewCell 中有一个 UIScrollView,以便能够滚动单元格内的图像.然而,当一个单元格被重用时,滚动位置/内容会重新加载,因此当它再次出现时,该单元格不记得它所在的滚动位置(页面).

I have a UIScrollView within a UITableViewCell for being able to scroll through images within a cell. However, as a cell is reused the scroll position/content is reloaded and therefore the cell doesn't remember what scroll position (page) it was on when it comes into view again.

在 UITableView 单元格中拥有滚动视图并让它在返回视图时保持位置的最佳方法是什么.AirBnB 应用程序 (https://itunes.apple.com/us/app例如,/airbnb/id401626263?mt=8) 似乎已经做到了这一点.

What's the best way to have a scroll view within a UITableView cell and have it maintain position as it comes back into view. The AirBnB app (https://itunes.apple.com/us/app/airbnb/id401626263?mt=8) seems to have accomplished this for example.

推荐答案

您需要在属性中跟踪滚动视图的内容偏移量.在下面的示例中,我使用可变字典来执行此操作.在 cellForRowAtIndexPath: 中,我给滚动视图一个标签并将控制器设置为委托.在滚动视图委托方法 scrollViewDidEndDecelerating: 中,滚动视图的内容偏移被设置为与滚动视图标签对应的键的对象.在 cellForRowAtIndexPath: 中,我检查 indexPath.row(转换为 NSNumber)是否是字典的键之一,如果是,则恢复该滚动视图的正确偏移量.我给标签添加 1 的原因是因为表格视图有自己的滚动视图,它的标签为 0,所以我不想使用 0 作为单元格滚动视图之一的标签.

You need to keep track of your scroll views' content offset in a property. In the example below, I do this with a mutable dictionary. In cellForRowAtIndexPath:, I give the scroll view a tag and set the controller as the delegate. In the scroll view delegate method, scrollViewDidEndDecelerating:, the scroll view's content offset is set as the object for the key corresponding to the scroll view's tag. In cellForRowAtIndexPath:, I check for whether the indexPath.row (converted to an NSNumber) is one of the keys of the dictionary, and if so, restore the correct offset for that scroll view. The reason I add 1 to the tags is because the table view has its own scroll view which has a tag of 0, so I don't want to use 0 as a tag for one of the cell's scroll views.

所以在 cellForRowAtIndexPath 中,你需要这样的东西:

So in cellForRowAtIndexPath, you need something like this:

cell.scrollView.tag = indexPath.row + 1;
cell.scrollView.delegate = self;

if ([self.paths.allKeys containsObject:@(indexPath.row + 1)]) {
    cell.scrollView.contentOffset = CGPointMake([self.paths[@(indexPath.row + 1)] floatValue],0);
}else{
    cell.scrollView.contentOffset = CGPointZero;
}
return cell;

在委托方法中:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.tag != 0)
        [self.paths setObject:@(scrollView.contentOffset.x) forKey:@(scrollView.tag)];
}

paths 是我在 viewDidLoad 中创建的一个属性 (NSMutableDictionary).

paths is a property (NSMutableDictionary) that I create in viewDidLoad.

这篇关于UITableViewCell 中的 UIScrollview 和维护滚动视图页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆