Swift 中没有项目时的视图而不是 Tableview

A view instead of a Tableview when no item in Swift

我想做一些类似于应用时钟的东西。

我有一个动态加载的表视图。

当没有项目可用时,我想添加一个带有文本消息的视图,而不是空的表视图。

我使用 actionSheet 来指示何时没有可用的项目,它可以工作,但我更喜欢显示带有文本消息的视图,就像在应用程序时钟中一样。

你知道如何进行吗?

    override func viewWillAppear(animated: Bool) {

    // get the destinations data ... 


    if (destinations.count<1){

        var myActionSheet: UIActionSheet = UIActionSheet()
        let title: String = NSLocalizedString("DestinationsMsg", comment: "")
        myActionSheet.title  = title
        myActionSheet.delegate = self;
        myActionSheet.addButtonWithTitle("Cancel")
        myActionSheet.addButtonWithTitle("Add a Destination")
        myActionSheet.cancelButtonIndex = 0
        myActionSheet.showInView(self.view)


    }

    super.viewWillAppear(true);

}

这就是解决方案,即使我对标签的位置也不完全满意。

       else {


        var bounds: CGRect = UIScreen.mainScreen().bounds
        var width:CGFloat = bounds.size.width
        var height:CGFloat = bounds.size.height

        var view1 = UIView()

        let label = UILabel(frame: CGRect(x: 0, y: height / 4, width: width, height: 40))
        label.textColor =  UIColor(red: 160/255, green: 160/255, blue: 160/255, alpha: 1.0)
        label.font = UIFont.boldSystemFontOfSize(26.0)
        label.textAlignment = NSTextAlignment.Center
        label.text = "No Destination"

        view1.addSubview(label)

        tableView.tableHeaderView = view1


    }

当没有要显示的行时,将 table 视图的 tableHeaderView 设置为占位符视图。当有行要显示时,将 tableHeaderView 设置为 nil