繁体   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