How can I hide an empty table view that doesn't end? Swift 3

I am just getting started quickly and I am making a calendar app. I am now showing a list of events if you click on a date. All questions here are about hiding cells that are at the end of the tablieview, but mine is not that kind of events. TableView.tableFooterView = UIView () doesn't work.

override func viewDidLoad() {
    super.viewDidLoad()
    eventsTableView.register(UITableViewCell.self, forCellReuseIdentifier: "theCell")
    self.eventsTableView.rowHeight = 80
}

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


    let event = model.events[indexPath.row]

    let theItem = eventsTableView.dequeueReusableCell(
        withIdentifier: "theCell",for: indexPath)

    let what = event.value(forKeyPath:"what") as? String
    let location = event.value(forKeyPath:"location") as? String
    let when = event.value(forKeyPath:"when") as? Date

    if model.checkDay(date: when!) == model.givendate && model.checkMonth(date: when!) == model.displayedMonth {
        theItem.textLabel?.numberOfLines = 3
        let labelText = what! + "\n" + "time: " + model.getTime(date: when!) + "\n" + "location: " + location!
        theItem.textLabel?.text = labelText
    } else {
        theItem.textLabel?.numberOfLines = 0
    }
    print(theItem)
    return theItem
}

      

This is what my result looks like

+3


source to share


2 answers


Try this code: You can put a check for blank data in the model and hide this cell completely like this:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  //Check if model is empty
  if shouldHideCell {
            return 0
        } else {
            return UITableViewAutomaticDimension
        }
  }

      



Please refer to SO Post . Happy coding.

+2


source


What you can do here is just set up your datasource, for example, if the data matching this row is empty, then it’s better not to add this row to your dataset.

Example when setting up your data source



if hasEvent{
dataSource.append(day)
}else{
// No need to show in UI
}

      

+2


source







All Articles