- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道是否有办法使用不同的“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
,您可以使用前两行检索 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 Swift - prepareForSegue - indexPathForSelectedRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32121008/
我有一个由 cellForRowAtIndexPath 填充的 UITableView: func tableView(tableView: UITableView, cellForRowAtInde
我有一个带有 segue 的 UITableViewController,我试图在其中获取当前选定的行: -(void)prepareForSegue:(UIStoryboardSegue *)seg
我正在为学校构建一个应用程序。该应用程序有点像字典应用程序,其中包括嵌入导航 Controller 中的词汇表(我的 WordsTableViewController),以及显示词汇及其相应定义的定义
我正在使用 UITableViewCell 开发 iOS 应用程序。当点击 UITableViewCell 并使用 segue 转到另一个屏幕时,indexPathForSelectedRow 始终为
我想知道是否有办法使用不同的“indexPath”值作为 segue 中使用的键... 原因是,我有未分组和排序的 JSON 提要数据: { "results": [ { "BusCat01":
我有一个名为 Objects 的数组,它是 NSManagedObject 的子类,并在 UITableView 中显示它们的属性。 我有一个 segue 来查看对象的详细信息,这是使用手势执行的 -
我正在尝试调整 segue 中弹出 View 的大小。弹出窗口有效,但位置错误。 为了设置位置,我使用以下代码尝试选择当前的 IndexPathForSelectedRow,然后设置弹出值。 它没有触
在我的 prepareForSegue 方法中,我尝试使用点击的 TableView 单元格来为我的目标 View Controller 设置数据,但应用程序崩溃并指出 fatal error: un
我有一个 UITableView,我加载了一些应用程序设置。我需要单选表格,并且由于表格保存设置,因此可能有机会根据设置启用状态以编程方式选择某些单元格。 目前,我遇到了一种奇怪的行为,如果我以编程方
基本上,当我单击单元格上的附件按钮时,如果我尝试通过执行 indexPathForSelectedRow 来访问 indexPath,则它无法正常工作并返回 nil。所以我研究并发现我需要在 prep
很抱歉,我是一个完全的菜鸟,但我在使用 TableView 的 didSelectRowAtIndexPath 函数中的 indexPathForSelectedRow() 时遇到了问题。 我正在尝试
我正在尝试将 TableView Controller 上的代码更改为 Collection View Controller ,现在我正在尝试让 Segue 工作,但出现此错误: No visible
我正在尝试用我的 MySQL 数据库中的一些产品(使用 PHP POST 文件)填充 tableView,目前一切正常,但是当我选择“Cell”时,prepareForSegue 被触发,但是 ind
我需要获取 UITableView 的索引与 UILongPressGestureRecognizer在 iOS 中。 首先我添加了UILongPressGestureRecognizer并使用以下代
我有一个带有表的 subview ,并且我正在很好地填充数据。我有来自 super View 的“prepare(for segue:”),可以很好地识别单元格的选择。但是,从 super View
我有一个 UITableViewController 和一个普通的 ViewController。在 VC(名为 nyTankning)中,用户将输入保存到核心数据数据库的信息。数据库已加载并显示在
我想替换被自定义单元格触及的单元格。我通过调用 reloadRowsAtIndexPaths 来完成此操作 - (void)tableView:(UITableView *)tableView did
我想根据在我的 tableView 中选择的行,将信息从一个 viewController 的数组传递到另一个。为此,我实现了以下功能: func tableView(_ messagesListTa
我正在尝试从 UITableView 转到 UITabBarController 中的特定选项卡。在谷歌搜索时,我发现了两组信息,它们似乎表明了如何做,但都没有使用 UITableView 作为来源。
我在 UIViewController 中有一个 tableview,在 Main.storyboard 上,我有一个 show从第一个 View Controller 中tableview 上的原型
我是一名优秀的程序员,十分优秀!