2015-08-20 19 views
0

我在想,如果有使用不同的「indexPath」值是在賽格瑞使用的密鑰的方式...的iOS斯威夫特 - prepareForSegue - indexPathForSelectedRow

的原因是,我已經得到了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中顯示Grouped和Sorted數據時,序號(行鍵)不對應JSON鍵。這會破壞主/細節數據。基本上,我在表格視圖中給出了給定主記錄的錯誤詳細信息。例如:

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

打印:

的cellForRowAtIndexPath indexPath:{長度= 2,路徑= 0 - 0}

的cellForRowAtIndexPath indexPath:{長度= 2,路徑= 0 - 1}

的cellForRowAtIndexPath indexPath:{長度= 2,路徑= 0 - 2}

的cellForRowAtIndexPath indexPath:{長度= 2,路徑= 0 - 3}

的cellForRowAtIndexPath indexPath:{長度= 2,路徑= 1 - 0}

的cellForRowAtIndexPath indexPath:{長度= 2,路徑= 1 - 1}

的cellForRowAtIndexPath indexPath:{長度= 2,路徑= 2 - 0}

的cellForRowAtIndexPath indexPath:{長度= 2,路徑= 2 - 1}

的cellForRowAtIndexPath indexPath:{長度= 2,路徑= 3 - 0}

請參閱「path = ...」?

問:是否可以使用我的JSON「objectId」作爲「indexPathForSelectedRow()?。row」的值而不是tableViews的節和「prepareForSegue」中的行值? (一口,我知道...)

這裏是分組和排序:

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) 
      } 
     } 
    } 
} 
+0

你的問題不是很清楚 – boidkan

+0

你可以添加你'UITableViewDataSource'這個問題的方法?然後,我們將能夠看到您的索引是如何設置的。這些將是'numberOfSectionsInTableView','numberOfRowsInTableView'和'cellForRowAtIndexPath'。 –

+0

@boidkan - 這是我能做到的最好......這是非常複雜的 – user1481614

回答

1

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,你可以與前兩行檢索keynameSection。給定這些值,您將需要一個函數,該函數可以根據原始結果數組中的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]] 
+0

我使用一個Singleton對象: viewDidLoad中(){ 廠商= LibraryAPI.sharedInstance.getVendors() } ===然後通過我的分組運行的「供應商」和排序算法(Array的,日文N3 N4 N5)=== getData_VendorsByCategory(){...} getData_VendorsByName(){...} ===然後標記它的所有行動TableView中代表== = tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath){ key = catsArr [indexPath.section] let nameSection = vendsInCatDict [key]! cell.textLabel?.text = nameSection [indexPath.row] return cell } – user1481614

+0

@ Tom - 「您將需要跟蹤分組和排序的數據如何對應您的原始對象。」是啊...我想過這個。它只是越來越「厚」...我希望會有更簡單的東西,我錯過了... – user1481614

+0

我已經添加了一個建議,你可能會如何去存儲對象映射。可能需要一點調整,因爲我沒有完全測試它。 –