繁体   English   中英

Swift 3:使用ID过滤结果并重新加载TableView

[英]Swift 3: Filtering results with ID's and reload tableview

我试图弄清楚如何过滤数据并重新加载tableview。

我有4个地区(北部,南部,东部和西部)以及所有地区。

默认情况下,它将显示所有配置文件。

如果按北(即1),则仅应过滤北区域中可用的配置文件

同样,南部,东部和西部也是如此。 根据所按下的区域,它应该过滤轮廓。

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

    if indexPath.section == 0
    {
        let cell = self.bussinessTableView.dequeueReusableCell(withIdentifier: "bussinessCell", for: indexPath) as! BussinessCell

        let tableObjects:StorageSubCatagoriezTwo = subCatagorizeDataTwo[indexPath.row]

        let charCheck = (tableObjects.address).replacingOccurrences(of: " ", with: "")
        cell.bussinessWebView.scrollView.isScrollEnabled = false

        var strings = ""

        if regId == 1
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row]

            if tableObjects.address.characters.isEmpty != true
            {
                 strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "")
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "")
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://localbluepages.com.au/images/icons/email.gif\"> "+tableObjects.email+"<br>" : "")
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
                 strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "")
            }
            else{

            }
        }
        else if regId == 2
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoEast = subCatagorizeDataTwoE[indexPath.row]
            //cell.bussinessName.text = tableObjects.nameString
            if tableObjects.address.characters.isEmpty != true
            {
              //  cell.bussinessName.text = tableObjects.nameString
                strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "")
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "")
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://localbluepages.com.au/images/icons/email.gif\"> "+tableObjects.email+"<br>" : "")
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
                strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "")
            }
            else{

            }
        }
        else if regId == 3
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoSouth = subCatagorizeDataTwoS[indexPath.row]
           // cell.bussinessName.text = tableObjects.nameString
            if tableObjects.address.characters.isEmpty != true
            {
                //cell.bussinessName.text = tableObjects.nameString
                strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "")
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "")
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://localbluepages.com.au/images/icons/email.gif\"> "+tableObjects.email+"<br>" : "")
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
                strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "")
            }
            else{

            }
        }
        else if regId == 4
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoWest = subCatagorizeDataTwoW[indexPath.row]
        //    cell.bussinessName.text = tableObjects.nameString
            if tableObjects.address.characters.isEmpty != true
            {
              //  cell.bussinessName.text = tableObjects.nameString
                strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "")
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "")
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://localbluepages.com.au/images/icons/email.gif\"> "+tableObjects.email+"<br>" : "")
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
                strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "")
            }

        }
        else{
            let tableObjects:StorageSubCatagoriezTwo = subCatagorizeDataTwo[indexPath.row]
            cell.bussinessName.text = tableObjects.nameString

            let charCheck = (tableObjects.address).replacingOccurrences(of: " ", with: "")
            print(charCheck)
            cell.bussinessName.text = " "

            var strings = ""

            strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.nameString+"</b></font><br>" : "")
            strings = strings+(tableObjects.nameString.characters.count > 0 ? "<font size=\"3\"><b>"+tableObjects.address+"</b></font><br>" : "")
            // strings = strings+(tableObjects.address.characters.count > 0 ? "<img src=\"http://getmydetails.pe.hu/img/yt.jpg\"> "+tableObjects.address+"<br>" : "")

            strings = strings+(tableObjects.telephone.characters.count > 0 ? "<img style='width:22px; height:22px;' src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br>" : "")

            strings = strings+(tableObjects.email.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://getmydetails.pe.hu/img/email.gif\"> "+tableObjects.email+"<br>" : "")

            strings = strings+(tableObjects.website.characters.count > 0 ? "<img style='width:22px; height:22px;'  src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website : "")

            // cell.bussinessWebView.loadHTMLString("<font size=\"5\"><b>"+tableObjects.nameString+"</b></font><br><img src=\"http://getmydetails.pe.hu/img/yt.jpg\"> "+tableObjects.address+"<br><img src=\"http://getmydetails.pe.hu/img/mobile.gif\"> "+tableObjects.telephone+"<br><img src=\"http://getmydetails.pe.hu/img/email.gif\"> "+tableObjects.email+"<br><img src=\"http://getmydetails.pe.hu/img/website.gif\"> "+tableObjects.website, baseURL: nil)

            cell.bussinessWebView.loadHTMLString(strings, baseURL: nil)
            let heightIndex = strings.components(separatedBy: "<br>")
            let heightMul = heightIndex.count
            print(heightMul*32)

            self.heightOfTheCell = CGFloat(((heightMul)*32)+4)


        }
        return cell
    }
    else if indexPath.section == 1
    {
        let cell = self.bussinessTableView.dequeueReusableCell(withIdentifier: "bussinessCell2")
        cell?.textLabel?.textAlignment = .center
        switch regId
        {
        case 0:
            if subCatagorizeDataTwo.count != forAll
            {
                cell?.textLabel?.text = "LOAD MORE DATA"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "NO MORE FOUND!"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }
        case 1:
            if subCatagorizeDataTwoN.count != forN
            {
                cell?.textLabel?.text = "LOAD MORE DATA"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "NO MORE FOUND!"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }
        case 2:
            if subCatagorizeDataTwoE.count != forE
            {
                cell?.textLabel?.text = "LOAD MORE DATA"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "NO MORE FOUND!"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }
        case 3:
            if subCatagorizeDataTwoS.count != forS
            {
                cell?.textLabel?.text = "LOAD MORE DATA"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "NO MORE FOUND!"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }
        case 4:
            if subCatagorizeDataTwoW.count != forW
            {
                cell?.textLabel?.text = "LOAD MORE DATA"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "NO MORE FOUND!"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }

        default:break
        }

        return cell!
    }
    else
    {
        let tableObjects:StorageSubCatagoriez3rd = SubCatagoriez3rd[indexPath.row]
        let cell = self.bussinessTableView.dequeueReusableCell(withIdentifier: "bussinessCell3")
        cell?.textLabel?.text = tableObjects.nameString
        return cell!

    }
}

