0

I am building a tableView which displays a message when empty.

I'm using the really helpful answers on this question (Handling an empty UITableView. Print a friendly message) This has led me to a function:

func emptyMessage(message:String, viewController:UITableViewController) {
        let VCSize = viewController.view.bounds.size
        let messageLabel = UILabel(frame: CGRect(x:0, y:0, width:VCSize.width, height:VCSize.height))
        messageLabel.text = message
        messageLabel.textColor = UIColor.black
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = .center;
        messageLabel.font = UIFont(name: "Futura", size: 15)
        messageLabel.sizeToFit()
        viewController.tableView.backgroundView = messageLabel;
        viewController.tableView.separatorStyle = .none;
    }

I could call this in every table views data source like this :

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if projects.count > 0 {
    return 1
} else {
    TableViewHelper.EmptyMessage("You don't have any projects yet.\nYou can create up to 10.", viewController: self)
    return 0
}
}

which would work. However I would rather not have to implement that repeatedly and instead have one custom tableview with a method in the data source asking what message you would like to add.

I've tried extending the TableView class or making a subclass of tableView but I think this isn't the solution. Instead I think the solution is to overwrite the UITableViewDataSource protocol but my knowledge of protocols and delegation isn't sufficient.

I hope i'm on the right track with this. And to clarify I could do it in the way mentioned above but i'm trying to override the functionality to make a smart solution where i'm not repeating myself.

Lewis Black
  • 957
  • 2
  • 8
  • 22

1 Answers1

0

There is a very good library :

https://github.com/dzenbot/DZNEmptyDataSet

This can be used for all types of containers like UITableView, UICollectionView etc.

After conforming to some DZNEmptyDataSetSource, DZNEmptyDataSetDelegate , you can simply implement these functions:

func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let str = "Empty Data."
        let attrs = [NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)]
        return NSAttributedString(string: str, attributes: attrs)
    }

    func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        let str = "Any Details about empty data."
        let attrs = [NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)]
        return NSAttributedString(string: str, attributes: attrs)
    }

Apart from that, you can add a button to to perform some actions. Please refer the library for more features.

schinj
  • 794
  • 4
  • 19
  • Really appreciate this and i've looked at the library and that would work. I didn't specify in the question so you wouldn't of known but the reason I didn't go with a library is I wanted to learn the theory behind how I could do this. I will try and read the github files and see how they do it. thanks! – Lewis Black May 31 '17 at 20:28