Swift 3:在 uitableviewcell 中删除重复项/将它们分组为 1
Posted
技术标签:
【中文标题】Swift 3:在 uitableviewcell 中删除重复项/将它们分组为 1【英文标题】:Swift 3: Removing Duplicates / group them as 1 in uitableviewcell 【发布时间】:2017-08-03 03:10:28 【问题描述】:我正在尝试删除从外部数据库中提取的重复条目/将它们组合为一个。最初,我的 sql 查询有一个 group 子句。当我将它用作组子句时,查询正在工作,但不合适。然后我从查询中删除了 group 子句,它现在显示了重复的条目。
我有四个区域:- 北、东、南、西和所有
默认显示所有区域。现在可能会有一个配置文件出现在多个区域中。因此,当它默认加载时 - 所有区域,配置文件应该只在那里出现一次,但是当我们在区域上进行选项卡时,如果配置文件属于该区域,配置文件也应该显示在那里。
此时此刻,一切都是随机的。有些配置文件在多个地区可用,它们确实出现,有些则不出现。某些配置文件仅在一个区域中出现,但确实出现在 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.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!
【问题讨论】:
查看这个答案:***.com/a/44960181/8234523 什么是 tableObjects.nameString 你能给出源数组和你的预期输出 tableObjects.nameString 是从数据库中获取的配置文件的名称。 好的,我刚刚更新了我的问题。如果您可以在屏幕截图中看到,我有一个业务,例如 George's Concreting,这个业务重复了不止一次。实际上,这里应该只重复一次。当我按下 North 时,如果此商家在此区域中列出,则此商家应显示或隐藏。 @JaydeepVyas,tableObjects.nameString 保存从外部数据库获取的 Profile Name 的值。显然,表格视图的每个单元格上显示的值是重复的。我需要删除重复项 【参考方案1】:据我所知,如果您想从数组中删除重复项,请使用以下逻辑与您的 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 ])
在所有地区都这样做
【讨论】:
嗨 Jaydeep,我尝试了您的建议,但显然它仍然无法正常工作。我已经更新了上面的代码,请看一下。谢谢 第一次加载数据时,您必须从主数组中删除重复项 上述方法用于从数组中删除重复项,您的 tableObjects 似乎不是数组是方法` let tableObjects:StorageSubCatagoriez3rd = SubCatagoriez3rd[indexPath.row]` 好的。那么我该如何使用它来指出数组。因为“subCatagorizeDataTwo”包含数组值 我尝试了其他几种可能性,但无法弄清楚。你能帮我么?谢谢【参考方案2】:在 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]
【讨论】:
您的回答只是部分正确。 3 不会只删除它的重复项。最终输出将是 [2, 4, 5, 7, 3, 1, 8]以上是关于Swift 3:在 uitableviewcell 中删除重复项/将它们分组为 1的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 3 中将 UIButton 添加到我的 UITableViewCell 中?
在 Swift 3 中单击和双击 UITableViewCell
从另一个视图控制器单击 uitableviewcell 后,swift 3 更改 uitableviewcell?
仅在 Swift 3 中,圆形 UIImage 在 UITableViewCell 中消失
Swift 3:TPKeyboardAvoidingScrollView 在 UITableViewCell 中的一个 UITextField 上无法正常工作