补充笔记06-tableviewcell的自定义与重用
2016-08-14 本文已影响97人
没有北方的南方
tableviewcell的重用和自定义
*首先在viewcontroller中显示视图
import UIKit
import Kingfisher
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
var source : Array<NSDictionary>!
override func viewDidLoad() {
super.viewDidLoad()
creatview()
showdata()
}
func creatview(){//创建tableview并显示
let tableview = UITableView(frame: self.view.bounds, style: .Plain)
self.view.addSubview(tableview)
//注册cell1/cell2/cell3
tableview.registerClass(cell1.self, forCellReuseIdentifier: "cell1")
tableview.registerClass(cell2.self, forCellReuseIdentifier: "cell2")
tableview.registerClass(cell3.self, forCellReuseIdentifier: "cell3")
tableview.dataSource = self
tableview.delegate = self
}
func showdata(){
//获取本地数据
let path = NSBundle.mainBundle().pathForResource("datasource", ofType: "json")
let data = NSData(contentsOfFile: path!)
//数据类型转换
source = try! NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? Array<NSDictionary>
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return source.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//判断数据结构来创建不同的cell
if source[indexPath.row]["imageurl3"] == nil{
tableView.rowHeight = 150
let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as? cell1
cell?.infoCell1(source[indexPath.row])
return cell!
}else{
let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as? cell2
cell?.infoCell1(source[indexPath.row])
return cell!
}
}
}
*然后自己定义一个tableviewcell的子类,不同的cell,考虑重用
import UIKit
import SnapKit
class cell1: UITableViewCell {
var image12 : UIImageView!
var title1 : UILabel!
var des : UILabel!
var data : Array<NSDictionary>!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//创建image12/title1/des并添加到cell中
image12 = UIImageView()
self.contentView.addSubview(image12)
title1 = UILabel()
self.contentView.addSubview(title1)
//定义title1的约束
title1.snp.makeConstraints { (make) in
make.top.equalTo(self)
make.left.equalTo(image12.snp.right).offset(20)
}
des = UILabel()
self.contentView.addSubview(des)
des.snp.makeConstraints { (make) in
make.left.equalTo(image12.snp.right).offset(20)
make.top.equalTo(self).offset(30)
make.right.equalTo(self)
}
des.numberOfLines = 0//使其换行
}
//程序崩溃掉用
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//引用自动布局
override func layoutSubviews() {
image12.snp.makeConstraints { (make) in
make.left.top.bottom.equalTo(self)
make.width.equalTo(self.snp.width).multipliedBy(0.3)
}
}
//显示数据
func infoCell1(data : NSDictionary){
self.des.text = data["desc"] as? String
self.title1.text = data["title"] as? String
let path = data["imageurl"] as? String
let url = NSURL(string: path!)
self.image12.kf_setImageWithURL(url)
}
}
*定义第二种cell
import UIKit
class cell2: UITableViewCell {
var title : UILabel!
var image1 : UIImageView!
var image2 : UIImageView!
var image3 : UIImageView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
image1 = UIImageView()
self.contentView.addSubview(image1)
image1.snp.makeConstraints { (make) in
make.top.equalTo(self).offset(30)
make.left.equalTo(self).offset(20)
make.bottom.equalTo(self)
make.width.equalTo(self.snp.width).multipliedBy(0.3)
}
image2 = UIImageView()
self.contentView.addSubview(image2)
image2.snp.makeConstraints { (make) in
make.left.equalTo(image1.snp.right)
make.bottom.top.width.equalTo(image1)
}
image3 = UIImageView()
self.contentView.addSubview(image3)
image3.snp.makeConstraints { (make) in
make.left.equalTo(image2.snp.right)
make.bottom.top.width.equalTo(image2)
}
title = UILabel()
self.contentView.addSubview(title)
title.snp.makeConstraints { (make) in
make.left.top.equalTo(self)
make.width.equalTo(self)
make.bottom.equalTo(image1.snp.top)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
}
func infoCell1(data : NSDictionary){
self.title.text = data["title"] as? String
let path1 = data["imageurl1"] as? String
let url1 = NSURL(string: path1!)!
let path2 = data["imageurl2"] as? String
let url2 = NSURL(string: path2!)!
let path3 = data["imageurl3"] as? String
let url3 = NSURL(string: path3!)!
//自动下载显示图片
self.image1.kf_setImageWithURL(url1)
self.image2.kf_setImageWithURL(url2)
self.image3.kf_setImageWithURL(url3)
}
}