ios - 有没有办法获取 UITableViewCell 的 ID?

标签 ios swift uitableview

我的问题:如果用户在 TableView 单元格中按下按钮,我想打开某种配置文件。单元格数据是从 Parse 下载的。

这个想法基于 Instagram,如果您点击 Insta 上的用户名按钮,就会打开发布图像的用户的个人资料。我想创建相同的代码,但无法创建代码来获取用户。你能帮帮我吗?

这是一些代码:

import UIKit
import Parse
class HomeController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let reuseIdentifer = "FeedCell"
var delegate: HomeControllerDelegate?
var newCenterController: UIViewController!
let tableView = UITableView()
//Für Parse:
var users = [String: String]()
var comments = [String]()
var usernames = [String]()
var lastnames = [String]()
var imageFiles = [PFFileObject]()
var wischen: UISwipeGestureRecognizer!
var wischen2: UISwipeGestureRecognizer!
override func viewDidLoad() {
    super.viewDidLoad()
        view.backgroundColor = .white
        getData()
        configureNavigationBar()
        configurateTableView()

    wischen = UISwipeGestureRecognizer()
    wischen.addTarget(self, action: #selector(handleMenuToggle))
    wischen.direction = .right
    wischen.numberOfTouchesRequired = 1
    view.addGestureRecognizer(wischen)

    wischen2 = UISwipeGestureRecognizer()
    wischen2.addTarget(self, action: #selector(handleMenuToggle))
    wischen2.direction = .left
    wischen2.numberOfTouchesRequired = 1
    view.addGestureRecognizer(wischen2)
}
@objc func handleMenuToggle() {
    delegate?.handleMenuToggle(forMenuOption: nil)
}

@objc func showProfile() {
    let vc: AProfileViewController!
    vc = AProfileViewController()
    vc.modalPresentationStyle = .fullScreen
    present(vc, animated: true)
}

func configureNavigationBar() {
    navigationController?.navigationBar.barTintColor = .darkGray
    navigationController?.navigationBar.barStyle = .black
    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Noteworthy", size: 22)!, NSAttributedString.Key.foregroundColor: UIColor.white]
    //navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

    navigationItem.title = "Mobile Job Board"
    navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_menu_white_3x").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleMenuToggle))
    navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_mail_outline_white_2x").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(showCreateNewArticle))
}
  //MARK: Table View

     //skiped table view configuration

    }
// - MARK: Table view data source

func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return comments.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifer, for: indexPath) as! FeedCell
        imageFiles[indexPath.row].getDataInBackground { (data, error) in
            if let imageData = data {
                if let imageToDisplay = UIImage(data: imageData) {
                    cell.postImage.image = imageToDisplay
                }
            }
        }
        cell.descriptionLabel.text = comments[indexPath.row]
        cell.userButton.setTitle("\(usernames[indexPath.row]) \(lastnames[indexPath.row])", for: UIControl.State.normal)
        cell.userButton.addTarget(self, action: #selector(showProfile), for: .touchUpInside)
        return cell
}
//skiped
}

非常感谢!

汤姆

最佳答案

这里的问题是您的按钮在选择器上工作,并且它不知道发送者或从哪里调用它。

我将通过创建一个自定义表格 View 单元格(例如 FeedCell)来实现此目的,它允许您设置一个委托(delegate)(例如 FeedCellDelegate) .将您的类设置为单元格的委托(delegate),并将其当前的 indexPath 传递到单元格中。然后,您可以在委托(delegate)调用中返回 indexPath

示例:请注意,为了简单起见,代码已被删除,并且该代码尚未经过测试。这只是为了引导您走向正确的方向。

View Controller

import UIKit

class HomeController: UIViewController {
    // stripped additional information for example

    func showProfile(_ username: String) {
        let vc: AProfileViewController!
        vc = AProfileViewController()
        vc.username = username
        vc.modalPresentationStyle = .fullScreen
        present(vc, animated: true)
    }
}

extension HomeController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return comments.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifer, for: indexPath) as! FeedCell

        cell.delegate = self

        cell.descriptionLabel.text = comments[indexPath.row]
        cell.userButton.setTitle("\(usernames[indexPath.row]) \(lastnames[indexPath.row])", for: UIControl.State.normal)

        cell.setIndex(indexPath)

        return cell
    }
}

extension HomeController: FeedCellDelegate {
    func didPressButton(_ indexPath: IndexPath) {
        let userName = usernames[indexPath.row]
        showProfile(username)
    }
}

Feed Cell

import UIKit

protocol FeedCellDelegate {
    didPressButton(_ indexPath: IndexPath)
}

class FeedCell: UICollectionViewCell {

    var delegate: FeedCellDelegate?
    var indexPath: IndexPath
    @IBOutlet weak var userButton: UIButton

    setIndex(_ indexPath: IndexPath) {
        self.indexPath = indexPath
    }

    @IBAction userButtonPressed() {

        if(delegate != nil) {
            delegate?.didPressButton(indexPath)
        }
    }
}

关于ios - 有没有办法获取 UITableViewCell 的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58964271/

相关文章:

swift - 将支持的方法限制为 Swift 4 中的泛型参数类型

ios - 删除行前的确认

ios - 在 iOS8 中选择/突出显示时 UITableViewCell 周围出现分隔线

java - 使用 Java 且没有第 3 方框架(iOS 客户端)的谷歌应用引擎自定义身份验证 - 设计合理吗?

Swift 3 - AES 加密 Heimdall - 零填充

swift - 自定义 Cell Segue 将数据发送到第二个 View Controller 不起作用

ios - RxSwift Observable 不会在数据更改时更新 TableView

iphone - 如何只加载网页的主体

ios - View 设计未显示在 ViewController 中

iphone - 即时创建 *.docx(也许还有 *.doc?)文档?