【问题标题】:How to select the cell in the section of aTableView in swift如何快速选择aTableView部分中的单元格
【发布时间】:2019-05-22 21:15:58
【问题描述】:

我正在处理情节提要的表格视图中的部分,在该部分中选择一个单元格将引导我进入另一个屏幕。我设法做到了,但我觉得我使用了太多代码,必须找到更好的开发方法,我看过类似的出版物,他们试图做同样的事情,here我找到了这个答案

部分和部分中的项目数量会改变吗?如果没有,创建静态单元格并将每个单元格的转场连接到不同的目的地都可以在没有代码的情况下完成(全部在 Interface Builder 中)。

这正是我的情况,我有兴趣做这种形式。我有固定的数据,不会改变。现在我尝试以最好的方式开发这个功能,而不是像我目前拥有的那样。 我必须强调,我是 swift 开发的初学者。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.section {
        case 0:
            switch indexPath.row{
            case 0:
                self.performSegue(withIdentifier: "goToCreateAccountViewController", sender: nil)
                break
            case 1:
                break
            case 2:
                break
            default:
                break
            }
            break

        case 1:
            switch indexPath.row{
            case 0:
                break
            case 1:
                break
            case 2:
                break
            default:
                break
            }
            break

        case 2:
            break

        case 3:

            break

        default:
            break
        }
    }

cellForRow :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "item_option")
        cell.textLabel?.text = options[indexPath.section][indexPath.row]
        cell.imageView?.image = UIImage(named: "icon-myprofile")
        cell.textLabel?.font = cell.textLabel?.font.withSize(14)
        return cell
    }

【问题讨论】:

  • 能贴出cellForRow后面的代码吗?
  • 我认为这不是一个好方法。即使您在情节提要中执行此操作,也将很难维护。您的数据现在没有变化,但在不久的将来可能会发生变化。我认为如果你了解一下 Delegates 并这样做,对你会更好。
  • @iDevid 用请求的方法更新我的问题

标签: swift uitableview interface-builder uistoryboardsegue uitableviewsectionheader


【解决方案1】:

您可以将那个巨大的 switch 缩短为

switch (indexPath.section, indexPath.row) {
    case (0,0):
        self.performSegue(withIdentifier: "goToCreateAccountViewController", sender: nil)
    default:
        break
}

【讨论】:

    【解决方案2】:

    我认为您可以使用这样的情节提要:

    首先将视图控制器和单元格添加到 UITableView,然后将 TableView 内容设置设置为右上角的Static Cells。之后只需添加转场:

    【讨论】:

      【解决方案3】:

      如果您有 multiple cases 来处理,请使用 switch 语句。

      但从您的代码中可以明显看出,您只是在处理single condition。在这种情况下,我认为不需要使用 switch statement

      您可以轻松使用简单的if statement 来实现该功能。

      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          if indexPath.section == 0 && indexPath.row == 0  {
              self.performSegue(withIdentifier: "goToCreateAccountViewController", sender: nil)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-10-09
        • 1970-01-01
        • 2018-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-24
        • 1970-01-01
        • 2016-06-29
        相关资源
        最近更新 更多