Ios 如何将当前单元格中的数据与其在TableView Swift 4中的前一个单元格中的数据进行比较?
我有一个JSON是这样的:Ios 如何将当前单元格中的数据与其在TableView Swift 4中的前一个单元格中的数据进行比较?,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个JSON是这样的: "product": [ { "product_id": 471, "info": "123456", }, { "product_id": 471, "info": "356697456", }, { "product_id": 472, "info": "1432", }, { "product_i
"product": [
{
"product_id": 471,
"info": "123456",
},
{
"product_id": 471,
"info": "356697456",
},
{
"product_id": 472,
"info": "1432",
},
{
"product_id": 473,
"info": "4321",
},
]
我想将我的TableView设置为类似下图:
我想要的是:
产品标识
与前一个单元格的产品标识
相同,则产品1不再显示,它将消失产品id
与前一个单元格(第二个单元格)不同,因此显示红色标签产品2产品3
和表格视图中的单元格其余部分
cellAtRow
委托中获取indexPath,因此我将每个单元格的product_id
与前一个单元格进行比较,然后控制其中的逻辑
这是我的密码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
let thisIndexPath = indexPath.row
if thisIndexPath - 1 > -1 {
let previousProductId = self.productItem[thisIndexPath - 1].productId
let thisProductId = self.productItem[thisIndexPath].productId
if previousProductId == thisProductId {
cell.productLabel.isHidden = true
cell.productHeight.constant = 0
cell.productnameTopContraint.constant = 0
cell.productnameBottomContraints.constant = 0
}else {
cell.productnameLabel.isHidden = false
}
}else{
cell.productnameLabel.isHidden = false
}
cell.item = selfProductItem[indexPath.row]
return cell
}
}
但现在的问题是:
--当TableView
第一次启动时,UI显示如上图所示,但当我开始滚动时,所有单元格的产品标签(红色)都消失了,尽管Product\u id
与上一个单元格的Product\u id
不同
--当我滚动回到第一个单元格时,产品标签(红色)也消失了。这意味着UI只在屏幕第一次启动时才正确,这是不持久的
所以我的问题是:
cellForRowAt
delegate方法中进行比较是否正确?如果不正确,我应该在哪里进行比较我认为在本例中,您可以将表视图的单元格划分为多个部分,并为每个部分分配标题(产品名称)。有关更多信息,请参阅。我认为在这种情况下,您可以将表格视图的单元格划分为多个部分,并为每个部分指定标题(产品名称)。有关更多信息,请参阅。我认为要解决您的问题,您应该考虑如何存储JSON数据 您可以先创建一个名为“Product”的结构,该结构将存储产品信息,然后将其设置为“Equalable”,您可以添加一个函数,该函数允许您比较productID的:
/// Structure Of A Product
struct Product: Equatable{
var productID: Int
var productInfo: Int
static func == (lhs: Product, rhs: Product) -> Bool {
return lhs.productID == rhs.productID
}
}
现在,要使用此结构,您可以创建一个数组变量来存储产品:
//Array To Store Array Of Product
var products = [Product]()
在本例中,我只是手动输入产品信息,但您应该以更好的方式处理此问题。但是,它确实说明了一种可以“开始”处理此问题的方法:
override func viewDidLoad() {
super.viewDidLoad()
//1. Create Products
let productOne = Product(productID: 471, productInfo: 123456)
let productTwo = Product(productID: 471, productInfo: 356697456)
let productThree = Product(productID: 472, productInfo: 1432)
let productFour = Product(productID: 473, productInfo: 4321)
//2. Add Them To The Products Array
addUnique(productOne)
addUnique(productTwo)
addUnique(productThree)
addUnique(productFour)
}
/// Checks That A Product Doesn't Exist
///
/// - Parameter product: Product
func addUnique(_ product: Product) {
if !products.contains(product) {
products.append(product)
}
}
在步骤1中,我们手动创建产品
在步骤2中,我们将调用addUnique(u-product)函数,该函数只允许存储唯一的产品
在确保没有重复的ProductID之后,您可以轻松地设置格式:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.productLabel.text = products[indexPath.row].productID
cell.productnameLabel.text = products[indexPath.row].productInfo
}
当然,您需要修复标签的任何颜色等。我认为要解决您的问题,您应该考虑如何存储JSON数据 您可以先创建一个名为“Product”的结构,该结构将存储产品信息,然后将其设置为“Equalable”,您可以添加一个函数,该函数允许您比较productID的:
/// Structure Of A Product
struct Product: Equatable{
var productID: Int
var productInfo: Int
static func == (lhs: Product, rhs: Product) -> Bool {
return lhs.productID == rhs.productID
}
}
现在,要使用此结构,您可以创建一个数组变量来存储产品:
//Array To Store Array Of Product
var products = [Product]()
在本例中,我只是手动输入产品信息,但您应该以更好的方式处理此问题。但是,它确实说明了一种可以“开始”处理此问题的方法:
override func viewDidLoad() {
super.viewDidLoad()
//1. Create Products
let productOne = Product(productID: 471, productInfo: 123456)
let productTwo = Product(productID: 471, productInfo: 356697456)
let productThree = Product(productID: 472, productInfo: 1432)
let productFour = Product(productID: 473, productInfo: 4321)
//2. Add Them To The Products Array
addUnique(productOne)
addUnique(productTwo)
addUnique(productThree)
addUnique(productFour)
}
/// Checks That A Product Doesn't Exist
///
/// - Parameter product: Product
func addUnique(_ product: Product) {
if !products.contains(product) {
products.append(product)
}
}
在步骤1中,我们手动创建产品
在步骤2中,我们将调用addUnique(u-product)函数,该函数只允许存储唯一的产品
在确保没有重复的ProductID之后,您可以轻松地设置格式:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.productLabel.text = products[indexPath.row].productID
cell.productnameLabel.text = products[indexPath.row].productInfo
}
当然,您需要修复标签的任何颜色等。也可以尝试在其他部分设置高度限制。
else
此部分:如果以前的ProductId==thisProductId{
和thisIndexXPath-1>-1{也尝试在else部分设置高度约束。
else
这一部分:如果先前的ProductId==thisProductId{
和thisIndexPath-1>-1{,那么这:如果thisIndexPath-1>-1{我试过了,它工作正常。我为您制作了一个虚拟数组。请检查下面的代码
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblProduct: UITableView!
var arrProduct = NSMutableArray()
var arrForSection = NSMutableArray()
var arrForProductId = NSMutableArray()
override func viewDidLoad()
{
super.viewDidLoad()
let dict = NSMutableDictionary()
dict.setValue("471", forKey: "product_id")
dict.setValue("123456", forKey: "info")
arrProduct.add(dict)
let dict1 = NSMutableDictionary()
dict1.setValue("471", forKey: "product_id")
dict1.setValue("356697456", forKey: "info")
arrProduct.add(dict1)
let dict2 = NSMutableDictionary()
dict2.setValue("472", forKey: "product_id")
dict2.setValue("1432", forKey: "info")
arrProduct.add(dict2)
let dict3 = NSMutableDictionary()
dict3.setValue("472", forKey: "product_id")
dict3.setValue("4321", forKey: "info")
arrProduct.add(dict3)
print(arrProduct)
self.createSection()
}
//MARK:
//MARK: Create section
func createSection()
{
arrForSection.removeAllObjects()
let arrtemp = NSMutableArray()
arrtemp.addObjects(from: (self.arrProduct as NSArray) as! [Any])
for i in 0 ..< arrtemp.count
{
let dict = self.arrProduct[i] as! NSMutableDictionary
let strProductId = (dict["product_id"] as? String)!
if(!arrForProductId .contains(strProductId))
{
arrForProductId.add(strProductId)
}
}
for j in 0 ..< arrForProductId.count
{
let strTempDate:String = arrForProductId .object(at: j) as! String
let arr1 = NSMutableArray()
for i in 0 ..< arrtemp.count
{
let dict = arrtemp .object(at: i) as! NSMutableDictionary
let strProductId = (dict["product_id"] as? String)!
if(strProductId == strTempDate)
{
arr1.add(dict)
}
}
arrForSection.add(arr1)
}
self.tblProduct.reloadData()
}
//MARK:
//MARK: TableView Delegate
func numberOfSections(in tableView: UITableView) -> Int {
return self.arrForSection.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (((arrForSection .object(at: section)) as! NSMutableArray).count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell:UITableViewCell = self.tblProduct.dequeueReusableCell(withIdentifier: "cell")!
let dictData = ((arrForSection .object(at: indexPath.section)) as! NSMutableArray).object(at: indexPath.row) as! NSDictionary
cell.textLabel?.text = dictData["info"] as? String
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return arrForProductId[section] as? String
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
类ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource{
@IBOutlet弱var tblProduct:UITableView!
var arrProduct=NSMutableArray()
var arrForSection=NSMutableArray()
var arrForProductId=NSMutableArray()
重写func viewDidLoad()
{
super.viewDidLoad()
设dict=NSMutableDictionary()
dict.setValue(“471”,forKey:“产品id”)
dict.setValue(“123456”,forKey:“info”)
ARR产品添加(dict)
设dict1=NSMutableDictionary()
第1条设定值(“471”,forKey:“产品id”)
dict1.setValue(“356697456”,forKey:“info”)
arrProduct.add(第1条)
设dict2=NSMutableDictionary()
第2条设定值(“472”,forKey:“产品id”)
dict2.设置值(“1432”,forKey:“info”)
arrProduct.add(第2条)
设dict3=NSMutableDictionary()
第3条设定值(“472”,forKey:“产品id”)
dict3.设置值(“4321”,forKey:“info”)
arrProduct.add(第3条)
印刷品(产品)
self.createSection()
}
//标记:
//标记:创建节
func createSection()
{
arrForSection.removeAllObjects()
设arrtemp=NSMutableArray()
arrtemp.addObjects(from:(self.arrProduct as NSArray)as![Any])
对于0中的i..