【问题标题】:How to return different array count in tableView numberOfRowsInSection in swift如何在 swift 中返回 tableView numberOfRowsInSection 中的不同数组计数
【发布时间】:2019-10-30 15:04:47
【问题描述】:

我有一个包含一个标签的单元格的 tableView.. 我需要为不同的类别显示不同的标签文本。 为此,我创建了空的 itamArray,我正在尝试使用 if 条件为不同的类别附加 itamArray。以同样的方式,我需要在numberOfRowsInSection 中返回数组计数,但在这里我无法检查是否条件,如果我给出 return itamArray.count 它只会在返回时出现,然后如果我给出返回 0 它会显示所有类别值表格视图中没有任何内容。

如果我点击水,我只需要在 tabelview 中显示与水相关的 itamsArray。但在这里,如果我点击任何类别,它会在 tableview 中显示所有类别相关的值。请在代码中帮助我。

这是我的代码:

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

请在上面的代码中帮助我。

【问题讨论】:

    标签: arrays json swift uitableview if-statement


    【解决方案1】:

    当你在这里将所有项目嵌入到同一个数组中时

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

    即使categoryName 是不同的,你更需要

    var iteamsArray = [Category]()
    var filteredArray = [Category]()
    

    guard let res = try? JSONDecoder().decode(Root.self,from:data) else { return }
    self.iteamsArray = res.billerdetails
    

    struct Root {
        let billerdetails:[Category]
    }
    
    struct Category {
        let bcategoryname,bname:String
    }
    

    然后在表格中使用filteredArray,当您需要更改类别时,请执行

    self.filteredArray = itemsArray.filter{ $0.bcategoryname == "Water" }
    self.tableView.reloadData()
    

    【讨论】:

    • 你是对的,我将所有值附加到同一个数组......但在你的回答中,我如何在不使用 JSONDecoder() 的情况下实现这一点......请在代码中帮助我
    • 我已经发布了我所有的代码,如果我做错了什么请在这里修改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多