廖豪豪
廖豪豪

Reputation: 93

Loading tableview data

I am in a table view introduction and repair of the data when x is equal to 1 will be introduced into the data, equal to 2 will be transferred to the repair data, but when I was blank after the implementation of the table view, and my delegate and data source are there is a link

Here is my download data function:

func laodingTableviewData() {
    self.suggestionTableView.isHidden = true
    let frame = CGRect(x: 150, y: 283, width: 80, height: 80)
    let color = UIColor(red: 50.0/255.0, green: 124.0/255.0, blue: 203.0/255.0, alpha: 1)
    let activityIndicatorView = NVActivityIndicatorView(frame: frame, type: .ballSpinFadeLoader, color: color, padding: 20)

    self.view.addSubview(activityIndicatorView)
    activityIndicatorView.startAnimating()


    if self.x == 1 {
        DispatchQueue.main.async {
            print("Start download suggestion data...")
            self.suggestions = Suggestion.downloadAllSuggestion()
            self.suggestionTableView.isHidden = false
            self.suggestionTableView.reloadData()
        }

    } else if self.y == 2 {
        DispatchQueue.main.async {
             print("Start download repair data...")
            self.repairs = Repair.downloadAllRepair()
            self.suggestionTableView.isHidden = false
            self.suggestionTableView.reloadData()
        }

    }
        activityIndicatorView.stopAnimating()
}

I have set a breakpoint in self.suggestions = advice.downloadAllSuggestion (), I am sure there are caught the data, but can not show in the table view, please help answer, thank you!

Here is table view function

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! FirstTableViewCell

   if x == 1 {
        cell.suggestions = suggestions[indexPath.row]
    } else if y == 2 {
        cell.repairs = repairs[indexPath.row]
    }
    return cell

}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var value = 0

    switch value {
    case self.x:
        value = suggestions.count
    case self.y:
        value = repairs.count
    default:
        break
    }
    return value
}

Upvotes: 1

Views: 144

Answers (2)

luckyShubhra
luckyShubhra

Reputation: 2751

As per my understanding you are binding your data conditionally on the basis of x and y value. Your numberOfRowsInSection method shuould also reflect the same.

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if x == 1 {
        return suggestions.count
       } else if y == 2 {
           return repairs.count
            }
     return 0
    }

Hope it helps. Happy Coding!!

Upvotes: 1

Subramanian P
Subramanian P

Reputation: 4375

In numberOfRowsInSection, value is initialised with 0.

And passing to switch case. it will always goes to default. So numberOfRowsInSection is returning always 0

Your function suppose to be like this.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var value = 0

    if self.x == 1 {
       value = suggestions.count
    } else if self.y == 2 {
       value = repairs.count
     }

    return value
}

Upvotes: 2

Related Questions