ios - 如何按下 tableview 单元格以在导航 Controller 中显示带有文本的 View Controller

标签 ios swift uitableview

本质上,我有一个名为 FirstViewController 的 View Controller ,该 View Controller 中包含一个名为 listTableView 的 TableView 。

我想点击 TableView listTableView 中的一个单元格,然后将单元格中的任何文本显示为导航 Controller 标题。

点击单元格时出现的导航 Controller 称为 showDetailsViewController

如何做到这一点?

下面是我在FirstViewController

中写的
import UIKit
import AudioToolbox

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, FeedModelProtocol  {
    var feedItems: NSArray = NSArray()
    var selectedStock : StockModel = StockModel()
    let tableView = UITableView()
    @IBOutlet weak var listTableView: UITableView!
    @IBOutlet weak var refreshButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        //set delegates and initialize FeedModel


        self.listTableView.delegate = self
        self.listTableView.dataSource = self

        let feedModel = FeedModel()
        feedModel.delegate = self
        feedModel.downloadItems()

    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    @IBAction func reloadData(_ sender: Any) {

        print("reload pressed")
        listTableView.reloadData()
        viewDidLoad()
        _ = AudioServicesPlaySystemSound(1519)
    }



    func itemsDownloaded(items: NSArray) {

        feedItems = items
        self.listTableView.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of feed items

        print("item feed loaded")
        return feedItems.count

    }

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

        // Retrieve cell
        let cellIdentifier: String = "stockCell"
        let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
        myCell.textLabel?.textAlignment = .center
        myCell.textLabel?.font = .boldSystemFont(ofSize: 18)
        // Get the stock to be shown
        let item: StockModel = feedItems[indexPath.row] as! StockModel
        // Configure our cell title made up of name and price


        let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")


        print(titleStr)
        // Get references to labels of cell
        myCell.textLabel!.text = titleStr

        return myCell
    }

}

更新:

这段代码有什么问题:

注意: tableview的恢复id是scheduleTable

var homeworkIdentifierFromTableViewCell = ""
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        homeworkIdentifierFromTableViewCell = feedItems[indexPath.row].myCell
        self.performSegue(withIdentifier: "scheduleTable", sender: self)
        listTableView.deselectRow(at: indexPath, animated: true)

    }

更新 2

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let item: StockModel = feedItems[indexPath.row] as! StockModel
    let titleStr = [item.customer].compactMap { $0 }.joined(separator: "-")
    print(titleStr)


}

最佳答案

您可以使用 didSelectRowAt 来注意单击了哪个单元格并存储单元格中的文本(homeworkArray 是结构中的单元格列表。Homeworkidentifier 是结构中的值)。

var homeworkIdentifierFromTableViewCell = ""
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        homeworkIdentifierFromTableViewCell = homeworkArray[indexPath.row].homeworkIdentifier

        self.performSegue(withIdentifier: "homeworktoExpandHomework", sender: self)
        homeworkTableView.deselectRow(at: indexPath, animated: true)

    }

然后,您可以使用 prepare for a segue 函数将 TableView 单元格的文本传递到下一个 View Controller 。为此,您可以在另一个 View Controller (您要将数据传递到的 View Controller )中创建一个变量,然后从另一个 View Controller 访问它并更改其值。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "reportBug" {
            let destinationViewController = segue.destination as! WebViewController
            destinationViewController.reason = "reportBug"

        }
        else if segue.identifier == "provideFeedback" {
            let destinationViewController = segue.destination as! WebViewController
            destinationViewController.reason = "provideFeedback"
        }
    }

这里是关于在 View Controller 之间传递数据的更多信息:Passing data between View Controllers in Swift (From TableView to DetailViewController)

希望对你有帮助


编辑: 这是我正在使用的结构:

struct homeworkTableViewCellData {
    let homeworkName : String!
    let className : String!
    let dateName : String!
    let colorImage : UIImage!
    let homeworkIdentifier : String!
}

我已经用这个结构初始化了我的 homeworkArray。当我从单元格中调用一个值时,我是从结构中选择一个值。

用struct设置table view更有条理。这是一个很好的视频,可以教您如何设置它(如果您想这样做的话):https://www.youtube.com/watch?v=zAWO9rldyUE&list=LL--UalPCi7F16WzDFhMEg7w&index=20&t=921s

关于ios - 如何按下 tableview 单元格以在导航 Controller 中显示带有文本的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55754036/

相关文章:

ios - 隐藏包含 UITextview 的 UITableviewcell(自动布局)

ios - UITabBarController自动生成的自定义tableview

ios - 内部 Apple Watch 应用程序开发和部署?

xcode - 如何在 swift 中将闭包作为参数传递(只是基础知识)

ios - 崩溃 firebase Swift

swift - 如何创建图像数组,并使用draw()在Swift中将它们绘制出来?

ios - 是 insertSections :withRowAnimation: necessary? 的显式调用

ios - 如何使 CLProximityImmediate 的 iBeacon 前景测距在 iOS 中更快?

ios - 从函数内部访问常量或变量到 `viewdidload`以保存在firebase swift 3中

objective-c - 多次点击 uitableviewcell