mercredi 30 octobre 2019

How to return different array count in tableView numberOfRowsInSection in swift

I have tableView with one cell which contains one label.. i need to display different label text for different category. for that i have created empty iteamArray and i am trying to append iteamArray for different category using if condition. in the same way i need to return array count in numberOfRowsInSection but here i am unable to check if condition, it only goes out side return if i give return iteamArray.count then it displays all category values if i give return 0 it display nothing in tableview.

if i click water i need to show only water related iteamsArray in tabelview.. but here if i click any category it shows all category related values in tableview.. please help me in the code.

here is my code:

import UIKit

class PaymentTableViewCell: UITableViewCell{

@IBOutlet weak var pamntTypeLabel: UILabel!
}

class AllMakePaymentViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var categoryName: String?
var iteamsArray = [String]()
override func viewDidLoad() {
    super.viewDidLoad()
    allPaymentService()
}
func allPaymentService(){

    let urlStr = "https://dev.com/webservices/api.php?rquest"
    let url = URL(string: urlStr)
    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
        guard let respData = data else {
            return
        }
        guard error == nil else {
            print("error")
            return
        }
        do{
            let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
            //print("the all make payment json is \(jsonObj)")
            let billerdetailsArray = jsonObj["billerdetails"] as! [[String: Any]]

            for billerdetail in billerdetailsArray {
                self.categoryName = billerdetail["bcategoryname"] as? String

                if self.categoryName == "Water"{
                    let bName = billerdetail["bname"] as? String
                    self.iteamsArray.append(bName ?? "")
                    print("water arry \(self.iteamsArray.append(bName ?? ""))")
                }
                if self.categoryName == "Landline Postpaid"{
                    let bName = billerdetail["bname"] as? String
                    self.iteamsArray.append(bName ?? "")
                    print("Landline arry \(self.iteamsArray.append(bName ?? ""))")
                }
                if self.categoryName == "DTH"{
                    let bName = billerdetail["bname"] as? String
                    self.iteamsArray.append(bName ?? "")
                    print("DTH arry \(self.iteamsArray.append(bName ?? ""))")
                }
                if self.categoryName == "Electricity"{
                    let bName = billerdetail["bname"] as? String
                    self.iteamsArray.append(bName ?? "")
                    print("Electricity arry \(self.iteamsArray.append(bName ?? ""))")
                }
            }
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
        catch {
            print("catch error")
        }
    }).resume()
}
}

extension AllMakePaymentViewController: UITableViewDelegate, UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

    if categoryName == "Water"{
        return iteamsArray.count
    }
    if categoryName == "Landline Postpaid"{
        return iteamsArray.count
    }
    if categoryName == "DTH"{
        return iteamsArray.count
    }
    if categoryName == "Electricity"{
        return iteamsArray.count
    }
    return iteamsArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PaymentTableViewCell
    cell.pamntTypeLabel.text = iteamsArray[indexPath.row]
    self.tableView.separatorStyle = .none
    return cell
}
}

Please help me in above code.

Aucun commentaire:

Enregistrer un commentaire