ios - 如何在两个单元格之间传递数据

标签 ios swift uitableview uicollectionview

经过几个小时的谷歌搜索后,我似乎需要来自社会的提示。

所以问题是: 我有两个定制原型(prototype)单元。第一个包含带有 12 个单元格的 UICollectionView,第二个仅包含一个标签。

我的任务是将 indexPath.row 从第一个单元格的 collectionView 传递到第二个单元格的标签。

didSelectItemAtIndexPath: 不起作用或者我没有正确调整它。

任何有关如何实现的想法将不胜感激!

这是我的代码(很抱歉 StackOverflow 格式不佳)

    import UIKit

class MainTableViewController: UITableViewController {


    var storedOffsets = [Int: CGFloat]()


    var descrLabel = UILabel()


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

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if indexPath.row == 0 {

            let cell = tableView.dequeueReusableCellWithIdentifier("MainCell", forIndexPath: indexPath) as! MainTableViewCell

            let bgImg = UIImageView(image: UIImage(named: "background"))
            bgImg.contentMode = UIViewContentMode.ScaleAspectFill
            cell.backgroundView = bgImg
            return cell
        }

        else {
            let cell = tableView.dequeueReusableCellWithIdentifier("DescriptionCell", forIndexPath: indexPath) as! DescriptionTableViewCell

            cell.descriptionLabel.text = self.descrLabel.text
            return cell
        }
    }

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

        guard let tableViewCell = cell as? MainTableViewCell else { return }

        tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
        tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
    }

    override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

        guard let tableViewCell = cell as? MainTableViewCell else { return }

        storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
    }
}


extension MainTableViewController: UICollectionViewDelegate, UICollectionViewDataSource {

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

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

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("Collection view at row \(collectionView.tag) selected index path \(indexPath.row)")
        self.descrLabel.text = "testText"
    }
}

最佳答案

我在 Playground 上玩耍并做了这个。 它尚未经过测试,但应该可以为您提供一些想法。

  protocol CustomDelegate{
    func passData(data: AnyObject)
}

class CustomColletionView : UICollectionView, UICollectionViewDelegate{
    var customDelegate : CustomDelegate!
    func didSelectItemAtIndexPath(collectionView : UICollectionView, indexPath : NSIndexPath){
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        let someData = ""
        customDelegate.passData(someData)
    }
}
class ViewController: UITableViewController, CustomDelegate{
    var labelCellData : AnyObject?{
        didSet{
            //reloadCell when data is received
            tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 1, inSection: 0)], withRowAnimation: .Bottom)
        }
    }
    func passData(data: AnyObject) {
        labelCellData = data
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier:"Indetifier1")
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "simpleCell")
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        switch indexPath.row{
        case 0:
            //this should contain CustomCollectionView
            let cell = tableView.dequeueReusableCellWithIdentifier("Indetifier1") as! CustomTableViewCell
            cell.collectionView.customDelegate = self
            return cell
        case 1:
            let cell = tableView.dequeueReusableCellWithIdentifier("simpleCell")
            cell?.textLabel?.text = labelCellData as? String
            return cell!
        default:
            break
        }
        return UITableViewCell()
    }

}

class CustomTableViewCell: UITableViewCell {
    var collectionView : CustomColletionView!
    // You need to implement it

}

关于ios - 如何在两个单元格之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34837938/

相关文章:

iphone - 在应用程序处于后台时计时器用完后,如何让应用程序发送 GPS 位置?

objective-c - 2 单个 View 上的 TableView

ios - Tableview 仅在一侧弹跳

ios - 如何在实时帧捕获中使用 GoogleMobileVision?

ios - 当用户从照片库中选择一张时图像会旋转

ios - NSLayoutContraint 中的 UIView 不符合 swift 中的 AnyObject

swift - SKView 警告日志

ios - 在 XCTestCase 期间切换互联网可用性

iphone - 在 View 加载时修改 uitableview 上的多行

ios - 从控制台操作 xcode 方案