标签:
方法一: 方法一没有讲单元格重用 建的单元格数量少还可以
1、新建一个Swift文件
import UIKit class Custom: UITableViewCell { var label1: UILabel? var label2: UILabel? var label3: UILabel? var label4: UILabel? var label5: UILabel? override func awakeFromNib() { super.awakeFromNib() // Initialization code } func configure() { label1 = UILabel(frame: CGRect(x: 35, y: 15, width: 300, height: 20)) label2 = UILabel(frame: CGRect(x: 35, y: 45, width: 100, height: 40)) label3 = UILabel(frame: CGRect(x: 200, y: 45, width: 100, height: 20)) label4 = UILabel(frame: CGRect(x: 35, y: 70, width: 100, height: 20)) label5 = UILabel(frame: CGRect(x: 200, y: 70, width: 100, height: 20)) contentView.addSubview(label1!) contentView.addSubview(label2!) contentView.addSubview(label3!) contentView.addSubview(label4!) contentView.addSubview(label5!) } }
在viewController里面
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{ let index = indexPath.row let cell = CurriculumCell(frame: CGRect(x: index * 100, y: 0, width: 300, height: 300)) cell.configure() // let cell = tableView2?.dequeueReusableCellWithIdentifier("curriculumCell") as! CurriculumCell cell.label1?.text = "教三303" cell.label2?.text = "张三" cell.label3?.text = "自动控制理论" cell.label4?.text = "1,2,3" cell.label5?.text = "1-16" return cell }
方法二:
目的: 使用reuseIdentifier 来重用
但是在 tableview(tableview: cellForRowAtIndexPath:) 里首先对第一个 cell 实例化一个CustomCell 然后后面cell用
dequeueReusableCellWithIdentifier(identifier:) 方法来取回重用方法
这个方法报错: 取到的cell 为空
新建的文件里
class CustomCell: UITableViewCell { var courseName: UILabel? var location: UILabel? var teacherName: UILabel? var classTime: UILabel? var weekDuration: UILabel? override func awakeFromNib() { super.awakeFromNib() // Initialization code } required init() { super.init(style: .Default, reuseIdentifier: "hello")
self.frame = CGRect(x: index * 100, y: 0, width: 300, height: 300)
configure()
} required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
func configure() {
label1 = UILabel(frame: CGRect(x: 35, y: 15, width: 300, height: 20))
label2 = UILabel(frame: CGRect(x: 35, y: 45, width: 100, height: 40))
label3 = UILabel(frame: CGRect(x: 200, y: 45, width: 100, height: 20))
label4 = UILabel(frame: CGRect(x: 35, y: 70, width: 100, height: 20))
label5 = UILabel(frame: CGRect(x: 200, y: 70, width: 100, height: 20))
contentView.addSubview(label1!)
contentView.addSubview(label2!)
contentView.addSubview(label3!)
contentView.addSubview(label4!)
contentView.addSubview(label5!)
}
}
后面的调用失败了。。。
标签:
原文地址:http://www.cnblogs.com/baaingSheep/p/4859625.html