简体   繁体   中英

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.

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.

     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()
        }
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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