Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display label when table view is empty

Tags:

xcode

swift

I am trying to figure out how to display a message within a table view when the table is empty. I would like it to say something like: "You haven't added any transactions yet. Tap the add button to get started.". Obviously I would need it to revert back to this message if the user deletes all of the cells, too.

This is the code that I currently have in my table view 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)
        }
    }
}
}

Any help would be much appreciated!

like image 221
user3746428 Avatar asked Jul 24 '14 15:07

user3746428


3 Answers

You might want to set the backgroundView to a UILabel (Or some view you made when the table is empty

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

something like this works fine for me

like image 139
Putt Potsawee Avatar answered Nov 15 '22 10:11

Putt Potsawee


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
  }
like image 38
Sanjay Mali Avatar answered Nov 15 '22 11:11

Sanjay Mali


Override your viewDidLoad() method like this:

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;
    }
}
like image 37
Shamsiddin Saidov Avatar answered Nov 15 '22 10:11

Shamsiddin Saidov