请看截图:

显示全部时

显示南部地区时

而且,如第二个屏幕截图(南部区域)所示,该配置文件属于北部区域,因此本应被隐藏。

请让我知道如何解决此问题。

感谢您的时间:)

模型:

class StorageSubCatagoriezTwo
{
var nameString:String
var idString:String
var address:String
var telephone:String
var email:String
var website:String

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String)
{
    self.nameString = tempName.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.idString  = tempID.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.address = tempaddress.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.telephone = tempTelephone.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.email = tempEmail.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.website = tempWebsite.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
}
}

    class StorageSubCatagoriezTwoNorth

{
var nameString:String
var idString:String
var address:String
var telephone:String
var email:String
var website:String

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String)
    {
    self.nameString = tempName.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.idString  = tempID.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.address = tempaddress.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.telephone = tempTelephone.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.email = tempEmail.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.website = tempWebsite.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
}
}

class StorageSubCatagoriezTwoEast
{
var nameString:String
var idString:String
var address:String
var telephone:String
var email:String
var website:String

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String)
{
    self.nameString = tempName.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.idString  = tempID.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.address = tempaddress.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.telephone = tempTelephone.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.email = tempEmail.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.website = tempWebsite.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
}
}

class StorageSubCatagoriezTwoWest
{
var nameString:String
var idString:String
var address:String
var telephone:String
var email:String
var website:String

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String)
{
    self.nameString = tempName.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.idString  = tempID.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.address = tempaddress.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.telephone = tempTelephone.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.email = tempEmail.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.website = tempWebsite.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)    }
}

class StorageSubCatagoriezTwoSouth
{
var nameString:String
var idString:String
var address:String
var telephone:String
var email:String
var website:String

init(tempName:String,tempID:String,tempaddress:String,tempTelephone:String,tempEmail:String,tempWebsite:String)
{
    self.nameString = tempName.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.idString  = tempID.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.address = tempaddress.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.telephone = tempTelephone.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.email = tempEmail.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
    self.website = tempWebsite.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines)
}
}

获取区域并分配给var:

let id = String(describing: dictionary["id"]!)
                        let name = String(describing: dictionary["BUSINESS_NAME"]!)
                        let email = String(describing: dictionary["EMAIL"]!)
                        var streetNo = String(describing: dictionary["STREET_NO"]!)
                        var streetName = String(describing: dictionary["STREET_NAME"]!)
                        var suburbID = String(describing: dictionary["SUBURB_ID"]!)
                        var postCode = String(describing: dictionary["POSTCODE"]!)
                        let regionID = String(describing: dictionary["REGION_ID"]!)
                        var landline = String(describing: dictionary["PHONE_FIXED"]!)
                        var mobile = String(describing: dictionary["PHONE_MOBILE"]!)
                        let website = String(describing: dictionary["WEBURL"]!)
                        var suburbName = String(describing: dictionary["Suburb"]!)
                        let streedNoHide = String(describing: dictionary["STREET_NO_HIDDEN"]!)
                        let streetNameHide = String(describing: dictionary["STREET_NAME_HIDDEN"]!)
                        let suburbHide = String(describing: dictionary["SUBURB_HIDDEN"]!)
                        let postCodeHide = String(describing: dictionary["POSTCODE_HIDDEN"]!)
                        let mobileHide = String(describing: dictionary["PHONE_MOBILE_HIDDEN"]!)
                        let landlineHide = String(describing: dictionary["PHONE_FIXED_HIDDEN"]!)

JSON数据:(REGION_ID)

在此处输入图片说明

1)他们应该是2个不同的数组。

var mainArray = [StorageSubCatagories]()
var filteredArray = [StorageSubCatagories]()

具有所有对象的第一个数组。 (mainArray)

第二个数组只有主数组过滤的对象。 (filteredArray)

2) 您应该使用“ StorageSubCatagories”之类的通用类来解析所有数据, 而不是使用诸如StorageSubCatagoriezTwo,StorageSubCatagoriezTwoNorth,StorageSubCatagoriezTwoEast ...等多个类, 并将所有对象存储到单个数组中,如下所示:

3)此功能将根据按钮选择返回过滤后的数据。 对于按钮,根据您的值,均通过regionId = 0,对于北部regionId = 1,....。

 func fetchResultForSelection(_ regionId: Int) -> [StorageSubCatagories] {
       if regionId == 0 {
           return mainArray
       } 
       let tempArray = mainArray.filter { $0.regId == regionId }
       return tempArray
    } 

在删除之前的对象之后,将从此函数返回的数组对象添加到filteredArray中。

4)始终基于filteredArray而不是mainArray显示表数据。

希望这对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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