迟到的Swift入门 - UITableView

2020-09-28  本文已影响0人  刘_小_二

UITableView入门操作

1.基础操作

1.0 初始化UITableView

// 懒加载
lazy var tableView : UITableView = {
  let tableView = UITableView(frame: view.bounds, style: .grouped)
  tableView.delegate = self
  tableView.dataSource = self
  return tableView
}()
let tableView = UITableView(frame: view.bounds, style: .grouped)
tableView.dataSource = self
view.addSubview(tableView)

滚动到底部

使用scrollToRow方法滚动到最后一行

let secon = 1 //最后一个分组的索引(0开始,如果没有分组则为0)
let rows = 10 //最后一个分组最后一条项目的索引
let indexPath = IndexPath(row: rows, section: secon)
self.tableView?.scrollToRow(at: indexPath, at:.bottom, animated: true)

使用setContentOffset设置偏移量实现滚动:

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

1.1 协议方法

1.1.0 协议 - UITableViewDelegate
// 方式一
extension LZTableVC : UITableViewDelegate {
    // 当用户点击选择指定索引位置的单元格时,调用此方法。
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
    }
    // 设置单元格高度,每当表格需要显示时,都会调用此方法。
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 74.0
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
}
1.1.1 协议 - UITableViewDataSource
方式一:

extension LZTableVC : UITableViewDataSource {
    
    // 设置表格中的章节(section)个数。
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSource.count
    }
    
    // 设置某一章节(section)中的单元格数量,必须实现。
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
//     初始化和复用指定索引位置的UITableViewCell,必须实现。
    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 = "这个是标题"
        cell?.detailTextLabel?.text = "这里是内容了油"
        cell?.imageView?.image = UIImage(named: "icon")
        return cell!
    }
}

1.2 UITableViewCell基本操作

1.2.0 基本Cell设置
//MARK:初始化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!
    }
1.2.1 Cell自定义
import UIKit

class AutoUITableViewCell: UITableViewCell {

    let width:CGFloat = UIScreen.main.bounds.width
    var userLabel:UILabel! // 名字
    var brirthdayLabel:UILabel!//出事日期
    var sexLabel:UILabel! //性别
    var iconImv:UIImageView! //头像
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 头像
        iconImv = UIImageView(frame: CGRect(x: 20, y: 15, width: 44, height: 44))
        iconImv.layer.masksToBounds = true
        iconImv.layer.cornerRadius = 22.0
        
        // 名字
        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
        
        // 性别
        sexLabel = UILabel(frame: CGRect(x: 150, y: 20, width: 50, height: 13))
        sexLabel.textColor = .black
        sexLabel.font = UIFont.systemFont(ofSize: 13)
        sexLabel.textAlignment = .left
        
        //出生日期
        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() {
        super.awakeFromNib()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }

}
上一篇下一篇

猜你喜欢

热点阅读