简体   繁体   English

TableView数据使用相同的UIVIewController传递到另一个tableview中?

[英]TableView data pass into another tableview with same UIVIewController?

I want to pass first tableview data into second tableview with same viewController when didSelectRowAt on first tableview. 我想在第一个表视图上执行didSelectRowAt时,使用相同的viewController将第一个表视图数据传递到第二个表视图中。

var bidCreatedByBankerArray : [BidCreatedByBanker]? = nil
var tableData = [String]()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if tableView == tableView1{
        return (bidCreatedByBankerArray?.count)!
    }else{
        return (tableData.count)
    }  
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if (tableView == self.tableView1) {
        let cell = tableView1.dequeueReusableCell(withIdentifier: "NumberOfBidsForBorrowerTableViewCell1ID") as? NumberOfBidsForBorrowerTableViewCell1
        let bidCreatedByBankerData = bidCreatedByBankerArray?[indexPath.row]
        cell?.projectDescriptionLabel.text = bidCreatedByBankerData?.projectDescription
        return cell!
    }else {
        let cell = tableView1.dequeueReusableCell(withIdentifier: "NumberOfBidsForBorrowerTableViewCell2ID") as? NumberOfBidsForBorrowerTableViewCell2
        let bidCreatedByBankerData = tableData[indexPath.row]
        return cell!
    }
}

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

    let cell = tableView.cellForRow(at: indexPath) as? NumberOfBidsForBorrowerTableViewCell2
}

for me it's showing empty details when click on first tableview. 对我来说,单击第一个表格视图时显示的是空的详细信息。

Thanks in advance 提前致谢

your question seems a bit unclear. 您的问题似乎还不清楚。 But I'm having a few assumptions here: 但是我在这里有一些假设:

  • Your both table views are in a same view controller. 两个表视图都在同一个视图控制器中。
  • Passing data between views is meaning here to be linked with cell of your tableview as this is where all the data is placed or suppose it resides in your cell class object. 在视图之间传递数据的意思是将其与tableview的单元格链接,因为这是所有数据的放置位置或假定它位于您的单元格类对象中。

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) as? NumberOfBidsForBorrowerTableViewCell2 // getting cell model object from controller instance array let bankerBid = bidCreatedByBankerArray?[indexPath.row] // assuming your data is in cell let data = cell?.dataNeeded // updating second table data tableData[indexPath.row] = bankerBid if tableView != tableView1 { tableView.reloadData() } } 

If this isn't what you're asking for comment down below I'm happy to help! 如果这不是您要在下方发表评论的地方,我们很乐意为您提供帮助!

var bidCreatedByBankerArray : [BidCreatedByBanker] = [BidCreatedByBanker]()
var tableData : [BidCreatedByBanker] = [BidCreatedByBanker]()

@IBOutlet weak var tableView1: UITableView!
@IBOutlet weak var tableView2: UITableView!

extension NumberOfBidForBorrowerViewController : UITableViewDelegate, UITableViewDataSource{

    //MARK: - TableView

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if tableView == tableView1{
            return (bidCreatedByBankerArray.count)
        }else{
            return (tableData.count)
        }
    }

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

        if (tableView == self.tableView1) {
            let cell = tableView1.dequeueReusableCell(withIdentifier: "NumberOfBidsForBorrowerTableViewCell1ID") as? NumberOfBidsForBorrowerTableViewCell1
            let bidCreatedByBankerData = bidCreatedByBankerArray[indexPath.row]
            cell?.projectDescriptionLabel.text = bidCreatedByBankerData.projectDescription
            return cell!
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "NumberOfBidsForBorrowerTableViewCell2ID", for: indexPath) as! NumberOfBidsForBorrowerTableViewCell2
            cell.selectionStyle = .default  
            let bidCreatedByBankerData = tableData[indexPath.row]

            cell.cityNameLabel.text = bidCreatedByBankerData.cityName
            return cell
        }
    }

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

        tableView2.reloadData()
        if (tableView == self.tableView1) {
            tableView2.isHidden = false
            let bidCreatedByBankerData = bidCreatedByBankerArray[indexPath.row]
            tableData.removeAll()
            tableData.append(bidCreatedByBankerData)
            tableView2.reloadData()
        }
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM