NelbeDG
NelbeDG

Reputation: 445

I need to give space between one cell and another

I not find what is my problem. I need to give space between one cell and another.

I wrote self.tableView Storyline.delegate = self because I read that this could be the problem, but not know if it is correct.

This is my code:

class ClassName: UIViewController, UITableViewDataSource, UITabBarControllerDelegate, UITableViewDelegate {
    public var notifications: [APINotification] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBarController?.delegate = self
        tableViewStoryline.rowHeight = UITableViewAutomaticDimension
        tableViewStoryline.estimatedRowHeight = 140
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(_ animated: Bool) {
        self.notifications = []
        self.getLastNotifications()
        APIAuth.shared.count_badge = 0
        self.tabBarController?.tabBar.items![0].badgeValue = nil
    }

    public func getLastNotifications() {
        let req = Notification()
        req.getLastNotifications(onComplete: {events in
            self.notifications = events

            DispatchQueue.main.async(execute: {
                self.tableViewStoryline.delegate = self
                self.tableViewStoryline.dataSource = self
                self.tableViewStoryline.sectionHeaderHeight = 10
                self.tableViewStoryline.reloadData()
            })
        })
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    } 

    // There is just one row in every section
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.notifications.count > 4 {
            return 4
        } else {
            return self.notifications.count
        }
    }

    // Set the spacing between sections
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        cell = tableView.dequeueReusableCell(withIdentifier: "controller")! as UITableViewCell
        titleLbl.text = notifications[indexPath.row].title
        bodyLbl.text =  notifications[indexPath.row].description
        typeLbl.text = notifications[indexPath.row].type

        return cell!
    } 
}

Does anyone have any idea what the problem is? Is there some code missing?

Thanks!

Upvotes: 1

Views: 84

Answers (2)

Milan Nosáľ
Milan Nosáľ

Reputation: 19737

I think this is what you want:

    func numberOfSections(in tableView: UITableView) -> Int {
        if self.notifications.count > 4 {
            return 4
        } else {
            return self.notifications.count
        }
    }

    // There is just one row in every section
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

Then you have to change also cellForRowAt to take this into account.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        cell = tableView.dequeueReusableCell(withIdentifier: "controller")! as UITableViewCell
        titleLbl.text = notifications[indexPath.section].title
        bodyLbl.text =  notifications[indexPath.section].description
        typeLbl.text = notifications[indexPath.section].type

        return cell!
    }

Instead of having one section with notifications.count rows, you want notifications.count sections and each section with one row. Now setting 10 points as height for section header will make cells appear as having spaces between them.

There are some other options that you can consider, see my other answer.

Upvotes: 3

Pallavi Srikhakollu
Pallavi Srikhakollu

Reputation: 598

you are using wrong delegate function.

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

is used for the hieght of header not for height of cell.

use func tableView(UITableView, heightForRowAt: IndexPath) you will get correct sized cells.

Hope this helps!

Upvotes: 0

Related Questions