gpt4 book ai didi

ios - UiCollectionView 单元格选择错误

转载 作者:行者123 更新时间:2023-11-29 01:10:13 25 4
gpt4 key购买 nike

我已经使用 Alamofire 和 SwiftyJSON 来填充 UiCollectionview 并且它工作正常但是 didSelectItemAtIndexPath 函数显示数组超出索引认为我已经打印了数组计数并且它不是空的

任何建议

这是我的代码:-

模型

import Foundation

class ProductModel {
private var _ProductItemId: String!
private var _ProductMainCategory: String!
private var _ProductCategoryId: String!
private var _ProductName: String!
private var _ProductItemNo: String!
private var _ProductAvalaibility: String!
private var _ProductSeoDesc: String!
private var _ProductImageURL: String!
private var _ProductBrand_ID: String!
private var _ProductCat_name: String!

//Level 1
private var _ProductTotalQuantity : String!
private var _Productprice : String!
private var _ProductSalePrice : String!
private var _ProductWeightName : String!
private var _ProductCode : String!




var ProductItemId : String {
return _ProductItemId
}

var ProductMainCategory : String {
return _ProductMainCategory
}

var ProductCategoryId : String {
return _ProductCategoryId
}

var ProductName : String {
return _ProductName
}

var ProductItemNo : String {
return _ProductItemNo
}

var ProductAvalaibility : String {
return _ProductAvalaibility
}

var ProductSeoDesc : String {
return _ProductSeoDesc
}

var ProductImageURL : String {
return _ProductImageURL
}

var ProductBrand_ID: String {
return _ProductBrand_ID
}

var ProductCat_name: String {
return _ProductCat_name
}

//Level 1
var ProductTotalQuantity : String {
return _ProductTotalQuantity
}

var Productprice : String {
return _Productprice
}

var ProductSalePrice : String {
return _ProductSalePrice
}

var ProductWeightName : String {
return _ProductWeightName
}

var ProductCode : String {
return _ProductCode
}



//Initilizer
init(ProductImageURL : String, ProductName : String, Productprice : String, ProductSalePrice : String)
{

self._ProductName = ProductName
self._ProductImageURL = ProductImageURL//

//Level 1
self._Productprice = Productprice//
self._ProductSalePrice = ProductSalePrice//

}

我的 CollectionView 委托(delegate)和数据源

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ProductCell", forIndexPath: indexPath)as? ProductCell {

let _prod: ProductModel!
_prod = prod [indexPath.row]
cell.configureCell(_prod)
return cell
}
else{
return UICollectionViewCell()
}


}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let prodDetail: ProductModel!
prodDetail = prod[indexPath.row] //error Array index out of range
print(prodDetail.Productprice)
performSegueWithIdentifier("productDetailSegue", sender: prodDetail)

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//if inSearchMode{
//return filteredProd.count
// }
return prod.count
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

调用API并解析

    Alamofire.request(.POST, "http://www.picknget.com/webservice/index.php/Home/filter_grocery_product_practice/", parameters: parameterDictionary as? [String : AnyObject])
.responseJSON { response in
if let value = response.result.value {
let json = JSON(value)
print(json)
if let _statusCode = json["status"].string {
// print("the ststus code is ", _statusCode)
if (_statusCode == "1"){
self.parseJSON(json)
}
if (_statusCode == "0"){
SwiftSpinner.hide({
self.callAlert("OOP's", _msg: "No More Product is available in this section right now")
})
}
}
//print ("json result ", json)

}
}.responseString { response in
//print("response ",response.result.value)
}
}

func parseJSON(json: JSON) {
for result in json["cat"].arrayValue {
let name = result["Name"]

let aString: String = "\(result["ImageURL"])"
let product_Image_Url = aString.stringByReplacingOccurrencesOfString("~", withString: "http://www.picknget.com", options: NSStringCompareOptions.LiteralSearch, range: nil)
let price = result["cat_price"][0]["Price"].string
let SalePrice = result["cat_price"][0]["SalePrice"].string


let product = ProductModel(ProductImageURL: "\(product_Image_Url)", ProductName: "\(name)", Productprice: "\(price!)", ProductSalePrice: "\(SalePrice!)")
prod.append(product)
}

print("@@@@@@@@")
print(prod.count)
dispatch_async(dispatch_get_main_queue(),{
self.productCollect.reloadData()
});
}

最佳答案

根据您的意见,我认为问题与您如何为 Collection View 设置获取的产品有关。

parseJSON 函数很可能在辅助线程上执行。这实际上是方法 responseJSON 的完成处理程序的相同执行上下文。

在函数 parseJSON 中你有这样的语句:

    prod.append(product)

这里,prod 不应该 是一个全局变量或者不是 view controller 的成员变量!使其成为函数 parseJSON 中的本地 变量!

你的 View Controller 也应该有这个数组的属性,例如产品。这用作 View Controller 的“模型”。它将只能从主线程访问。

parseJSON 中,将产品分配给 View Controller ,如下所示:

func parseJSON(json: JSON) {
var tmpProducts: [Product] = []
for result in json["cat"].arrayValue {
let name = result["Name"]

let aString: String = "\(result["ImageURL"])"
let product_Image_Url = aString.stringByReplacingOccurrencesOfString("~", withString: "http://www.picknget.com", options: NSStringCompareOptions.LiteralSearch, range: nil)
let price = result["cat_price"][0]["Price"].string
let SalePrice = result["cat_price"][0]["SalePrice"].string


let product = ProductModel(ProductImageURL: "\(product_Image_Url)", ProductName: "\(name)", Productprice: "\(price!)", ProductSalePrice: "\(SalePrice!)")
tmpProducts.append(product)
}

dispatch_async(dispatch_get_main_queue(),{
self.products = tmpProducts // assuming `self` is the ViewController
self.productCollect.reloadData()
});
}

注意:您需要相应地更改您的数据源委托(delegate),例如访问“模型”(self.products.count)等

关于ios - UiCollectionView 单元格选择错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35846014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com