1

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!

NelbeDG
  • 425
  • 1
  • 7
  • 19
  • Right now you have configured an automatic height for your cells (which will be define by your autolayout rules). If you need a fixed height you could provide it via `rowHeight` or by implementing `func tableView(UITableView, heightForRowAt: IndexPath)`. If your question is about the sections, you could place a breakpoint and see if the method is called and make sure that you supply header views as well (viewForHeaderInSection...). I hope it makes sense... – Alladinian Jan 17 '18 at 11:28
  • where you have assigned to header view in the code I don't see that delegate viewForHeaderInSectionimplemnted or tableview.headerview is set – Vinodh Jan 17 '18 at 11:29
  • Thanks. You're right. I had confused the methods. I need to give space between one cell and another. – NelbeDG Jan 17 '18 at 11:30

2 Answers2

3

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.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
0

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!

  • Thanks, this has been helpful for this error. But I was wrong to express it, I really need to give space between cells. – NelbeDG Jan 17 '18 at 11:34
  • Then you should go through this post: https://stackoverflow.com/questions/43849297/xcode-8-swift-3-uitableview-space-between-cells – Pallavi Srikhakollu Jan 17 '18 at 11:36