繁体   English   中英

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

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

我刚刚开始,我正在制作一个日历应用程序。 现在,如果您点击日期,我会显示事件列表。 这里的所有问题都是关于隐藏在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
}

这就是我的输出的样子

试试这段代码:您可以检查模型中的空数据并完全隐藏该单元格:

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

请参阅此SO帖子 快乐的编码。

你可以在这里做的只是自定义你的dataSource,就像对应那行的Data是空的那样,最好不要在你的数据源数组中添加那一行。

自定义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