简体   繁体   English

如何隐藏空单元格表格视图,哪些不在最后?斯威夫特3

[英]How to hide empty cell tableview, which are not at the end? Swift 3

I'm just starting swift and I'm making a calendar app. 我刚刚开始,我正在制作一个日历应用程序。 Right now I'm showing a list on events if you click on a date. 现在,如果您点击日期,我会显示事件列表。 All questions here are about hiding cells that are at the end of a tablieview, but mine are not so eventsTableView.tableFooterView = UIView() doesn't work. 这里的所有问题都是关于隐藏在tablieview末尾的单元格,但我的不是eventTableView.tableFooterView = UIView()不起作用。

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 output looks like 这就是我的输出的样子

Try this code : You can put a check for empty data in a model and hide that 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 this SO Post . 请参阅此SO帖子 Happy coding. 快乐的编码。

What you can do here is just customise your dataSource, like if the Data corresponding to that row is empty then it's better to not to add that row in your datasource array. 你可以在这里做的只是自定义你的dataSource,就像对应那行的Data是空的那样,最好不要在你的数据源数组中添加那一行。

Example while customising your dataSource 自定义dataSource时的示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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