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

问题描述 投票:0回答:2

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

我有四个区域: - 北部、东部、南部、西部和全部

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

此时,一切都是随机的。有些配置文件在多个地区可用,它们确实会出现,而有些则不会。某些配置文件本应仅出现在一个区域中,但却出现在 2 个区域中。

我不知道如何分享代码或屏幕截图,但很乐意提供更多详细信息以查看通话。

谢谢

我的代码:

 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.businessTableView.dequeueReusableCell(withIdentifier: "businessCell", for: indexPath) as! BusinessCell
        
        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.businessName.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.businessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoEast = subCatagorizeDataTwoE[indexPath.row]
            cell.businessName.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.businessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoSouth = subCatagorizeDataTwoS[indexPath.row]
            cell.businessName.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.businessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoWest = subCatagorizeDataTwoW[indexPath.row]
            cell.businessName.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.businessName.text = tableObjects.nameString
            
            let charCheck = (tableObjects.address).replacingOccurrences(of: " ", with: "")
            
            cell.businessName.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.businessTableView.dequeueReusableCell(withIdentifier: "businessCell2")
        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.businessTableView.dequeueReusableCell(withIdentifier: "businessCell3")
        cell?.textLabel?.text = tableObjects.nameString
        return cell!
        
    }
}

enter image description here

ios xcode uitableview swift3
2个回答
0
投票

据我所知,如果您想从数组中删除重复项,请使用以下逻辑与您的

datatype

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)

输出

[1,2,5,8,3]

更换此行

let tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row]

有了这个

  var tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row]
    tableObjects = removeDuplicate(sourceArray: [tableObjects.nameString ])

对所有地区都这样做


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

© www.soinside.com 2019 - 2024. All rights reserved.