swift 自定义UITableview

使用swift创建tableview

  • ViewController
import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    private var tableView : UITableView? = nil
    private var listArray = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        // 加载数据
        addData()
        // 添加tableview
        addTableview()
    }
}

extension ViewController {
    private func addTableview () {
    tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height), style: .plain)
        tableView?.delegate = self
        tableView?.dataSource = self
        view.addSubview(tableView!)
        // 需要注册 cell
        tableView?.register(MainCell.self, forCellReuseIdentifier: "Identifier")
    }
}

// MARK -- delegate
extension ViewController {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listArray.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 180
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let Identifier = "Identifier"
        var cell : MainCell = tableView.dequeueReusableCell(withIdentifier: Identifier, for: indexPath) as! MainCell
        if cell.isEqual(nil) {
            cell = UITableViewCell(style: .default, reuseIdentifier: Identifier) as! MainCell
        }
        let dic = Dictionary<String, String>()
        cell.setValueForCell(dic: dic as NSDictionary)
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let nav = CollectionVC()
        navigationController?.pushViewController(nav, animated: true)
    }
}

// MARK -- addData
extension ViewController {
    private func addData() {
        listData { (listA) in
            // 拼接数据
            self.listArray += listA
            // 刷新列表
            self.tableView?.reloadData()
        }
    }
}

// MARK -- listData
extension ViewController {
    private func listData (completHander: @escaping(_ lists:[String]) -> ()) {
        var a = [NSString]()
        DispatchQueue.global().async {
            for i in 0..<20 {
                a.append(NSString(string: "\(i)"))
            }
            DispatchQueue.main.async {
                completHander(a as [String])
            }
        }

    }
}
  • MainCell
import UIKit

class MainCell: UITableViewCell {

    // 定义属性
    private var showImage: UIImageView?
    private var fristTitle : UILabel?
    private var subTitle : UILabel?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
}

extension MainCell {
    private func setupUI () {

      showImage = UIImageView.init(frame: CGRect(x: 15, y: 15, width: 150, height: 150))
        showImage?.layer.masksToBounds = true
        self.contentView.addSubview(showImage!)

        subTitle = UILabel.init(frame: CGRect(x: 180, y: 10, width: 100, height: 12))
        subTitle?.font = UIFont.systemFont(ofSize: 15)
        subTitle?.textAlignment = .center
        subTitle?.textColor = UIColor.red
        self.contentView.addSubview(subTitle!)

        fristTitle = UILabel.init(frame: CGRect(x: 180, y: 50, width: 100, height: 30))
        fristTitle?.font = UIFont.systemFont(ofSize: 18)
        fristTitle?.textAlignment = .center
        fristTitle?.textColor = UIColor.blue
        self.contentView.addSubview(fristTitle!)
    }

     func setValueForCell(dic : NSDictionary) {
        subTitle?.text = "45到"
        fristTitle?.text = "打牌的"
        showImage?.image = UIImage(named: "head_2@3x")
    }
}

使用纯代码创建tableview

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值