为什么在UITableView中,在heightForRowAtIndexPath之前调用cellForRowAtIndexPath

时间:2018-11-09 16:02:49

标签: ios swift uitableview

两种方法中都有断点。 cellForRowAtIndexPath总是在heightForRowAtIndexPath之前调用。 问题是我在cellForRowAtIndexPath中有一个取决于行高的逻辑代码。 目前,我进入cellForRowAtIndexPath的行高度始终为0。

是否有办法让heightForRowAtIndexPath之前的cellForRowAtIndexPath被调用? 如果没有,如何在我的代码中显式调用heightForRowAtIndexPatch

编辑:

这是我的cellForRowAtIndexPath代码:

let slideshowCell = tableView.dequeueReusableCell(withIdentifier: "SlideShowCell") as! SlideShowTableViewCell
slideshowCell.delegate = self
slideshowCell.news = featuredNews
return slideshowCell

这是我的heightForRowAtIndexPatch代码:

let filterApplied = categoryNews.count != filteredCategoryNews.count   
if featuredNews.count == 0 || filterApplied
{
    return 0
}
else
{
    return 530
}

当我将featuredNews设置为slideshowCell.news时,我在slideshowCell.news的{​​{1}}属性观察器中运行此方法:

didSet

问题在于private func populateSlideshowScrollView() { let imagesCount = CGFloat(slideshowNews.count) // set the scrollView's content size to the size of all the images scrollView.contentSize = CGSize(width: scrollView.frame.width * imagesCount, height: scrollView.frame.height) // Remove subviews of ScrollView before populating it with new images for subview in scrollView.subviews { subview.removeFromSuperview() } // populate the scrollView with images form the 'slideshowNews' property for i in 0..<slideshowNews.count { let imgView = UIImageView(frame: CGRect(x: scrollView.frame.width * CGFloat(i), y: scrollView.bounds.origin.y, width: scrollView.frame.width, height: scrollView.frame.height)) imgView.contentMode = .scaleAspectFill imgView.clipsToBounds = true imgView.image = slideshowNews[i].image scrollView.addSubview(imgView) } // set scrollview to the first page from the right, only the first two times this method called. // After that the current page of page control should be in the correct position if numberOfRefresh < 2 { scrollView.setContentOffset(CGPoint(x: scrollView.frame.width * (imagesCount - 1), y: 0), animated: false) newsTitleLabel.text = slideshowNews.last?.title sourceLabel.text = slideshowNews.last?.source.name numberOfRefresh += 1 } scrollToPage(page: pageControl.currentPage, animated: false) // to update scrollView's contentOffset } 将始终返回零,因为此时尚未调用scrollView.frame.height

0 个答案:

没有答案