Swift 3:删除重复项/在uitableviewcell中将它们分组为1

时间:2017-08-03 03:10:29

标签: ios xcode uitableview swift3

我正在尝试删除从外部数据库中提取的重复条目/将它们组合为一个。最初,我的sql查询有一个group子句。当我将它用作组子句时,查询工作正常,但不合适。然后我从查询中删除了group子句,它现在显示了重复的条目。

我有四个区域: - 北,东,南,西和所有

默认显示所有区域。现在可能有一个出现在多个地区的个人资料。因此,当它作为默认加载 - 所有区域时,配置文件只应出现在那里,但是当我们在区域上选项卡时,如果配置文件属于该区域,则配置文件也应显示在那里。

在他们的时刻,它显示所有随机。有些配置文件可以在多个区域中使用,它们确实会出现,有些则不会出现。某些仅在一个区域内的配置文件确实出现在两个区域中。

我不知道如何分享代码或屏幕截图,但很高兴提供更多细节来查看电话。

由于

我的代码:

 func removeDuplicate (sourceArray: [String]) -> [String] {
    var uniqueArray:[String] = []
    for object in sourceArray {
        if !uniqueArray.contains(object)
        {
            uniqueArray.append(object)
        }
    }
    return uniqueArray
}


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 testing = removeDuplicate(sourceArray: [tableObjects.nameString])
        print(testing.description)

        //let charCheck = (tableObjects.address).replacingOccurrences(of: " ", with: "")


 if reg == "North"
        {
            cell.bussinessName.text = " "
            cell.addLabel.text = " "
            cell.telLabel.text = " "
            cell.emailLabel.text =  " "
            cell.webLabel.text = " "
            let tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row]


            if tableObjects.address.characters.isEmpty != true
            {
                cell.addLabel.text = tableObjects.address
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                cell.telLabel.text = tableObjects.telephone
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                cell.emailLabel.text = tableObjects.email
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
                cell.webLabel.text = tableObjects.website
            }
            else{

            }
        }
        else if reg == "East"
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoEast = subCatagorizeDataTwoE[indexPath.row]
            cell.bussinessName.text = tableObjects.nameString
            if tableObjects.address.characters.isEmpty != true
            {
                cell.addLabel.text = tableObjects.address
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                cell.telLabel.text = tableObjects.telephone
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                cell.emailLabel.text = tableObjects.email
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true
            {
                cell.webLabel.text = tableObjects.website
            }
            else{

            }
        }
        else if reg == "South"
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoSouth = subCatagorizeDataTwoS[indexPath.row]
            cell.bussinessName.text = tableObjects.nameString
            if tableObjects.address.characters.isEmpty != true
            {
                cell.addLabel.text = tableObjects.address
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                cell.telLabel.text = tableObjects.telephone
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                cell.emailLabel.text = tableObjects.email
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
                cell.webLabel.text = tableObjects.website
            }
            else{

            }
        }
        else if reg == "West"
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoWest = subCatagorizeDataTwoW[indexPath.row]
            cell.bussinessName.text = tableObjects.nameString
            if tableObjects.address.characters.isEmpty != true
            {
              cell.addLabel.text = tableObjects.address
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                cell.telLabel.text = tableObjects.telephone
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                cell.emailLabel.text = tableObjects.email
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
               cell.webLabel.text = tableObjects.website
            }

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

            let charCheck = (tableObjects.address).replacingOccurrences(of: " ", with: "")

            cell.bussinessName.text = tableObjects.nameString
            cell.addLabel.text = tableObjects.address
            cell.telLabel.text = tableObjects.telephone
            cell.emailLabel.text = tableObjects.email
            cell.webLabel.text = tableObjects.website

        }


        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 Data 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 Data 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 Data 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 Data 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 Data 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!

    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

根据我的知识,如果你想从数组中删除重复数据,那么请使用以下逻辑与{if !$priceDisplay || $priceDisplay == 2} {assign var='productPrice' value=$product->getPrice(true, $smarty.const.NULL, $priceDisplayPrecision)} {assign var='productPriceWithoutReduction' value=$product->getPriceWithoutReduct(false, $smarty.const.NULL)} {elseif $priceDisplay == 1} {assign var='productPrice' value=$product->getPrice(false, $smarty.const.NULL, $priceDisplayPrecision)} {assign var='productPriceWithoutReduction' value=$product->getPriceWithoutReduct(true, $smarty.const.NULL)} {/if}

datatype

<强>输出

[1,2,5,8,3]

替换此行

func removeDublicate (sourceArray: [Int]) -> [Int] {
    var uniqueArray:[Int] = []
    for object in sourceArray {
        if !uniqueArray.contains(object)
        {
            uniqueArray.append(object)
        }
    }
    return uniqueArray
}

let testing = removeDublicate(sourceArray: [1,2,2,5,1,8,3,1,2,8,5])
print(testing)

有了这个

let tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row]

为所有地区做这件事

答案 1 :(得分:-1)

在swift 3 中,您可以使用此代码从数组中删除重复项。只需要这么多代码。 试试此代码..

import UIKit

class ViewController2: UIViewController {

   var myArray: [Int] = [1,2,3,4,3,2,5,7,5,8]

   override func viewDidLoad()
   {
       super.viewDidLoad()

       let uniqueArray = Array(Set(self.myArray))
       print(uniqueArray) // This print result will be [2, 4, 5, 7, 1, 8]
   }
}

<强>输出

[2,4,5,7,1,8]