Ios 水平滚动UICollectionView的所有行

Ios 水平滚动UICollectionView的所有行,ios,swift,uitableview,uiscrollview,uicollectionview,Ios,Swift,Uitableview,Uiscrollview,Uicollectionview,我有UITableView。在UITableViewCell内,我已放置UICollectionView。当前,所有行都会单独滚动。我需要的是,当用户以水平方式(从左向右或从右向左)滚动任何行时,所有行必须以相同的模式滚动 我已经按照下面的链接,但没有成功 请看一下我的密码 视图控制器 import UIKit class ViewController: UIViewController, UITableViewDataSource { var categories = ["He

我有
UITableView
。在
UITableViewCell
内,我已放置
UICollectionView
。当前,所有行都会单独滚动。我需要的是,当用户以水平方式(从左向右或从右向左)滚动任何行时,所有行必须以相同的模式滚动

我已经按照下面的链接,但没有成功

请看一下我的密码

视图控制器

import UIKit

class ViewController: UIViewController, UITableViewDataSource {

    var categories = ["Header 1", "Header 2", "Header 3", "Header 4", "Header 5", "Header 6", "Header 7", "Header 8"]

    @IBOutlet var myTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        myTableView.dataSource = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return categories[section]
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return categories.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? CategoryRow
        if cell == nil {
            cell = CategoryRow(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")
        }
        else {
        }
        cell!.tag = indexPath.section
        cell?.collection.reloadData()
        return cell!
    }
}
MyCollectionViewCell

import UIKit

class MyCollectionViewCell: UICollectionViewCell {

    @IBOutlet var title: UILabel!
    @IBOutlet var data: UILabel!
}
CategoryRow

import UIKit

class CategoryRow: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {

    var Title1 = ["1st Title 1", "1st Title 2", "1st Title 3", "1st Title 4", "1st Title 5", "1st Title 6", "1st Title 7", "1st Title 8"]
    var Data1 =  ["1st Data 1", "1st Data 2", "1st Data 3", "1st Data 4", "1st Data 5", "1st Data 6", "1st Data 7", "1st Data 8"]

    var Title2 = ["2nd Title 1", "2nd Title 2", "2nd Title 3", "2nd Title 4", "2nd Title 5", "2nd Title 6", "2nd Title 7", "2nd Title 8"]
    var Data2 =  ["2nd Data 1", "2nd Data 2", "2nd Data 3", "2nd Data 4", "2nd Data 5", "2nd Data 6", "2nd Data 7", "2nd Data 8"]

    var Title3 = ["3rd Title 1", "3rd Title 2", "3rd Title 3", "3rd Title 4", "3rd Title 5", "3rd Title 6", "3rd Title 7", "3rd Title 8"]
    var Data3 =  ["3rd Data 1", "3rd Data 2", "3rd Data 3", "3rd Data 4", "3rd Data 5", "3rd Data 6", "3rd Data 7", "3rd Data 8"]

    var Title4 = ["4th Title 1", "4th Title 2", "4th Title 3", "4th Title 4", "4th Title 5", "4th Title 6", "4th Title 7", "4th Title 8"]
    var Data4 =  ["4th Data 1", "4th Data 2", "4th Data 3", "4th Data 4", "4th Data 5", "4th Data 6", "4th Data 7", "4th Data 8"]

    var Title5 = ["5th Title 1", "5th Title 2", "5th Title 3", "5th Title 4", "5th Title 5", "5th Title 6", "5th Title 7", "5th Title 8"]
    var Data5 =  ["5th Data 1", "5th Data 2", "5th Data 3", "5th Data 4", "5th Data 5", "5th Data 6", "5th Data 7", "5th Data 8"]

    var Title6 = ["6th Title 1", "6th Title 2", "6th Title 3", "6th Title 4", "6th Title 5", "6th Title 6", "6th Title 7", "6th Title 8"]
    var Data6 =  ["6th Data 1", "6th Data 2", "6th Data 3", "6th Data 4", "6th Data 5", "6th Data 6", "6th Data 7", "6th Data 8"]

    var Title7 = ["7th Title 1", "7th Title 2", "7th Title 3", "7th Title 4", "7th Title 5", "7th Title 6", "7th Title 7", "7th Title 8"]
    var Data7 =  ["7th Data 1", "7th Data 2", "7th Data 3", "7th Data 4", "7th Data 5", "7th Data 6", "7th Data 7", "7th Data 8"]

    var Title8 = ["8th Title 1", "8th Title 2", "8th Title 3", "8th Title 4", "8th Title 5", "8th Title 6", "8th Title 7", "8th Title 8"]
    var Data8 =  ["8th Data 1", "8th Data 2", "8th Data 3", "8th Data 4", "8th Data 5", "8th Data 6", "8th Data 7", "8th Data 8"]

    @IBOutlet var collection: UICollectionView!


    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        // Some logic here
    }


    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("videoCell", forIndexPath: indexPath) as! MyCollectionViewCell

        switch (self.tag) {
            case 0:
                cell.title.text = self.Title1[indexPath.row]
                cell.data.text = self.Data1[indexPath.row]
                break

            case 1:
                cell.title.text = self.Title2[indexPath.row]
                cell.data.text = self.Data2[indexPath.row]
                break

            case 2:
                cell.title.text = self.Title3[indexPath.row]
                cell.data.text = self.Data3[indexPath.row]
                break

            case 3:
                cell.title.text = self.Title4[indexPath.row]
                cell.data.text = self.Data4[indexPath.row]
                break

            case 4:
                cell.title.text = self.Title5[indexPath.row]
                cell.data.text = self.Data5[indexPath.row]
                break

            case 5:
                cell.title.text = self.Title6[indexPath.row]
                cell.data.text = self.Data6[indexPath.row]
                break

            case 6:
                cell.title.text = self.Title7[indexPath.row]
                cell.data.text = self.Data7[indexPath.row]
                break

            case 7:
                cell.title.text = self.Title8[indexPath.row]
                cell.data.text = self.Data8[indexPath.row]
                break

            default:
                break
        }
        return cell
    }
}
编辑-1

