Table view in iOS

*Dynamic label problem solved by only a margin from trailing

*Dynamic Row Height

Drag and drop a table view from left bottom corner of xcode

inherit from UITableViewDataSource, UITableViewDelegate

and write following code

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = UITableViewCell(style: .default, reuseIdentifier:”cell”)
//cell.textLabel?.text = list[indexPath.row] //return cell

let cell = self.mainTableView.dequeueReusableCell(withIdentifier: “cell”, for: indexPath) as! DynamicHeightTableViewCell

// cell.testLabel.sizeToFit()

cell.testLabel?.text = list[indexPath.row] cell.testLabel.numberOfLines = 0

return cell
}

*UITableView
Use two delegate UITableViewDataSource, UITableViewDelegate
and create one prototype cell and name it cell and connect tableView to ViewController with delegate and dataSource

ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate
let list = [“Lion.jpg”,”Panda.jpg”]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = UITableViewCell(style: .default, reuseIdentifier:”cell”)
//cell.textLabel?.text = list[indexPath.row] //return cell

let cell = tableView.dequeueReusableCell(withIdentifier: “cell”, for: indexPath) as! ViewControllerTableViewCell
cell.myImage.image = UIImage(named: list[indexPath.row])
cell.myLabel.text = list[indexPath.row] return cell
}
//heightForRow is also important for equal height of row. https://www.youtube.com/watch?v=kYmZ-4l0Yy4

*ViewControllerTableViewCell (link to UI tables)
class ViewControllerTableViewCell: UITableViewCell {

@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var myLabel: UILabel!

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
}

}
for more

and

*UITableView from nib file(Custom Table View Cells! (XIB Files : Swift 3 in Xcode 8))

Posted in IOS

Leave a Reply

Your email address will not be published. Required fields are marked *