如何使用Swift 3正确实现静态单元格

时间:2022-09-02 10:58:59

I literally could not find a single tutorial that showed me how to build an app that uses static cells; with clickable cells. Based on few out dated posted and object-c answers, I've put something together. My issue is, when I click on a cell, I get staticDemoTableCell is has no member present.

我实际上找不到一个教程,告诉我如何构建一个使用静态单元格的应用程序;可点击的单元格。基于几个过时的发布和对象-c答案,我把一些东西放在一起。我的问题是,当我点击一个单元格时,我得到staticDemoTableCell没有成员存在。

I have embedded a Table Controller in my UIViewController. For that cell (only one so far), I've created a class:

我在UIViewController中嵌入了一个表控制器。对于那个单元格(目前只有一个),我创建了一个类:

class staticDemoTableCell: UITableViewCell, UITableViewDelegate {
  @IBOutlet weak var tableView: UITableView! 

  override func awakeFromNib() {
    [...]
    tableView.delegate = self
  }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("clicked") // Works when the cell is clicked
    // self.present() do not work. I need to present another viewcontroller when this cell is clicked
  }

}

Something does not sit right, for every cell is a class?

有些东西不合适,因为每个单元都是一个类?

I really need to know if I went the correct way about doing this. What I really want is more to this such. Have you seen ie: grouped transactions Monday: a list, Tuesday: a list etc. Each cell will be clickable just like the settings of your iOS device. Any pointers will be highly grateful.

我真的需要知道我是否采用了正确的方法来做到这一点。我真正想要的更多是这样的。你见过ie:分组交易星期一:列表,星期二:列表等。每个单元格都可以点击,就像iOS设备的设置一样。任何指针都将非常感激。

2 个解决方案

#1


8  

It's much easier if the table view contains only static cells:

如果表视图仅包含静态单元格,则会更容易:

  • In Interface Builder select the table view and set the Content to Static Cells
  • 在Interface Builder中,选择表视图并将Content设置为Static Cells

  • Create IBOutlets and IBActions in the controller class and connect them.
  • 在控制器类中创建IBOutlets和IBActions并连接它们。

  • Implementing table view data source methods and subclassing cells is not needed.
  • 不需要实现表视图数据源方法和子类化单元。

#2


7  

So you can do the following. Set your table view, then make the cells static, once you've done that, you need to make sure you know how many sections you are going to have. This depends on your design or what you want to achieve.

所以你可以做到以下几点。设置表格视图,然后使单元格保持静态,一旦完成,您需要确保知道将要有多少部分。这取决于您的设计或您想要实现的目标。

Then you can do something like this:

然后你可以做这样的事情:

If you have more than one section, and section 1 has more than one cell. And section 2 has only one cell.

如果您有多个部分,并且部分1有多个单元格。第2节只有一个单元格。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.section {
    case 1:
        switch indexPath.row {
        case 0:
            // Do something
        case 1:
            // Do something 
        default:
            break
        }
    case 2:
        // Do something
    default:
        break
    }
}

If you have only one section with two cells you can do something like:

如果您只有一个包含两个单元格的部分,则可以执行以下操作:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.row {
    case 0:
        // Do something
    case 1:
        // Do something
    default:
        break
    }
}

I hope this helps to solve your problem. Good luck

我希望这有助于解决您的问题。祝好运

#1


8  

It's much easier if the table view contains only static cells:

如果表视图仅包含静态单元格,则会更容易:

  • In Interface Builder select the table view and set the Content to Static Cells
  • 在Interface Builder中,选择表视图并将Content设置为Static Cells

  • Create IBOutlets and IBActions in the controller class and connect them.
  • 在控制器类中创建IBOutlets和IBActions并连接它们。

  • Implementing table view data source methods and subclassing cells is not needed.
  • 不需要实现表视图数据源方法和子类化单元。

#2


7  

So you can do the following. Set your table view, then make the cells static, once you've done that, you need to make sure you know how many sections you are going to have. This depends on your design or what you want to achieve.

所以你可以做到以下几点。设置表格视图,然后使单元格保持静态,一旦完成,您需要确保知道将要有多少部分。这取决于您的设计或您想要实现的目标。

Then you can do something like this:

然后你可以做这样的事情:

If you have more than one section, and section 1 has more than one cell. And section 2 has only one cell.

如果您有多个部分,并且部分1有多个单元格。第2节只有一个单元格。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.section {
    case 1:
        switch indexPath.row {
        case 0:
            // Do something
        case 1:
            // Do something 
        default:
            break
        }
    case 2:
        // Do something
    default:
        break
    }
}

If you have only one section with two cells you can do something like:

如果您只有一个包含两个单元格的部分,则可以执行以下操作:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.row {
    case 0:
        // Do something
    case 1:
        // Do something
    default:
        break
    }
}

I hope this helps to solve your problem. Good luck

我希望这有助于解决您的问题。祝好运