美文网首页
11.8自定义cell

11.8自定义cell

作者: 冰凡513 | 来源:发表于2016-11-08 16:27 被阅读0次

//ViewController.swift

importUIKit

classViewController:UIViewController{

//使用懒加载的方式加载plist文件

/*

lazy var变量名:变量类型= {

let常量出来并return

}()

*/

lazyvardictArray:[[String:String]] = {

//加载.plist文件,首先获取.plist文件路径

letpath =NSBundle.mainBundle().pathForResource("Students", ofType:"plist")

letarray =NSArray(contentsOfFile: path!)as! [[String:String]]

returnarray

}()

overridefuncviewDidLoad() {

super.viewDidLoad()

print(dictArray)

lettableView =UITableView(frame:UIScreen.mainScreen().bounds, style:UITableViewStyle.Plain)

view.addSubview(tableView)

tableView.registerClass(StudentCell.self, forCellReuseIdentifier:"Student")

tableView.dataSource=self

tableView.delegate=self

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

extensionViewController:UITableViewDelegate,UITableViewDataSource{

//返回分区行数

functableView(tableView:UITableView, numberOfRowsInSection section:Int) ->Int{

returndictArray.count

}

//返回单元格

functableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{

letcell = tableView.dequeueReusableCellWithIdentifier("Student")as!StudentCell

cell.selectionStyle= .None

//(1)找到该单元格对应的字典

letdict =dictArray[indexPath.row]

letstudent =Student()

student.setValuesForKeysWithDictionary(dict)

//(2)将该字典给cell里面的每个控件赋值

// cell.cellWithDict(dict)

cell.cellWithStudent(student)

/*cell.nameLabel?.text = dict["name"]

cell.genderLabel?.text = dict["sex"]

cell.headView?.image = UIImage(named: dict["icon"]!)

cell.phoneLabel?.text = dict["phoneNumber"]

cell.introducelLabel?.text = dict["introduce"]

*/

returncell

}

functableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath) ->CGFloat{

return150

}

//点击到表示图的任何一行(indexPath(section,row))被调用的方法

functableView(tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {

//页面跳转到详情页面

}

}


//StudentCell.swift

importUIKit

classStudentCell:UITableViewCell{

//头像

varheadView:UIImageView?

//姓名标签

varnameLabel:UILabel?

//电话标签

varphoneLabel:UILabel?

//性别标签

vargenderLabel:UILabel?

//自我介绍标签

varintroducelLabel:UILabel?

overrideinit(style:UITableViewCellStyle, reuseIdentifier:String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

headView=UIImageView(frame:CGRectMake(10,10,80,80))

headView?.backgroundColor=UIColor.cyanColor()

//将上面创建的头像添加到单元格上面

contentView.addSubview(headView!)

nameLabel=UILabel(frame:CGRectMake(110,10,100,20))

nameLabel?.backgroundColor=UIColor.redColor()

//添加到单元格上

contentView.addSubview(nameLabel!)

phoneLabel=UILabel(frame:CGRectMake(110,40,150,20))

phoneLabel?.backgroundColor=UIColor.orangeColor()

//将号码的标签添加到单元格

contentView.addSubview(phoneLabel!)

genderLabel=UILabel(frame:CGRectMake(110,70,100,20))

genderLabel?.backgroundColor=UIColor.blueColor()

contentView.addSubview(genderLabel!)

introducelLabel=UILabel(frame:CGRectMake(110,100,200,40))

introducelLabel?.numberOfLines=0

introducelLabel?.backgroundColor=UIColor.purpleColor()

contentView.addSubview(introducelLabel!)

}

//加载xib文件(可视化编程)的时候会调用

requiredinit(coder aDecoder:NSCoder) {

fatalError("init(coder:) has not been implemented")

}

funccellWithDict(dict:[String:String]){

nameLabel?.text= dict["name"]

genderLabel?.text= dict["sex"]

headView?.image=UIImage(named: dict["icon"]!)

phoneLabel?.text= dict["phoneNumber"]

introducelLabel?.text= dict["introduce"]

}

funccellWithStudent(stu:Student){

nameLabel?.text= stu.name

phoneLabel?.text= stu.phoneNumber

genderLabel?.text= stu.sex

headView?.image=UIImage(named: stu.icon!)

introducelLabel?.text= stu.introduce

}

overridefuncawakeFromNib() {

super.awakeFromNib()

// Initialization code

}

overridefuncsetSelected(selected:Bool, animated:Bool) {

super.setSelected(selected, animated: animated)

// Configure the view for the selected state

}

}


//Student.swift

importUIKit

classStudent:NSObject{

varicon:String?

varname:String?

varphoneNumber:String?

varsex:String?

varintroduce:String?

//防止模型根字典的键值对不照应的情况下出现的报错

overridefuncsetValue(value:AnyObject?, forUndefinedKey key:String) {

}

}

相关文章

网友评论

      本文标题:11.8自定义cell

      本文链接:https://www.haomeiwen.com/subject/cynauttx.html