UITableView entry operation

1. Basic operations

Initialize UITableView

/ / lazy loading
lazy var tableView : UITableView = {
  let tableView = UITableView(frame: view.bounds, style: .grouped)
  tableView.delegate = self
  tableView.dataSource = self
  return tableView
}()
Copy the code
let tableView = UITableView(frame: view.bounds, style: .grouped)
tableView.dataSource = self
view.addSubview(tableView)
Copy the code

Roll to the bottom

Use the scrollToRow method to scroll to the last line

let secon = 1 // Index of the last group (starting at 0, or 0 if there is no group)
let rows = 10 // The index of the last item in the last group
let indexPath = IndexPath(row: rows, section: secon)
self.tableView?.scrollToRow(at: indexPath, at:.bottom, animated: true)
Copy the code

Use setContentOffset to set offsets for scrolling:

let offset = CGPoint(x:0, y:self.tableView!.contentSize.height
                                - self.tableView!.bounds.size.height)
self.tableView!.setContentOffset(offset, animated: true)
Copy the code

1.1 Protocol Methods

1.1.0 Protocol – UITableViewDelegate
  • tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath): sets the height of the cell and is called whenever the table needs to be displayed.
  • tableView(_ tableView: UITableView, heightForHeaderInSection section: Int): Sets the height of the section header under an index.
  • tableView(_ tableView: UITableView, heightForFooterInSection section: Int): Sets the height of the end of a chapter in an index.
  • tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath): Called when a cell at the specified index position is about to be displayed. This method gives the delegate object a chance to override its state properties, such as the background color, before the cell is displayed.
  • tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath): This method is called when the user clicks the cell that selects the specified index location.
  • tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath): Called when the user clicks on a cell that has already been selected.
/ / way
extension LZTableVC : UITableViewDelegate {
    // This method is called when the user clicks the cell that selects the specified index location.
    func tableView(_ tableView: UITableView.didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
    }
    // Sets the cell height, which is called whenever the table needs to be displayed.
    func tableView(_ tableView: UITableView.heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 74.0
    }
    func tableView(_ tableView: UITableView.heightForHeaderInSection section: Int) -> CGFloat {
        return 20}}Copy the code
1.1.1 Protocol – UITableViewDataSource
  • tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath): initializes and reuses the specified index locationUITableViewCell.Must be implemented.
  • tableView(_ tableView: UITableView, numberOfRowsInSection section: Int): Set a chapter (section),Must be implemented.
  • numberOfSections(in tableView: UITableView): Set the section in the table (sectionNumber).
  • tableView(_ tableView: UITableView, titleForHeaderInSection section: Int): Sets the title text of the specified section. If it is not set or the agent returns a value of nil, it will not be displayed.
  • tableView(_ tableView: UITableView, titleForFooterInSection section: Int): Sets chapter foot header text, if not set or the agent returns a value of nil, not displayed.
  • tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath): to set the specified index location in the tablecellWhether editable, editablecellInsert and delete ICONS are displayed.
  • tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath): This method is called when an insert or delete operation is completed.
  • tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath): of the specified index locationcellI wonder if I can change its position by dragging it.
  • tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath): whencellThis method is called when you drag from one location to another.
A:extension LZTableVC : UITableViewDataSource {
    
    // Set the number of sections in the table.
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSource.count
    }
    
    // Set the number of cells in a section.
    func tableView(_ tableView: UITableView.numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
// Initialize and reuse the UITableViewCell with the specified index position, must be implemented.
    func tableView(_ tableView: UITableView.cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellID = "testCellID"
        var cell = tableView.dequeueReusableCell(withIdentifier:cellID)
        if cell = = nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellID)
        }
        cell?.textLabel?.text = "This is the title."
        cell?.detailTextLabel?.text = "Here is the content of the oil."
        cell?.imageView?.image = UIImage(named: "icon")
        return cell!}}Copy the code

1.2 Basic Operations of UITableViewCell

1.2.0 Basic Cell Settings
//MARK: initializes the Cell
    func tableView(_ tableView: UITableView.cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "indexsCellId")
        if cell = = nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "indexsCellId")}let arr = contents[keys[indexPath.section]]
        cell?.textLabel?.text = arr?[indexPath.row]
        return cell!
    }
Copy the code
1.2.1 Cell customization
import UIKit

class AutoUITableViewCell: UITableViewCell {

    let width:CGFloat = UIScreen.main.bounds.width
    var userLabel:UILabel! / / name
    var brirthdayLabel:UILabel!// Date of accident
    var sexLabel:UILabel! / / gender
    var iconImv:UIImageView! / / avatar
    
    
    override init(style: UITableViewCell.CellStyle.reuseIdentifier: String?). {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        / / avatar
        iconImv = UIImageView(frame: CGRect(x: 20, y: 15, width: 44, height: 44))
        iconImv.layer.masksToBounds = true
        iconImv.layer.cornerRadius = 22.0
        
        / / name
        userLabel = UILabel(frame: CGRect(x: 74, y: 18, width: 70, height: 15))
        userLabel.textColor = UIColor.black
        userLabel.font = UIFont.systemFont(ofSize: 15)
        userLabel.textAlignment = .left
        
        / / gender
        sexLabel = UILabel(frame: CGRect(x: 150, y: 20, width: 50, height: 13))
        sexLabel.textColor = .black
        sexLabel.font = UIFont.systemFont(ofSize: 13)
        sexLabel.textAlignment = .left
        
        // Date of birth
        brirthdayLabel = UILabel(frame: CGRect(x: 74, y: 49, width: width-94, height: 13))
        brirthdayLabel.textColor = .gray
        brirthdayLabel.font = UIFont.systemFont(ofSize: 13)
        brirthdayLabel.textAlignment = .left
        
        contentView.addSubview(iconImv)
        contentView.addSubview(userLabel)
        contentView.addSubview(sexLabel)
        contentView.addSubview(brirthdayLabel)
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")}override func awakeFromNib(a) {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool.animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state}}Copy the code