两种方法中都有断点。 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
。