วิธีการก็ทำไปตาม step เลยนะครับ ที่ไฟล์ ViewController ก็สร้าง object ของ UITableview จะได้ตามนี้ครับ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// // ViewController.swift // Test2 // // Created by Nisit Sirimarnkit on 9/25/2559 BE. // Copyright © 2559 Nisit Sirimarnkit. All rights reserved. // import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { let tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. tableView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height) tableView.delegate = self tableView.dataSource = self tableView.isScrollEnabled = false tableView.register(TableViewCell2.self, forCellReuseIdentifier: "tbCell") view.addSubview(tableView) } public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "tbCell", for: indexPath) as! TableViewCell2 return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
จากนั้นต้องมีการสร้างไฟล์ UITableViewCell ขึ้นมานะครับ โดยที่ถ้าเราสร้างจาก Cocoa Touch ฟอร์มที่สร้างมาให้จะเป็นดังนี้ครับ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// // TableViewCell2.swift // Test2 // // Created by Nisit Sirimarnkit on 9/26/2559 BE. // Copyright © 2559 Nisit Sirimarnkit. All rights reserved. // import UIKit class TableViewCell2: UITableViewCell { let myText = UILabel() override func awakeFromNib() { super.awakeFromNib() // Initialization code myText.frame = CGRect(x: 0, y: 0, width: 200, height: 300) myText.textColor = UIColor.black self.contentView.addSubview(myText) } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } } |
จากรูปข้างบน ถ้าหากเราตั้งค่า text โดยตั้งค่าลงใน awakeFromNib() ตามที่มันกำหนดมาให้ หลังจากเรารัน ค่า text จะไม่แสดงผลออกมาใน cell เพราะว่าฟังก์ชันนี้จะใช้ได้ในกรณี สร้างโดยใช้ storyboard เท่านั้น ดังนั้นให้เรา override ฟังก์ชัน init() จะได้ดังนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// // TableViewCell.swift // Test2 // // Created by Nisit Sirimarnkit on 9/26/2559 BE. // Copyright © 2559 Nisit Sirimarnkit. All rights reserved. // import UIKit class TableViewCell2: UITableViewCell { var myText = UILabel() override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) myText.frame = CGRect(x: 0, y: 0, width: 200, height: 300) myText.textColor = UIColor.black self.contentView.addSubview(myText) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } } |
ผลที่ได้ก็จะมี text โชว์ใน cell แล้วนะครับ 🙂
0