佚名通过本文主要向大家介绍了swift uitableview,uitableviewcell,uitableviewcellstyle,ios uitableviewcell,uitableviewcell 点击等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: swift UItableViewCell怎么自定义添加控件、设置样式
描述:
解决方案1:
描述:
swift 在不用storybord的前提下,UItableViewCell怎么做才能自定义Cell,在Cell里面自己随意添加label、button等控件呢?
解决方案1:
建议用xib,新建类继承UITableViewCell 同时勾选 xib的复选框,直接在xib里面拖相应的控件
// 记得加上这句话
tableView.registerNib(UINib.init(nibName: "RedPacketCell(自定义的cell的类名)", bundle: nil), forCellReuseIdentifier: "redPacket(标识符)")
解决方案2:自定义一个UITableViewCell的类,并创建xib,在xib上面可以自定义样式,比如拖入label,imageview等。
解决方案3:你可以使用xib,推荐使用,创建cell,同时创建Xib即可,xib上面可以自定义布局
// 注册nib
tableView.registerNib(UINib(nibName: "nibName", bundle: mainBundle), forCellReuseIdentifier: "cellIdentifier");
// 注册类
tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "cellIdentifier")
// 数据源
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as! TableViewCell
return cell;
}
使用注册类的时候需要手写代码
重写init(style: UITableViewCellStyle, reuseIdentifier: String?)
方法,并在里面进行UI设置
ps: 网上搜自定义cell教程还是挺多的