gpt4 book ai didi

iOS Swift - prepareForSegue - indexPathForSelectedRow

转载 作者:行者123 更新时间:2023-11-29 01:43:57 31 4
gpt4 key购买 nike

我想知道是否有办法使用不同的“indexPath”值作为 segue 中使用的键...

原因是,我有未分组和排序的 JSON 提要数据:

{ "results":
[
{
"BusCat01": "Household",
"CompanyDescription": "Household",
"CompanyName": "Bed \u0026 Bath Store",
"objectId": "Vq3lmoE0PA",
},
{
"BusCat01": "Food",
"CompanyDescription": "Hot Dogs",
"CompanyName": "Wiener Schnitzl",
"objectId": "xcCeuVoexD",
},
{
"BusCat01": "Clothing",
"CompanyDescription": "Clothing",
"CompanyName": "Banana Republic",
"objectId": "FGV8QuHmiw",
}
]
}

我使用了 JSON,然后编写了自己的分组和排序代码。当我在 TableView 中显示分组和排序数据时,序号(行键)不对应 JSON 键。这会损坏主/详细数据。基本上,我在 TableView 中得到了给定主记录的错误详细信息。示例:

println("cellForRowAtIndexPath indexPath: \(indexPath)")

打印:

cellForRowAtIndexPath indexPath: {length = 2, path = 0 - 0}

cellForRowAtIndexPath indexPath: {length = 2, path = 0 - 1}

cellForRowAtIndexPath indexPath: {length = 2, path = 0 - 2}

cellForRowAtIndexPath indexPath: {length = 2, path = 0 - 3}

cellForRowAtIndexPath indexPath: {length = 2, path = 1 - 0}

cellForRowAtIndexPath indexPath: {length = 2, path = 1 - 1}

cellForRowAtIndexPath indexPath: {length = 2, path = 2 - 0}

cellForRowAtIndexPath indexPath: {length = 2, path = 2 - 1}

cellForRowAtIndexPath indexPath: {length = 2, path = 3 - 0}

看到“path = ...”???

问题:是否可以使用我的 JSON“objectId”作为“indexPathForSelectedRow()?.row”值,而不是“prepareForSegue”中的 tableViews 部分和行值? (满口,我知道...)

这是分组和排序:

func getData_VendorsByCategory() {
clearData()
if vendors.count > 0 {
for object in vendors {
let key = object.busCat01
if vendsInCatDict.indexForKey(key!) != nil { // Add item to a pre-existing List that contains pre-existing Items
vendsInCatDict[key!]?.append(object.companyName!)
vendsArr.append(object.companyName!)
vendsArr = sorted(vendsArr, descending)
} else { // Create an array and add item to it
vendsInCatDict[key!] = [object.companyName!]
catsArr.append(key!)
catsArr = sorted(catsArr, descending)
vendsArr.append(object.companyName!)
vendsArr = sorted(vendsArr, descending)
}
}
}
}

func getData_VendorsByName() {
clearData()
if vendors.count > 0 {
for object in vendors {
let key = object.companyName as String?
var index = advance(key!.startIndex, 1)
var firstCharacter = key!.substringToIndex(index)

if vendsInCatDict.indexForKey(firstCharacter) != nil { // Add item to a pre-existing List that contains pre-existing Items
vendsInCatDict[firstCharacter]?.append(object.companyName!)
vendsArr.append(object.companyName!)
vendsArr = sorted(vendsArr, descending)
} else { // Create an array and add item to it
vendsInCatDict[firstCharacter] = [object.companyName!]
catsArr.append(firstCharacter)
catsArr = sorted(catsArr, descending)
vendsArr.append(object.companyName!)
vendsArr = sorted(vendsArr, descending)
}
}
}
}

最佳答案

indexPath 指示 TableView 中的位置。如果您使用数组在 UITableViewDataSource 中构建表格,则只需使用 indexPathForSelectedRow()?.row 和/或 indexPathForSelectedRow()?.section 作为这个数组的索引。

编辑:根据您的评论,您需要反转用于获取 nameSection 的步骤。

    let key = catsArr[indexPath.section]
let nameSection = vendsInCatDict[key]!
cell.textLabel?.text = nameSection[indexPath.row]
return cell

因此,给定单元格的 indexPath,您可以使用前两行检索 keynameSection。给定这些值,您将需要一个函数,该函数可以根据对象的键/名称部分在原始结果数组中找到对象(反向 getData_VendorsByCategory 和/或 getData_VendorsByName)。我建议在进行分组和排序时创建查找数组或字典。

例如,如果您添加了如下内容:

// Top of class
var vendorMap : [String:AnyObject] = []

// In group/search
for object in vendors {
let key = object.companyName as String?
vendorMap[key!] = object

您可以像这样检索供应商对象:

let key = catsArr[indexPath.section]
let nameSection = vendsInCatDict[key]!
let vendor = vendorMap[nameSection[indexPath.row]]

关于iOS Swift - prepareForSegue - indexPathForSelectedRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32121008/

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