简体   繁体   中英

How to show another viewcontroller if tableView data is empty

How would you structure an app like this when there's no tableView data which shows another viewcontroller?

The web show ways to show a message if the tableview has no data but I need to show another view. That viewcontroller (PlaceholderVC) has an image and label (a 404 styled page etc).

Basically on the first screen (initial), user taps a button to get to the tableView. On the navigationBar, an item lets you create a new table cell (data). There's no other way to create that item unless you go onto the tableView controller (FeedVC). I struggle to get this working correctly. What I've implemented has many bugs:

//FeedVC
let data = []
viewDidLoad() {
 [...]
}

numberOfRowsInSection {
  if data.count == 0 {
    showPlaceholderVC()
  } else {
    return data.count
  }
}

func showPlaceholderVC() {
  let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  let pc: PlaceholderVC = storyboard.instantiateViewController(withIdentifier: "placeholderVC") as! PlaceholderVC
  self.showDetailViewController(pc, sender: Any?.self)
}

That would take me to the PlaceholderVC . That PlaceholderVC can also present the VC to create a new table cell. When I hit save, it dismisses and go back to the PlaceholderVC . I could modify the save function to present the FeedVC but I dosent feel right, it should be dismiss() . If data > 0 and I create a new data, that save func would still present. See what's happening? Save should run some code then dismiss. I wish not to add layer upon more layer.

I could have a clean code if I could add a hidden image and label over the tableView. Is that possible? When I drag an image view over the tableView, it shrinks.

I could get this all to work but it'll be too many "ifs" is all VCs with many "presents".

As per you issue you need to follow these steps:

  1. If you are calling webservice, then first call webservice on getting response check array size. If array size > 0 means you can show the data on table and if array is empty then you need to navigate on default or placeholder controller.

Do you want to always push a separate view controller for showing the placeholder text?

If not, you can have a tableView as subView of the main view and placeholderView again as subview of the main view.

You can hide and show tableView depending on the data.

If you're allowed to use third party library into your code, you can refer DZNEmptyDataSet

According to me taking a Hidden View (with whatever you want to show when there is no data) is better approach.it can be like:

 @IBOutlet weak var mEmptyView: UIView!
 @IBOutlet weak var mTableView: UITableView!
  var tableViewDataArray: [String] = []


  override func viewDidLoad() {
        super.viewDidLoad()
     tableViewDataArray = //get array from your resource//  
   if tableViewDataArray.count > 0 {
    mEmptyView.hidden = false
    mTableView.hidden = true
   }else{
    mEmptyView.hidden = true
    mTableView.hidden = false
   }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM