Swift 3:如何点击单元格内的按钮并传递该单元格的indexPath?

Jim*_*mmy 2 uitableview uicollectionview uicollectionviewcell swift swift3

我正在以编程方式进行所有操作,避免使用情节提要。我有一个collectionView,一个Post拥有用户名和密码的模型对象,当然我有collectionView单元格。我遇到的问题是,我的collectionView单元格内部有一个按钮。我希望能够点击此按钮并选择一个新的控制器。这是简单的部分。问题在于,我不仅要检入新控制器,还要传递我点击的按钮的特定Post Cell的特定用户名。我希望这是有道理的。基本上,我想拥有一个帖子,就像任何社交媒体应用程序一样,我想选择一个“个人资料”,并且还将该特定用户的用户名也传递给该个人资料控制器。希望您能理解。由于我在此问题上停留了一段时间,因此将给予任何帮助,我们将不胜感激。我当然会标记为答案。感谢大伙们...

class MyController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

var posts = [Post]()

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = .white
    collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: "cellId")
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return posts.count
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 50)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CustomCell

    cell.post = posts[indexPath.item]
    cell.homeController = self

    return cell
}

class CustomCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    var homeController: MyController?

    var post: Post? {
        didSet {
            if let username = post?.username {
                usernameLabel.text = username
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .blue
        setupViews()
    }

    lazy var myButton: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .red
        btn.setTitle("Push", for: .normal)
        btn.isUserInteractionEnabled = false
        btn.addTarget(self, action: #selector(handleTapped), for: .touchUpInside)
        return btn
    }()

    let usernameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .white
        return label
    }()

    func handleTapped() {
        let postController = PostController()
        homeController?.navigationController?.pushViewController(postController, animated: true)
    }

    func setupViews() {
        addSubview(myButton)

        myButton.frame = CGRect(x: (self.frame.width / 2) - 25, y: (self.frame.height / 2) - 25, width: 50, height: 50)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Run Code Online (Sandbox Code Playgroud)

Yan*_*ick 5

建议您告诉您UIViewController内部的按钮CustomCell已被单击。您可以添加一个协议,delegate向您的单元格添加一个属性,然后MyController必须符合该协议。该函数还应该传入已单击的单元格,您可以从中获取该indexPath单元格的collectionView

protocol CustomCellDelegate: class {
    func didSelectButton(in cell: CustomCell)
}

class CustomClass: UICollectionViewClass {
    weak var delegate: CustomCelLDelegate?
    func handleTapped() {
        delegate?.didSelectButton(in: self)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的MyController类中,添加委托并遵守协议。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CustomCell

    cell.post = posts[indexPath.item]
    cell.delegate = self

    return cell
}

extension MyController: CustomCellDelegate {
    func didSelectButton(in cell: CustomCell) {
        if let indexPath = collectionView.indexPath(for: cell) {
            let postController = PostController()
            let post = posts[indexPath.row]
            postController.post = post //pass the data
            self.navigationController?.pushViewController(postController, animated: true)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您还需要在中添加一个post变量PostController

class PostController {
    var post: Post!
}
Run Code Online (Sandbox Code Playgroud)