【发布时间】:2016-08-31 10:51:21
【问题描述】:
我想在 tableview 中动态添加集合视图。我已经制作了以下代码。
collectionview 的单元类
class NewsFeedCollectionViewCell : UICollectionViewCell{
@IBOutlet weak var imageViewSlider: UIImageView!
}
比在 indexpath 的 Tableview cellforrow 中分配 collectionview
cell.collectionViewNewsFeed.tag = indexPath.row
cell.collectionViewNewsFeed.reloadData()
添加以下collectionview的代表
// MARK: - Collection view Delegates / Datasources
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (mutableArrayNewsFeed[collectionView.tag]["images"] as! NSArray).count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NewsFeedCollectionViewCell", forIndexPath: indexPath) as! NewsFeedCollectionViewCell
print("tag : \(collectionView.tag) , Row : \(indexPath.row)")
let img = (mutableArrayNewsFeed[collectionView.tag]["images"] as! NSArray)[indexPath.row] as? String ?? "imgLoginScreenLogo"
cell.imageViewSlider.image = UIImage(named: img)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Collection View Row : \(indexPath.row)")
}
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
return collectionView.frame.size
}
它可以正确调整,但是当我滚动 collectionview 时索引会发生变化。例如,我将 collectionview 滚动到 3 个单元格,而不是转到 tableview 的第 4 个索引,而不是将 collectionview 的第 4 个索引的索引设置为第 3 个。
只想在 Table 视图中添加包含多个图像的集合视图。我已经添加了,但是在将集合视图滚动到 tableview 的第一个单元格上的第三个图像后,我移动到 Tableview 的第 4 个单元格,collectionview 也自动滚动到第 4 个单元格。
需要摆脱困境。
【问题讨论】:
标签: iphone swift uitableview uicollectionview uicollectionviewcell