ios - Swift 中没有项目时的 View 而不是 Tableview

标签 ios swift storyboard

我想做一些类似于应用程序时钟的事情。

我有一个动态加载的表格 View 。

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

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

您知道如何继续吗?

    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


    }

最佳答案

当没有要显示的行时,将表格 View 的 tableHeaderView 设置为您的占位符 View 。当有行要显示时,将 tableHeaderView 设置为 nil

关于ios - Swift 中没有项目时的 View 而不是 Tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28745155/

相关文章:

ios - Swift AVFoundation 翻转相机功能不起作用

ios - 您可以更改 scnView.autoenablesDefaultLighting 的属性吗?

ios - "Delete"滑动 UITableViewCell 时不显示

swift - 具有多个 ID 的 iOS Firestore 复合查询

ios - swift 结构体作为函数参数

ios - 为什么我的 Table View Cell 无法显示?

ios - 如何调用另一个 Storyboard作为弹出窗口?

ios - 如何在不连接 socket 的情况下将 Storyboard的按钮颜色设置为常用颜色

ios - 如何覆盖 XCTest 中的 TableView 委托(delegate)

ios - 按名称定义和转换类