3

I am new to programming and currently working on a newsfeed like app. I had a normal Table view up and running fine, but want to try it now with a costume cell type. So I created one and thought connecting the labels the usual way would be perfectly fine, but I was wrong. So I am wondering how I can let my Text label connect to my view controller, so I can use my custom cell.

class ViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var newsfeedTableView: UITableView!

var ref: DatabaseReference!

var posts = [String]()


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (posts.count)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")

//here is where I need the custom label to get the posts 
    cell.textLabel?.text = posts[indexPath.row]
    cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)

    return cell
 }
}

Costum cell

Roman H
  • 251
  • 2
  • 10

2 Answers2

4

Create subclass of UITableViewCell and connect IBOutlets to this class

class YourCell: UITableViewCell {
    @IBOutlet weak var customLabel: UILabel!
    ...
}

don't forget to set class of your prototype cell in storyboard:

enter image description here

then in cellForRowAt data source method downcast your dequeued cell as YourCell

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourCell

then you have access to YourCell outlets

cell.customLabel.text = "SomeText"
...
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
1

I'm assuming that you are using Storyboard.

First of all, you should understand that there is little difference when you use own custom table cell. In that case, in the method "cellForRowAtIndexPath", after dequeue your cell, you just have to typecast table cell like 'as! YourCustomTableCellClass'. After this line, you can access each property of this class.

First, design your table cell on Storyboard whatever you want.

Now, make a subclass of UITableViewCell and assign this class to your prototype custom cell which you have designed on Storyboard. Also, don't forget to set "reuse identifier in Storyboard table cell.

Then connect your outlets with custom cell class from Storyboard.

Now you can use code like this:

 class YourTableCellClass: UITableViewCell {

// I'm using these outlets as a sample, but you have to connect them from Storyboard.
var leftTextLabel: UILabel!
var rightTextLabel: UILabel!
}

class YourTableController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}

// MARK: - TableView Delegate & DataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1 // return your number of rows here...
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100 // return the height for the row here.....or you can set height from Storyboard if you have fix height of all rows.
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! YourTableCellClass
    cell.leftTextLabel.text = "Text" // Set text here....
    cell.rightTextLabel.text = "Text" // Set text here....
    return cell
}
}
nitin.agam
  • 1,949
  • 1
  • 17
  • 24