gpt4 book ai didi

ios - Swift - 如何检查 TableView 是否为空

转载 作者:搜寻专家 更新时间:2023-10-31 08:09:47 25 4
gpt4 key购买 nike

所以我正在制作这个待办事项列表应用程序。这个应用程序有本地通知,但我只希望它们在 tableview 为空时弹出。简而言之:如何检查 tableview 是否为空?

这是我当前的代码:

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet var tblTasks : UITableView!
@IBOutlet weak var countLbl: UILabel!
var localNotification = UILocalNotification()

//For persisting data
let defaults = NSUserDefaults.standardUserDefaults()

override func viewDidLoad() {
super.viewDidLoad()
self.tblTasks.reloadData()


// localNotification.alertAction = "Je hebt nog taken die gedaan moeten worden!"
localNotification.alertBody = "Je hebt nog taken die gedaan moeten worden! Namelijk nog \(updateCount)"
localNotification.timeZone = NSTimeZone.localTimeZone()

localNotification.fireDate = NSDate(timeIntervalSinceNow: 10)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

}

override func viewWillAppear(animated: Bool) {
self.tblTasks.reloadData()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return taskMgr.tasks.count

}

//Define how our cells look - 2 lines a heading and a subtitle
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default Tasks")

//Assign the contents of our var "items" to the textLabel of each cell
cell.textLabel!.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel!.text = taskMgr.tasks[indexPath.row].description
cell.backgroundColor = UIColor.clearColor()

return cell

}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

if (editingStyle == UITableViewCellEditingStyle.Delete){

taskMgr.removeTask(indexPath.row)
tblTasks.reloadData()
}

}

谁能帮帮我?谢谢 ;)

最佳答案

在 Swift 3 中:

if tableView.visibleCells.isEmpty {
//tableView is empty. You can set a backgroundView for it.
} else {
//do something
}

关于ios - Swift - 如何检查 TableView 是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36894387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com