首页 > 解决方案 > 将 TableViewCell 传递给新的 TableView(在顶部)Swift

问题描述

我想要做什么: 我有一个提要(TableView(并且一旦用户点击一个帖子(TableViewCell),他应该看到一个新页面(TableView),他首先点击的帖子(同一个TableViewCell)在顶部和一个下面有几条评论。

我的问题: 我不知道如何“克隆”那个 TableViewCell。

为了更好的理解,这里放两张图:

我要克隆的帖子的提要

它应该是这样的:第一部分中的 Post 并从用户单击的 Cell 中克隆

一些复杂性: 我在主提要中有多种帖子类型,因此代码必须区分它们以查看使用哪种单元格类型来显示内容。

我的代码:

主要饲料

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return mixed.count
        }
        else if section == 1{
            return phots.count
        }
        else{
            return texttt.count
        }

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.section == 0{
            let cell = tableView.dequeueReusableCell(withIdentifier: mixedTableViewCell.identifier, for: indexPath) as! mixedTableViewCell
            cell.configure(with: self.mixed[indexPath.row])
            return cell
        }
        else if indexPath.section == 1{
            let cell = tableView.dequeueReusableCell(withIdentifier: popularTableViewCell.identifier, for: indexPath) as! popularTableViewCell
            cell.configure(with: self.phots[indexPath.row])
            return cell
        }
        else{
            let cell = tableView.dequeueReusableCell(withIdentifier: textTableViewCell.identifier, for: indexPath) as! textTableViewCell
            cell.configure(with: self.texttt[indexPath.row])
            return cell
        }

    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "commentsVC")
        vc.modalPresentationStyle = .fullScreen

        self.navigationController?.pushViewController(vc, animated: true)
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return UITableView.automaticDimension
    }

我的第二个 ViewController

class CommentsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var table: UITableView!

    var texty = [TextComment]()

    override func viewDidLoad() {
        super.viewDidLoad()

        table.register(popularTableViewCell.nib(), forCellReuseIdentifier: popularTableViewCell.identifier)
        table.register(featuredTableViewCell.nib(), forCellReuseIdentifier: featuredTableViewCell.identifier)
        table.register(textTableViewCell.nib(), forCellReuseIdentifier: textTableViewCell.identifier)
        table.register(mixedTableViewCell.nib(), forCellReuseIdentifier: mixedTableViewCell.identifier)
        table.register(textComTableViewCell.nib(), forCellReuseIdentifier: textComTableViewCell.identifier)


        table.delegate = self
        table.dataSource = self


    }



    func numberOfSections(in tableView: UITableView) -> Int {
        2
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return 1
        }
        else{
            return 15
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0{
            let cell = tableView.dequeueReusableCell(withIdentifier: textTableViewCell.identifier, for: indexPath) as! textTableViewCell
            return cell
        }
        else{
            let cell = tableView.dequeueReusableCell(withIdentifier: textComTableViewCell.identifier, for: indexPath) as! textComTableViewCell
            return cell
        }
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0{
//            self.table.estimatedRowHeight = 250
//            self.table.rowHeight = UITableView.automaticDimension
//            return UITableView.automaticDimension
            return 300
        }
        else{
//            self.table.estimatedRowHeight = 150
//            self.table.rowHeight = UITableView.automaticDimension
//            return UITableView.automaticDimension
            return 150
        }
    }

注意 现在它根本不起作用,我想要它。我只是将这些“模拟”帖子作为空间填充物。

任何帮助或想法将不胜感激!

我的结构

struct PhotoPost {
    let numberOfComments: Int
    let username: String
    let timestampName: String
    let userImageName: String
    let postImageName: String
    let postID: String
}

struct TextPost {
    let numberOfComments: Int
    let username: String
    let timestampName: String
    let userImageName: String
    let textName: String
    let postID: String
}

struct MixedPhoto {
    let numberOfComments: Int
    let username: String
    let timestampName: String
    let userImageName: String
    let textName: String
    let postImageName: String
    let postID: String
}

这是我的错误: 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

标签: iosswiftuitableview

解决方案


每个实例UITableView都有自己的单元池,因此从一个单元“窃取”一个单元实例UITableView并将其放入另一个单元是不正确的。此外,据我所知,您已经有了一种方便的方法来为您的单元格提供数据,并将相应的类型出列。因此,这里唯一剩下的就是从MainFeed您的tableView(_: didSelectRowAt:)函数下传递所需的数据,如下所示:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    guard let vc = storyboard.instantiateViewController(withIdentifier: "commentsVC") as? CommentsViewController else {
       return
    }
    switch indexPath.section {
        case 0:
            vc.mixedData = mixed[indexPath.row]
        case 1:
            vc.photoData = photos[indexPath.row]
        default:
            vc.textData = texttt[indexPath.row]
    }
    vc.modalPresentationStyle = .fullScreen

    navigationController?.pushViewController(vc, animated: true)
}

然后,在CommentsViewController'stableView(_:, cellForRowAt:)函数下,实现与你在 's 中所做的几乎相同的事情MainFeed

var mixedData: MixedPhoto?
var photoData: PhotoPost?
var textData: TextPost?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell: UITableViewCell
        switch (mixedData, photoData, textData) {
        case (.some(let value), _, _):
            cell = tableView.dequeueReusableCell(withIdentifier: mixedTableViewCell.identifier, for: indexPath) as! mixedTableViewCell
            cell.configure(with: value)
        case (_, .some(let value), _):
            cell = tableView.dequeueReusableCell(withIdentifier: popularTableViewCell.identifier, for: indexPath) as! popularTableViewCell
            cell.configure(with: value)
        case (_, _, .some(let value)):
            cell = tableView.dequeueReusableCell(withIdentifier: textTableViewCell.identifier, for: indexPath) as! textTableViewCell
            cell.configure(with: value)
        default:
            fatalError("The data is not set")
        }
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: textComTableViewCell.identifier, for: indexPath) as! textComTableViewCell
        return cell
    }
}

另外我应该说,为您的数据类型实现一个通用协议是一个好主意,因此您实际上可以在 in 中定义一个非可选变量CommentsViewController而不是三个可选变量。


推荐阅读