我想知道是否有一种方法可以使用不同的“ 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,您可以使用前两行检索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]]

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

10-14 22:27