UITableView and delegates
- create cellReuseIdendifier for cell string reuse id
- set up tableview and its delegates, implement delegate methods
- tableView.register(YourCell.self, forCellReuseIdentifier: “”)
- in cellForRow, tableView.dequeueReusableCell(withIdentifier: cellReuseIdendifier) as! SearchDisplayTableViewCell
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import UIKit class SearchDisplayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { let cellReuseIdendifier = "SearchDisplayTableViewCell" // MARK: - Life Cycle override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.orange let screenSize: CGRect = UIScreen.main.bounds print(screenSize.debugDescription) let tableView: UITableView = UITableView() tableView.frame = CGRect(x: 0, y: 64.0, width: screenSize.width, height: screenSize.height - 64.0) tableView.dataSource = self tableView.delegate = self //REGISTER THE CELL tableView.register(SearchDisplayTableViewCell.self, forCellReuseIdentifier: cellReuseIdendifier) self.view.addSubview(tableView) } // MARK: - Table View func numberOfSectionsInTableView(tableView: UITableView) -> Int { NSLog("sections") return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { NSLog("rows") return 3 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdendifier) as! SearchDisplayTableViewCell cell.myLabel.text = "Candlewood Suites" return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 88.0;//Choose your custom row height } // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } } |
UITableViewCell
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 |
class SearchDisplayTableViewCell: UITableViewCell { var myLabel = UILabel() var myImageView = UIImageView() override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) self.contentView.backgroundColor = UIColor.brown myLabel.backgroundColor = UIColor.yellow self.addSubview(myLabel) //myImageView.image = UIImage(named: "Default") myImageView.backgroundColor = UIColor.red self.contentView.addSubview(myImageView) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func layoutSubviews() { super.layoutSubviews() myLabel.frame = CGRect(x: 100, y: 0, width: 280, height: 30) myImageView.frame = CGRect(x: 10, y: 10, width: 70, height: 70) } } |