[英]iOS Swift - prepareForSegue - indexPathForSelectedRow
我想知道是否有一種方法可以使用不同的“ indexPath”值作為segue中使用的鍵...
原因是,我有未分組和排序的JSON feed數據:
{ "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鍵。 這會破壞主數據/明細數據。 基本上,在表視圖中,給定的主記錄我得到了錯誤的詳細信息。 例:
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}
參見“路徑= ...” ???
問題:是否可以將我的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
指示表視圖中的位置。 如果使用數組在UITableViewDataSource
構建表,則可以僅使用indexPathForSelectedRow()?.row
和/或indexPathForSelectedRow()?.section
作為此數組的索引。
編輯:根據您的評論,您將需要撤銷用於獲取nameSection
的步驟。
let key = catsArr[indexPath.section]
let nameSection = vendsInCatDict[key]!
cell.textLabel?.text = nameSection[indexPath.row]
return cell
因此,給定單元格的indexPath
,您可以使用前兩行檢索key
和nameSection
。 給定這些值,您將需要一個函數,該函數可以根據原始結果數組的key / nameSection(反向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]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.