简体   繁体   中英

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?

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM