ios - 在uitableview中识别许多单元格

标签 ios swift didselectrowatindexpath uitableview

我想在 TableView 中识别许多自定义单元格“我在 Storyboard中构建它们”,但错误要求我返回值,我试图返回 nil 和 int 值和单元格,但错误是相同的

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->  UITableViewCell {
    if indexPath == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("CheefsCell", forIndexPath: indexPath) as UITableViewCell
            cell.accessoryType = .DisclosureIndicator
        return cell }
    else if indexPath == 1 {

        let cell = tableView.dequeueReusableCellWithIdentifier("BeautyCell", forIndexPath: indexPath) as UITableViewCell
            cell.accessoryType = .DisclosureIndicator
            return cell }
    else if indexPath == 2 {
        let cell = tableView.dequeueReusableCellWithIdentifier("StudentServicesCell", forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = .DisclosureIndicator
        return cell }
    else if indexPath == 3 {
        let cell = tableView.dequeueReusableCellWithIdentifier("ArtAndDesigneCell", forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = .DisclosureIndicator
        return cell }
    else if indexPath == 4 {
        let cell = tableView.dequeueReusableCellWithIdentifier("StoreCell", forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = .DisclosureIndicator
        return cell }
    else if indexPath == 5 {
        let cell = tableView.dequeueReusableCellWithIdentifier("OthersCell", forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = .DisclosureIndicator
        return cell }
    return

}

更新:: git hub link /

最佳答案

方法cellForRowAtIndexPath需要返回一个非可选的UITableViewCell,因此您必须确保在任何情况下都返回一个单元格。不允许使用简单的 returnreturn nil

代码可以精简,大部分是多余的。唯一的区别是标识符。

一个合适的解决方案是在索引路径的 row 属性上使用 switch 语句。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->  UITableViewCell {

  var identifier : String
  switch indexPath.row {
  case 0: identifier = "CheefsCell"
  case 1: identifier = "BeautyCell"
  case 2: identifier = "StudentServicesCell"
  case 3: identifier = "ArtAndDesigneCell"
  case 4: identifier = "StoreCell"
  default:  identifier = "OthersCell"
  }
  let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
  cell.accessoryType = .DisclosureIndicator
  return cell
}

关于ios - 在uitableview中识别许多单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35663246/

相关文章:

ios - idleTimer 时间到期后应用程序未休眠

swift - Swift 有文档生成支持吗?

ios - 选择表格中的单元格无法正常工作

ios - 为不同的行调用不同的方法

iOS Container View 和 Parent ViewController 通信?

ios - 使用 Parse 时关闭 iOS 中的登录屏幕

ios - FacebookShare 0.3.2 Swift ShareDialog 控制台错误

ios - 在 ios 9.1 中运行我的旧项目时显示此错误。iPhoneOS9.1.sdk/usr/lib/libsqlite3.dylib (没有这样的文件或目录)

ios - 找出单击单元格的坐标

ios - didSelectRowAtIndexPath 没有被调用?