iOS Swift - prepareForSegue - indexPathForSelectedRow

时间:2015-08-20 14:29:25

标签: ios swift tableview

我想知道是否有办法使用不同的“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中显示Grouped和Sorted数据时,序数(行键)与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}

参见“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)
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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。给定这些值,您将需要一个函数,该函数可以根据其键/ 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]]