xcode - 表格 View 为空时如何显示标签

标签 xcode swift

我想弄清楚当表格为空时如何在表格 View 中显示消息。我希望它说这样的话:“您还没有添加任何交易。点击添加按钮开始。”。显然,如果用户也删除了所有单元格,我需要它恢复到此消息。

这是我目前在我的 TableView Controller 中的代码:

class ThirdViewController: UITableViewController {

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

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
}

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

// #pragma mark - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return arrayObject.paymentsArray().count
}


override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    var cell:CustomTransactionTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
    cell.paymentNameLabel.text = (arrayObject.paymentsArray().objectAtIndex(indexPath.row)) as String
    cell.costLabel.text = (arrayObject.costArray().objectAtIndex(indexPath.row)) as String
    cell.dateLabel.text = (arrayObject.dateArray().objectAtIndex(indexPath.row)) as String

    if arrayObject.imageArray().objectAtIndex(indexPath.row) as NSObject == 0 {
        cell.paymentArrowImage.hidden = false
        cell.creditArrowImage.hidden = true
    } else if arrayObject.imageArray().objectAtIndex(indexPath.row) as NSObject == 1 {
        cell.creditArrowImage.hidden = false
        cell.paymentArrowImage.hidden = true
    }

    return cell
}

override func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    return true
}

override func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {

        if let tv=tableView {
            arrayDataCost.removeObjectAtIndex(indexPath!.row)
            arrayDataImage.removeObjectAtIndex(indexPath!.row)
            arrayDataPayments.removeObjectAtIndex(indexPath!.row)
            arrayDataDate.removeObjectAtIndex(indexPath!.row)
            tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
    }
}
}

如有任何帮助,我们将不胜感激!

最佳答案

您可能希望将 backgroundView 设置为 UILabel(或者您在表格为空时创建的某些 View

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.numberOfRow == 0{
        var emptyLabel = UILabel(frame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height))
        emptyLabel.text = "No Data"
        emptyLabel.textAlignment = NSTextAlignment.Center
        self.tableView.backgroundView = emptyLabel
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        return 0
    } else {
        return self.numberOfRow
    }
}

这样的东西对我来说很好

关于xcode - 表格 View 为空时如何显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24937951/

相关文章:

ios - Swift 仅在启用定位服务时执行代码

ios - 无法使用 plist 文件获取不同的图钉图像

c++ - 安装摩西翻译软件。错误消息 : "ld: library not found for -lboost_thread"

ios - 当应用在后台时,iOS 上不显示通知(使用 Firebase 发送)

ios - 如何从 firebase 检索数据到 TableView Controller 并将其传递到另一个 View Controller

ios - Swift JSON 响应两个字段值追加到单个数组中

ios - 如何将版权信息添加到 Nib 文件?

ios - NSURL错误代码提取

ios - 翻转过渡动画后 IBOutlet 为 nil

swift - 等待 swift 脚本中的异步调用