如何保存UICollectionViewCell的状态

3

请帮我,如何保存UICollectionViewCell的状态。例如:我有带有UIWebView的单元格,当我使用dequeueReusableCellWithReuseIdentifier:forIndexPath:返回错误的单元格时,不是为这个indexPath,然后UIWebView的内容将重新加载以获得正确的indexPath,并且会出现抽搐。谢谢!

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NEWS_CELL" forIndexPath:indexPath];

    if (!cell.isShowed) {  // I just added boolean property
        cell.isShowed = YES;

        /*** some init actions ***/
    }

    return cell;
}

如果我使用这种方法,那些 cell.isShowed == YES 的单元格会返回错误的 indexPath。但是如果我不使用 cell.isShowed ,每次显示时 UIWebView 的内容都将重新加载。

抱歉我的英语不好 :)

你使用了正确的indexPath吗? - sunkehappy
是的,我只是使用了 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 中的 indexPath。 - Alexander Zakatnov
“not for this indexPath”是什么意思?你能把你的代码粘贴一下吗?我需要更多的代码来看看你的问题。 - sunkehappy
“return on wrong indexPath” 意味着单元格成功显示,但在错误的行上? - sunkehappy
那么,sunkehappy,你对此有什么想法? :) - Alexander Zakatnov
1个回答

0

看看苹果的UIWebView Class Reference说了什么:

重要提示:不应将UIWebView或UITableView对象嵌入UIScrollView对象中。如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能会混淆和错误处理。

但是UICollectionView继承自UIScrollView。因此,您需要重新设计您的架构。


webView.scrollView.scrollEnabled = NO; webView.scrollView.scrollEnabled = NO; - Alexander Zakatnov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接