簡體   English   中英

TableView數據使用相同的UIVIewController傳遞到另一個tableview中?

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

我想在第一個表視圖上執行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
}

對我來說,單擊第一個表格視圖時顯示的是空的詳細信息。

提前致謝

您的問題似乎還不清楚。 但是我在這里有一些假設:

  • 兩個表視圖都在同一個視圖控制器中。
  • 在視圖之間傳遞數據的意思是將其與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() } } 

如果這不是您要在下方發表評論的地方,我們很樂意為您提供幫助!

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