项目链接了解场景可能会有所帮助

编辑-2 我已尝试实现下面的链接,但无法做到这一点


提前感谢

假设您有两个收藏视图。你能做的就是这样

// say cv0 and cv1 are outlets to two table views

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView == self.cv0 {
        self.cv1.contentOffset = self.cv0.contentOffset
    }
    else {
        if scrollView == self.tv1 {
            self.cv0.contentOffset = self.cv1.contentOffset
        }
    }
}

如果要获取更多集合视图,可以编写一个通用方法来处理它。您可以将scrollView传递给此方法,堆栈contentOffSet中的所有其他滚动视图都会相应地设置。

多亏了@impossable for help,问题现在得到了解决

首先,我在
UITableView
cellforrowatinexpath
中设置了标签
cell?.collection.tag

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? CategoryRow
    if cell == nil {
        cell = CategoryRow(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")
    }
    else {
    }
    cell!.tag = indexPath.section
    cell?.collection.tag = (100 * indexPath.section) + 1
    cell?.collection.reloadData()
    cell?.collection.delegate = self
    return cell!
}
func scrollViewDidScroll(scrollView: UIScrollView) {

    for var i = 0 ; i < 8 ; i++ {
        let cell2 = self.view .viewWithTag((100 * i) + 1) as? UICollectionView

        if cell2 != nil {
            cell2!.contentOffset = scrollView.contentOffset
        }
    }
}
然后我使用tag方法
viewWithTag
inside
scrollviewdidcroll
like获得视图

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? CategoryRow
    if cell == nil {
        cell = CategoryRow(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cell")
    }
    else {
    }
    cell!.tag = indexPath.section
    cell?.collection.tag = (100 * indexPath.section) + 1
    cell?.collection.reloadData()
    cell?.collection.delegate = self
    return cell!
}
func scrollViewDidScroll(scrollView: UIScrollView) {

    for var i = 0 ; i < 8 ; i++ {
        let cell2 = self.view .viewWithTag((100 * i) + 1) as? UICollectionView

        if cell2 != nil {
            cell2!.contentOffset = scrollView.contentOffset
        }
    }
}
func scrollViewDidScroll(scrollView:UIScrollView){
对于变量i=0;i<8;i++{
将cell2=self.view.viewWithTag((100*i)+1)设为UICollectionView
如果cell2!=nil{
cell2!.contentOffset=scrollView.contentOffset
}
}
}

您能详细解释一下预期的行为吗?我认为这可能是通过调用委托方法来实现的,该方法用于在委托调用中滚动所有其他集合视图,用于实际滚动的集合视图,我已经上传了项目的链接,它可能会帮助你得到场景。我会检查你的代码,并在今晚更新答案。现在工作有点忙Gihan,你能告诉我如何获得cv0和cv1吗?你可以在数组中保留对它们的引用。或者您可以使用var indexPathsForVisibleRows:[nsindepath]从tableView获取所有可见行?{get}并获取单元格,然后滚动视图我已尝试使用此变量visibleItems=self.collection.indexPathsForVisibleItems()让currentItem=visibleItems[0]让nextItem=NSIndexPath(forItem:currentItem.item+1,Instition:currentItem.section)self.collection.scrollToItemAtIndexPath(下一步,在ScrollPosition:UICollectionViewScrollPosition。对,动画:true)正确吗?任何代码都将非常感谢。我将马上查看您的代码。现在有点忙。@Gihan,我有类似的问题。如果您能帮上忙,请检查一下好吗?