Kunal Parekh
Kunal Parekh

Reputation: 380

Swift 3: Removing Duplicates / group them as 1 in uitableviewcell

I am trying to either remove the duplicate entries fetched from the external database /group them as one. Initially, my sql query had a group clause. When I used it as group clause, the query was working, but not appropriately. I then removed the group clause from the query, it now shows the duplicate entries.

I have four regions: - North, East, South,West and ALL

By default it shows all region. Now there could be a profile that appears in more than one region. So when it loads as default - all regions, the profile should only appear once there, but when we tab on the regions, the profile should also show up there if the profile belongs in that region.

At them moment, it is showing all random. Some profiles that are available in more than one region, they do appear and some they dont. Some profile that is meant to be only in one region, do appear in 2 regions.

I dont know how can I share the code or screenshots, but happy to provide more details to look at the call.

Thanks

My code:

 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

Upvotes: 0

Views: 473

Answers (2)

Jaydeep Vyas
Jaydeep Vyas

Reputation: 4480

As per my knowledge if you want to remove duplicate from array then use below logic with your 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)

OUTPUT

[1, 2, 5, 8, 3]

Replace this line

let tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row]

With this

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

Do it for all regions

Upvotes: 0

Wide Angle Technology
Wide Angle Technology

Reputation: 1222

In swift 3 you can use this code for removing duplicate from array. Only this much code is needed. Try this code..

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

OUTPUT

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

Upvotes: -1

Related Questions