簡體   English   中英

如何使用兩個自定義 UITableViewCell 在一個視圖控制器中創建兩個表視圖?

[英]How do I create two table views in one view controller with two custom UITableViewCells?

我試圖創建兩個UITableViews使用兩個自定義在一個視圖控制器UITableViewCells 我有以下幾點:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if tableView == self.tableView {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomOne") as! CustomOneTableViewCell
        return cell
    }

    if tableView == self.autoSuggestTableView {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomTwo") as! CustomTwoTableViewCell
        return cell
    }
}

但我不斷收到錯誤消息:

Missing return in a function expected to return 'UITableViewCell'

在方法結束時我必須返回什么?

出現錯誤是因為如果出於任何原因,表視圖不是您編寫的兩個選項,那么它沒有任何返回值,只需在最后添加一個默認值:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == firstTableView,
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomOne") as? CustomOneTableViewCell {
        return cell
    } else if tableView == autoSuggestTableView,
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTwo") as? CustomTwoTableViewCell {
        return cell
    }

    return UITableViewCell()
}

已更新至4.1.2迅速:我已經更新了這個答案版本4.1.2 ,同時,因為return value的方法不能nil ,修改為默認情況下,虛擬UITableViewCell

您的問題是編譯器會考慮if語句都可能為假並且在這種情況下您不返回任何內容的可能性,因此會出現錯誤。

如果您只有兩個表,最簡單的更改是:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if tableView == self.tableView {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomOne") as! CustomOneTableViewCell
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomTwo") as! CustomTwoTableViewCell
        return cell
    }
}

我對這個問題的首選解決方案是執行以下操作:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cellToReturn = UITableViewCell() // Dummy value
    if tableView == self.tableView {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomOne") as! CustomOneTableViewCell
        cellToReturn = cell
    } else if tableView == self.autoSuggestTableView {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomTwo") as! CustomTwoTableViewCell
        cellToReturn = cell
    }

    return cellToReturn
}

我認為這種方法保持了可讀性和清晰度,同時也解決了錯誤。 我不喜歡只為了兼容性而編寫(危險的)代碼,例如return nil

如果您在 ViewController 中嘗試使用兩個或兩個以上的表,那么您必須在所有委托和數據源方法中識別 tableView。 例如

extension ViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableView == firstTableView ? first.count: second.count
        //return second.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var returnCell = UITableViewCell()
        if tableView == firstTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "firstCell", for: indexPath)
            cell.textLabel?.text = first[indexPath.row]
            returnCell = cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "secondCell", for: indexPath)
            cell.textLabel?.text = second[indexPath.row]
            returnCell = cell
        }
        return returnCell
    }
}

基於 Fantini 的回答,我建議使用 switch 語句來清理一下:

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

  switch tableView {

    case firstTableView:
      let cell = tableView.dequeueReusableCell(withIdentifier: "CustomOne") as? CustomOneTableViewCell {
      return cell

    case autoSuggestTableView:
      let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTwo") as? CustomTwoTableViewCell {
      return cell

    default:
      return UITableViewCell()
  }
}

以防萬一您打算稍后添加更多 tableView。

導入 UIKit

類視圖控制器: UIViewController , UITableViewDelegate , UITableViewDataSource {

@IBOutlet weak var topTableView: UITableView!
@IBOutlet weak var downTableview: UITableView!
var topData : [String] = []
var downData = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    topTableView.delegate = self
    downTableview.delegate = self
    topTableView.dataSource = self
    downTableview.dataSource = self

    for index in 0...20 {
        topData.append("Top Table Row \(index)")
    }

    for index in 10...45 {
        downData.append("Down Table Row \(index)")
    }

}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var numberOfRow = 1
    switch tableView {
    case topTableView:
        numberOfRow = topData.count
    case downTableview:
        numberOfRow = downData.count
    default:
        print("Some things Wrong!!")
    }
    return numberOfRow
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = UITableViewCell()
    switch tableView {
    case topTableView:
        cell = tableView.dequeueReusableCell(withIdentifier: "topCell", for: indexPath)
        cell.textLabel?.text = topData[indexPath.row]
        cell.backgroundColor = UIColor.green
    case downTableview:
        cell = tableView.dequeueReusableCell(withIdentifier: "downCell", for: indexPath)
        cell.textLabel?.text = downData[indexPath.row]
        cell.backgroundColor = UIColor.yellow
    default:
        print("Some things Wrong!!")
    }
    return cell
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM