swift4 tableViewCell结合Kingfisher 、SnapKit的使用分析
pod中加入第三方 Kingfisher SnapKit pod第三方 platform :ios, '10.0' target 'ChatWorld' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! pod 'Alamofire' pod 'SwiftyJSON' pod 'HandyJSON', '~> 4.1.1' pod 'Kingfisher' pod 'SVProgressHUD' pod 'MJRefresh' pod 'SGPagingView' pod 'BMPlayer' pod 'FDFullscreenPopGesture' pod 'SnapKit' pod 'lottie-ios' pod 'HyphenateLite' pod 'EaseUI', :git => 'https://github.com/easemob/easeui-ios-hyphenate-cocoapods.git' end cell的编写 import UIKit class ConversionCell: UITableViewCell { //MARK: 懒加载 lazy var iconIM:UIImageView = { let imageView = UIImageView.init() return imageView }() lazy var topLab:UILabel = { let lab = UILabel.init() return lab }() lazy var messageLab:UILabel = { let lab = UILabel.init() return lab }() lazy var dateLab:UILabel = { let lab = UILabel.init() return lab }() override func awakeFromNib() { super.awakeFromNib() } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) self.initUI() } func updateUI(){ } //UI布局 func initUI(){ self.contentView.addSubview(self.iconIM) self.contentView.addSubview(self.dateLab) self.contentView.addSubview(self.topLab) self.contentView.addSubview(self.messageLab) self.iconIM.snp.makeConstraints { (make) in make.left.equalTo(12) make.centerY.equalTo(self.snp.centerY) make.width.height.equalTo(60) } self.dateLab.snp.makeConstraints { (make) in make.right.equalTo(-8) make.top.equalTo(5) make.width.equalTo(80) make.height.equalTo(20) } self.topLab.snp.makeConstraints { (make) in make.left.equalTo(iconIM.snp.right).offset(8) make.top.right.equalTo(0) make.height.equalTo(30) } self.messageLab.snp.makeConstraints { (make) in make.left.equalTo(self.iconIM.snp.right).offset(8) make.right.equalTo(-8) make.top.equalTo(self.snp.centerY) make.bottom.equalTo(-8) } } override func layoutSubviews() { super.layoutSubviews() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } 2.tableView中的使用 extension MessageViewController{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 20 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = ConversionCell.init(style: .default, reuseIdentifier: "cell") cell.dateLab.text = "2018-12-13" cell.messageLab.text = "在不?" cell.iconIM.kf.setImage(with: URL(string: "http://yourImage.com/id=1"), placeholder: UIImage.init(named: "4"), options:nil, progressBlock: nil, completionHandler: nil) cell.topLab.text = "狩猎开始" return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 80 } } Kingfisher 处理图片的流行第三方 cell.iconIM.kf.setImage(with: URL(string: "http://yourImage.com/id=1"), placeholder: UIImage.init(named: "4"), options:nil, progressBlock: nil, completionHandler: nil)