Mixed use of static cells and dynamic cells

Regarding the mixed use of static cells and dynamic cells, there are many related articles on google, and here are the notes I made after reading the experience of some predecessors (I forgot which article I got the help from).

In some interfaces, the mixed use of static cells and dynamic cells will do more with less, such as Wi-Fifunctions on mobile phones, etc. The renderings are as follows:

1.png

Wi-FiThe number of rows in the first and third groups of the interface is fixed, and the layout is not the same. Such a layout can be completed quickly using static cells, but the second group needs to use dynamic cells. At this time, the mixed use of static cells and dynamic cells will be used.

First, add a UITableViewController to the Storyboard, set it as Static cell, and set the content of the first and third groups. The second group needs to set an empty Cell, as shown in the figure:

2.png

Then, set the identifier value to the blank Cell of the second group, as shown in the figure:

3.png

Then, the second group needs to customize the cell, which is done using XIB, as shown in the figure:

4.png

Next, you need to register the Nib with code, as follows:

1
2
let nib = UINib(nibName:  "WiFiTableViewCell" , bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier:  "WiFiTableViewCell" )

Finally, in order to mix static cells and dynamic cells, it is necessary to implement the proxy of TableView. The code is as follows:

  // The following proxy methods must be implemented
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  if indexPath.section == 1 {
    let cell = tableView.dequeueReusableCellWithIdentifier("WiFiTableViewCell") as? WiFiTableViewCell
    return cell!
  }
  return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  if section == 1 {
    return 10 //here returns the number of rows in the second group
  }
  return super.tableView(tableView, numberOfRowsInSection: section)
}

override func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
  if indexPath.section == 1 {
    return super.tableView(tableView, indentationLevelForRowAtIndexPath: NSIndexPath(forRow: 0, inSection: 1))
  }
  return super.tableView(tableView, indentationLevelForRowAtIndexPath: indexPath)
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  if indexPath.section == 1 {
    return 44
  }
  return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

It is worth noting that:

  • In the storyboard, you need to set an empty cell for the second group and set the identifier value.

  • Cell also needs to be registered in the code

  • The proxy methods mentioned above must be implemented

Finally, this article mainly records the mixed use of static cells and dynamic cells, so many details have not been dealt with. For more information on other uses of static cells, you can click here [Use Static Cell More Elegantly]

【Demo download】

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326528445&siteId=291194637