- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我一直在尝试在我的应用程序中实现一项功能,以便当用户点击我的表格 View 中的单元格时,该单元格会向下扩展以显示注释。我在 Objective-C 中找到了很多这样的例子,但我还没有找到 Swift 的例子。
这个例子看起来很完美:Accordion table cell - How to dynamically expand/contract uitableviewcell?
我曾尝试将它翻译成 Swift:
var selectedRowIndex = NSIndexPath()
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedRowIndex = indexPath
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if selectedRowIndex == selectedRowIndex.row && indexPath.row == selectedRowIndex.row {
return 100
}
return 70
}
然而,这似乎只会让应用程序崩溃。
有什么想法吗?
编辑:
这是我的 cellForRowAtIndexPath 代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:CustomTransactionTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
if tableView == self.searchDisplayController?.searchResultsTableView {
cell.paymentNameLabel.text = (searchResults.objectAtIndex(indexPath.row)) as? String
//println(searchResults.objectAtIndex(indexPath.row))
var indexValue = names.indexOfObject(searchResults.objectAtIndex(indexPath.row))
cell.costLabel.text = (values.objectAtIndex(indexValue)) as? String
cell.dateLabel.text = (dates.objectAtIndex(indexValue)) as? String
if images.objectAtIndex(indexValue) as NSObject == 0 {
cell.paymentArrowImage.hidden = false
cell.creditArrowImage.hidden = true
} else if images.objectAtIndex(indexValue) as NSObject == 1 {
cell.creditArrowImage.hidden = false
cell.paymentArrowImage.hidden = true
}
} else {
cell.paymentNameLabel.text = (names.objectAtIndex(indexPath.row)) as? String
cell.costLabel.text = (values.objectAtIndex(indexPath.row)) as? String
cell.dateLabel.text = (dates.objectAtIndex(indexPath.row)) as? String
if images.objectAtIndex(indexPath.row) as NSObject == 0 {
cell.paymentArrowImage.hidden = false
cell.creditArrowImage.hidden = true
} else if images.objectAtIndex(indexPath.row) as NSObject == 1 {
cell.creditArrowImage.hidden = false
cell.paymentArrowImage.hidden = true
}
}
return cell
}
这里是 socket 设置:
最佳答案
我花了很多时间才让它工作。以下是我的解决方法。
PS:@rdelmar 的代码的问题在于他假定您的表中只有一个 section
,因此他只比较 indexPath.row
。如果您有多个部分(或者如果您已经考虑到稍后扩展代码),您应该比较整个索引,如下所示:
1) 您需要一个变量来告知选择了哪一行。我看到你已经这样做了,但你需要将变量返回到一致的“未选择”状态(当用户关闭所有单元格时)。我相信最好的方法是通过一个可选的:
var selectedIndexPath: NSIndexPath? = nil
2) 您需要确定用户何时选择了一个单元格。 didSelectRowAtIndexPath
是显而易见的选择。您需要考虑三种可能的结果:
对于每种情况,我们检查 selectedIndexPath 是否等于 nil(未展开单元格)、等于点击行的 indexPath(已展开相同的单元格)或不同于 indexPath(已展开另一个单元格)。我们相应地调整 selectedIndexPath。此变量将用于检查每行的正确 rowHeight。您在评论中提到 didSelectRowAtIndexPath
“似乎没有被调用”。您是否正在使用 println() 并检查控制台以查看它是否被调用?我在下面的代码中包含了一个。
PS:这在使用 tableView.rowHeight
时不起作用,因为显然,在更新 tableView 中的所有行之前,Swift 只检查一次 rowHeight。
最后但同样重要的是,我使用 reloadRowsAtIndexPath
仅重新加载所需的行。但是,同样,因为我知道它会重绘表格,必要时重新布局,甚至动画更改。请注意 [indexPath]
位于方括号之间,因为此方法需要一个 NSIndexPath 数组:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("didSelectRowAtIndexPath was called")
var cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCustomTableViewCell
switch selectedIndexPath {
case nil:
selectedIndexPath = indexPath
default:
if selectedIndexPath! == indexPath {
selectedIndexPath = nil
} else {
selectedIndexPath = indexPath
}
}
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
3) 第三步也是最后一步,Swift 需要知道何时将每个值传递给单元格高度。我们在这里使用 if/else 进行类似的检查。我知道您可以使代码更短,但我将所有内容都打出来,以便其他人也能轻松理解:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let smallHeight: CGFloat = 70.0
let expandedHeight: CGFloat = 100.0
let ip = indexPath
if selectedIndexPath != nil {
if ip == selectedIndexPath! {
return expandedHeight
} else {
return smallHeight
}
} else {
return smallHeight
}
}
现在,如果上述方法不能解决问题,请注意您的代码可能是问题的原因:
var cell:CustomTransactionTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
我不知道这是否是问题所在,但 self
应该不是必需的,因为您可能将这段代码放在您的 (Custom)TableViewController 中。此外,如果您正确地从出队中强制转换单元格,那么您可以信任 Swift 的推断,而不是指定您的变量类型。强制转换是下面代码中的 as!
:
var cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier" forIndexPath: indexPath) as! CustomTransactionTableViewCell
但是,您绝对需要设置该标识符。转到您的 Storyboard ,选择具有您需要的单元格的 tableView,为您需要的 TableViewCell 子类(在您的情况下可能是 CustomTransactionTableViewCell
)。现在选择 TableView 中的单元格(检查您是否选择了正确的元素。最好通过编辑器 > 显示文档大纲打开文档大纲)。选择单元格后,转到右侧的属性检查器并输入 Identifier
名称。
您也可以尝试注释掉 cell.selectionStyle = UITableViewCellSelectionStyle.None
以检查它是否以任何方式阻止选择(这样,如果单元格被选中,则在点击时会改变颜色)。
祝你好运,伙计。
关于ios - 在 Swift 中点击时展开单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26217480/
我需要你的帮助!我在它们之间放置了随机数量的 div。 Item description Item description Item description Item
我有两个 NSDates,时间格式为“h:mm a”(即 6:00 AM 和 8:00 PM)。 我试图找出这两个时间之间的中点是什么时间。 对于上面的示例,早上 6:00 和晚上 8:00 之间的中
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我正在寻找一种有效的算法来检查一个点是否在 3D 中的另一个点附近。 sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2) < radius 这似乎并不太快,实际上我不需要这
我可以让 pandas cut/qcut 函数返回 bin 端点或 bin 中点而不是一串 bin 标签吗? 目前 pd.cut(pd.Series(np.arange(11)), bins = 5)
我是一名优秀的程序员,十分优秀!