[英]how to programmatically hide or delete extra cell and seperators in tableview swift 2.2
[英]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.