【问题标题】:How to hide empty cell tableview, which are not at the end? Swift 3如何隐藏不在最后的空单元格表格视图?斯威夫特 3
【发布时间】:2017-05-19 16:51:56
【问题描述】:

我刚刚开始 swift,我正在制作一个日历应用程序。现在,如果您单击日期,我将显示事件列表。这里的所有问题都是关于隐藏 tablieview 末尾的单元格,但我的不是这样 eventsTableView.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

【问题讨论】:

  • 你可以使用约束来做到这一点

标签: ios swift uitableview core-data swift3


【解决方案1】:

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

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

请参考这个SO Post。编码愉快。

【讨论】:

  • 问题是这个函数没有执行。我在函数中放了一个打印语句来检查,但它只是不执行
【解决方案2】:

您可以在这里做的只是自定义您的数据源,例如如果与该行对应的数据为空,那么最好不要将该行添加到您的数据源数组中。

自定义数据源时的示例

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

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多