2014-07-24 35 views
3

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

这是我目前在我的表视图控制器代码:

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) 
     } 
    } 
} 
} 

任何帮助,将不胜感激!

回答

1

重写你的viewDidLoad()方法是这样的:

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.view.addSubview(self.yourLabel); 

    if arrayObject.paymentsArray().count > 0 { 
     self.tableView.hidden = NO; 
     self.yourLabel.hidden = YES; 
    } else { 
     self.tableView.hidden = YES; 
     self.yourLabel.hidden = NO; 
    } 
} 
+0

谢谢,但我收到错误ThirdViewController没有名为'addSubView'的成员。另外,如何将标签添加到视图控制器?我只能将它放在单元格内。 – user3746428

+0

我已经更新了我的答案。 self.view.addSubview(self.yourLabel);方法将标签添加到yourSomeViewController – Shamsiddin

+0

您是否尝试将标签放在tableView下方,以便它在tableView隐藏时变为可见? – Magnas

0

您可以隐藏的tableView和取消隐藏的标签,或显示某种动画的降低的观点之一的阿尔法的“淡出”的效果。

+0

我明白了。我现在的问题是,我不知道如何将标签贴到视图控制器上?我不能把它放在桌子上,它只允许我把它放在我明显不想做的单元格内。 – user3746428

7

您可能要到backgroundView设置为一个UILabel(或查看时,该表是空的,你做

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 
    } 
} 

像这样工作正常,我

+0

由于某种原因,即使array.count> 0时仍然显示标签。 – V1P3R

+0

即使计数不为0,您是否知道它为什么显示标签? –

0
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     var numOfSection: NSInteger = 0 
     if array.count > 0 
{ 
      self.tableView.backgroundView = nil 
      numOfSection = 1 
     } 
else 
{ 
      var noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height)) 
      noDataLabel.text = "No Data Available" 
      noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0) 
      noDataLabel.textAlignment = NSTextAlignment.Center 
      self.tableView.backgroundView = noDataLabel 
      } 
     return numOfSection 
    } 
0

您可以使用此法无功能限制

Swift3

if tableView.visibleCells.isEmpty{ 
      //tableview is not data 
      print("can not found data") 
    }else{ 
     //do somethings 

    } 
相关问题