Create a UITableView in viewDidLoad() :

Set the origin and size

  let myTableView = UITableView(frame: CGRect(x: 0, y: 20, 
  width: fullScreenSize.width, 
  height: fullScreenSize.height - 20), style: .grouped)
Copy the code

The style of the registered cell is the name

 myTableView.register(UITableViewCell.self,
 forCellReuseIdentifier: "Cell")
 myTableView.register(CollectionCell.self,
 forCellReuseIdentifier: "Collection") / /Copy the code

The register of the Quick Help

An error was reported when adding the cell again

Because the class definition was wrong, the new class was defined in the subclass

A modified version that has been compiled

People stupid to read more…….

Other Settings

/ / set to appoint myTableView object. The delegate = self myTableView. The dataSource = self. / / line style myTableView separatorStyle =. SingleLine The distance / / line four numerical respectively, left, down, right spacing myTableView. SeparatorInset = UIEdgeInsetsMake (0, 20, 0, 20) / / if you can click on the cell myTableView. AllowsSelection =true/ / if you can choose more cell myTableView. AllowsMultipleSelection =false// Add to the page self.view.addSubView (myTableView)Copy the code

Two things that must be defined in a tableView include:

Define how many cells a group has and what each cell shows

 func tableView(_ tableView: 
 UITableView, numberOfRowsInSection section: Int) -> Int {
        return info[section].count
    }
  
Copy the code

Where info is the array var variable defined

var info = [
        [Jeremy Lin."Sun on Chen"],
        ["Wei Yin Chen".Wang Chien-ming."Chen Jinfeng"."Lin Zhi Sheng"]]Copy the code

What each cell shows

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {// Get the cell currently used by tableViewlet cell:UITableViewCell!
     
        
        if indexPath.section == 0 && indexPath.row == 0 {
            let collectionCell = tableView.dequeueReusableCell(withIdentifier: "Collection".for: indexPath) as! CollectionCell
            collectionCell.setUp()
            
            collectionCell.cTextLabel.text = "hello"
            
            cell = collectionCell
        }else{
            cell = tableView.dequeueReusableCell(withIdentifier: "Cell".for: indexPath) as! UITableViewCell
Copy the code

New cell defined

DequeueReusableCell in Quick Help

The dequeueReusableCell compilation failed during recompilation

Because the class definition was wrong before that

Continue to set the Accessory button style and so on throughout the function

Func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> frowsinSection {// Func tableView: UITableView, numberOfRowsInSection section: Int) -> frowsinSectionreturnFunc tableView(tableView: UITableView, cellForRowAtIndexPath) indexPath: func tableView(tableView: UITableView, cellForRowAtIndexPath) NSIndexPath) -> UITableViewCell {// Get the cell that tableView is currently usinglet cell = 
      tableView.dequeueReusableCellWithIdentifier(
        "Cell".forIndexPath: IndexPath) as UITableViewCell // Set the Accessory button styleif indexPath.section == 1 {
        if indexPath.row == 0 {
            cell.accessoryType = .Checkmark
        } else if indexPath.row == 1 {
            cell.accessoryType = .DetailButton
        } else if indexPath.row == 2 {
            cell.accessoryType =
              .DetailDisclosureButton
        } else ifIndexPath. Row == 3 {cell.accessoryType =.disclosureIndicator}} // Display contentif let myLabel = cell.textLabel {
        myLabel.text = 
          "\(info[indexPath.section][indexPath.row])"
    }

    return cell
}
Copy the code

And the actions performed

Func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {// Remove cell selection state tableView.deselectRow(at: IndexPath, animated:true)
        
        let name = info[indexPath.section][indexPath.row]
        print("Select \(name)"AccessoryType func tableView(_ tableView:) accessoryType func tableView UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {let name = info[indexPath.section][indexPath.row]
        print("Press the detail of \(name)"} // There are several sections func numberOfSections(in tableView: UITableView) -> Int {
        returnInfo.count} // The title of each section func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {let title = section == 0 ? "Basketball" : "Baseball"
        return title
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return200} // Set the widthCopy the code