Bruno Snickers
Bruno Snickers

Reputation: 198

How to add cells to UITableview inside UICollectionview

I have got a collectionview
class ChatLogController: UICollectionViewController

and a tableview inside it

let colors: UITableView = {
    let div = UITableView();
    div.backgroundColor = UIColor(white: 0.90, alpha: 1)
    return div;
}()

Now i want to add cells to colors table,where should i put the numberOfSectionsInTableView function?

Upvotes: 0

Views: 311

Answers (1)

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

You should add datasource and delegate to the UITableView. Some readings for you delegate and datasource.

So it should be.

div.datasource = self
div.delegate = self

And than in the class that is datasource and delegate for the UITableView add methods for rendering and filling cells with data.

Edited code example.

class ViewController: UIViewController {

    let colors: UITableView = {
        let div = UITableView();
        div.backgroundColor = UIColor(white: 0.90, alpha: 1)
        return div;
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        colors.dataSource = self
    }
}

extension ViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        let label = UILabel()
        label.text="saksaokoask"
        cell.addSubview(label)
        return cell
    }
}

Upvotes: 1

Related Questions