Latest 0.1.0
Homepage https://github.com/qyz777/YZTableViewAdapter
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

Introduction

YZTableViewAdapter是一个用来快速组装多种Cell的轻量级工具

Example

快速使用

1. 实现TableViewCellProvider协议

你需要用一个类来计算cell需要的高度以及提供cell的类名

class FirstCellProvider: TableViewCellProvider {

    var color = UIColor.red
    var model: InfoModel?

    var height: CGFloat = 40

    func cellHeight() -> CGFloat {
        return height
    }

    func cellClassName() -> String {
        return "FirstCell"
    }

    init(with model: InfoModel) {
        self.model = model
//        在这可以计算cell高度
        let contentHeight = model.content.boundingRect(with: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: 0), options: [.usesLineFragmentOrigin], attributes: nil, context: nil).height
        height += contentHeight
        height += 21
    }

}

2. 让你的Cell实现TableViewCellDataReceiver协议

class FirstCell: UITableViewCell, TableViewCellDataReceiver {

    func updateCell(with provider: TableViewCellProvider) {
        let p = provider as! FirstCellProvider
        backgroundColor = p.color
        let model = p.model!
        titleLabel.text = model.title
        contentLabel.text = model.content
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(titleLabel)
        contentView.addSubview(contentLabel)
        titleLabel.frame = CGRect.init(x: 11, y: 11, width: 120, height: 21)
        contentLabel.frame = CGRect.init(x: 11, y: 42, width: 300, height: bounds.height - 42)
    }

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

    lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.textColor = .black
        return view
    }()

    lazy var contentLabel: UILabel = {
        let view = UILabel()
        view.textColor = .black
        return view
    }()

}

3. 组装不同的Cell提供给Adapter

adapter = TableViewAdapter.init(with: tableView)

let model = InfoModel()
var dataArray: [TableViewCellProvider] = []
for i in 0..<30 {
    if i % 2 == 0 {
        let p = FirstCellProvider.init(with: model)
        p.color = .lightGray
        dataArray.append(p)
    } else {
        let p = SecondCellProvider()
        p.color = .white
        dataArray.append(p)
    }
}

adapter?.reloadRowsData(by: dataArray)

使用Section

adapter = TableViewAdapter.init(with: tableView)

let model = InfoModel()
var dataArray: [[TableViewCellProvider]] = []
var d1: [TableViewCellProvider] = []
var d2: [TableViewCellProvider] = []
for i in 0..<30 {
    if i < 15 {
        let p = FirstCellProvider.init(with: model)
        p.color = .lightGray
        d1.append(p)
    } else {
        let p = SecondCellProvider()
        p.color = .white
        d2.append(p)
    }
}
dataArray.append(d1)
dataArray.append(d2)
adapter?.reloadSectionsData(by: dataArray)

使用TableViewDelegate或TableViewDataSource

实现adapter的tableViewDelegate协议即可

adapter?.tableViewDelegate = self

extension ViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(scrollView.contentOffset)
    }

}

Requirements

Swift >= 4.0

Installation

YZTableViewAdapter is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'YZTableViewAdapter'

Author

qyz777, [email protected]

License

YZTableViewAdapter is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "YZTableViewAdapter",
    "version": "0.1.0",
    "summary": "A Lightweight Tool for Fast Assembly of TableViewCells",
    "description": "YZTableViewAdapter uses adapter to manage various TableViewCells. You just need to implement some protocols for each cell to quickly build a list.",
    "homepage": "https://github.com/qyz777/YZTableViewAdapter",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "qyz777": "[email protected]"
    },
    "source": {
        "git": "https://github.com/qyz777/YZTableViewAdapter.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "YZTableViewAdapter/Classes/**/*"
}

Pin It on Pinterest

Share This