【发布时间】:2018-10-10 14:20:48
【问题描述】:
首先我是如何使用 UIPicker 创建可扩展单元格的。以防万一这与问题有关。
它是由这段代码在 UITableView 中创建的
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var identifyer = ""
switch(indexPath.row){
//other cells
case 1:
//Kategoria
identifyer = "category"
categoryCell = tableView.dequeueReusableCellWithIdentifier(identifyer) as? CategoryTableViewCell
categoryCell.pickerView.delegate = self
categoryCell.pickerView.dataSource = self
return categoryCell
//other cells
}
那我要识别它是否被触摸
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.row == 1){
isCategoryCellSelected = true
tableView.reloadRowsAtIndexPaths([categoryIndexPath()], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
这就是我替换UILabel中文本的方式
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
categoryCell.categoryNameLabel.text = categories[row]
}
最后,当 tableView 刷新单元格时
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
switch(indexPath.row){
case 1:
if (isCategoryCellSelected){
return CategoryTableViewCell.expandedHeight
} else {
return CategoryTableViewCell.defaultHeight
}
//other heights
}
}
默认单元格外观
展开的单元格外观
问题
因此,当我在选择器中选择项目时,上面的标签应该将其更改为文本,并且正在发生这种情况。但是,当此单元格缩小到默认高度时,替换效果就消失了。我必须向下和向上滚动才能看到它,然后我才能看到标签中更改的文本。
我假设 UITableView 缓存了这个单元格,当单元格重新加载时,它会采用这个缓存的版本。我只是猜测。是我的想法吗?我怎样才能改变这个不需要的动作?
解决方案
正如@the_critic 指出的那样,我在 vars 中保存单元格的方法是完全错误的。在同一时间,每次我选择行时重新创建 categoryCell 并不是正确的方法。我最终采用了他创建单元格的方式,但采用了我为单元格设置值的方式。
所以它看起来像这样:
创建单元格
categoryIdentifier 是private let String
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell
switch(indexPath.row){
// other cells
case 1:
//Kategoria
print("recreate, selected category \(selectedCategory)")
let categoryCell = tableView.dequeueReusableCellWithIdentifier(categoryIdentifier) as! CategoryTableViewCell
categoryCell.pickerView.delegate = self
categoryCell.pickerView.dataSource = self
updateSelectedCategoryIfNeeded(categoryCell)
cell = categoryCell
// other cells
return cell;
}
private func updateSelectedCategoryIfNeeded(cell:CategoryTableViewCell) {
if let selectedCategory = self.selectedCategory{
// A category has been selected!
cell.categoryNameLabel.text = selectedCategory
updatePicekerRowPosition(cell)
}else{
// no category selected!
cell.categoryNameLabel.text = "Wybierz kategorie..."
}
}
private func updatePicekerRowPosition(cell:CategoryTableViewCell) {
if let index = categories.indexOf(selectedCategory!){
cell.pickerView.selectRow(Int(index.value), inComponent: 0, animated: false)
}
}
识别行选择
categoryIndexPath 是private let NSIndexPath
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedCategory = categories[row]
if let categoryCell = tableView.cellForRowAtIndexPath(categoryIndexPath) as? CategoryTableViewCell {
categoryCell.categoryNameLabel.text = selectedCategory
}
}
【问题讨论】:
-
因此,您希望从选择器中选择“Wybierz kategorie”而不是“Wybierz kategorie”?顺便说一句,更改标签的代码在哪里?我猜它在选择器委托方法中,你也可以发布吗?
-
@the_critic 是的,我已经用
pickerView(pickerView:didSelectRow:inComponent:)完成了它,但是收缩后效果消失了,并且当此单元格展开时显示正确的视图。这就像两个分离的视图,它们在UITableView重建后同步。也许视频可以更好地解释它。 vid.me/yLSQ 。我还用UIPicker委托方法更新了我的问题。 :)
标签: ios swift uitableview