gpt4 book ai didi

swift - Swift 表中的可扩展单元格 - 单元格重用/出队?

转载 作者:行者123 更新时间:2023-11-30 12:20:16 24 4
gpt4 key购买 nike

我正在构建一个自定义界面,供用户在我的应用程序中输入首选项设置。我正在使用 example I found at AppCoda 后面的可扩展行。我重新设计了该示例,以使用 Swift 3/4 并使用代码中的单元格信息,而不是从 plist 中读取。

我遇到了某些单元格内容在屏幕上显示方式的问题。展开和折叠的行包含允许用户输入的文本字段。下面的示例代码中有四行这样的行。

当在这些单元格之一中进行输入时,它可能会或可能不会导致最后输入的值在展开时出现在所有四个单元格中。 “额外”文本甚至会覆盖属于那里的信息。

我已经尝试了一切我能想到的方法来摆脱这个令人讨厌的文字,但我正在用头撞墙。我错过了什么?

FWIW,我现在正在其他地方寻找类似的解决方案。这是我非常喜欢的一个:

https://github.com/jeantimex/ios-swift-collapsible-table-section-in-grouped-section

这个看起来很有趣,但不是用 Swift 编写的:

https://github.com/singhson/Expandable-Collapsable-TableView

相同评论:

https://github.com/OliverLetterer/SLExpandableTableView

这看起来很有趣 - 得到了很好的支持 - 但我还没有时间去调查:

https://github.com/Augustyniak/RATreeView

这里有一个类似的请求:

Expand cell when tapped in Swift

这里描述了类似的问题,但我想我已经按照建议的做了?

http://www.thomashanning.com/the-most-common-mistake-in-using-uitableview/

这是我的 TableView Controller 代码。我相信问题出在...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath):

...功能,但我一生都看不到它。

    override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
test = defineCellProps() // This loads my hard-coded cell properties into array "test"
configureTableView()
}

func configureTableView() {
loadCellDescriptors()
tblExpandable.delegate = self
tblExpandable.dataSource = self
tblExpandable.tableFooterView = UIView(frame: CGRect.zero)
tblExpandable.register(UINib(nibName: "NormalCell", bundle: nil), forCellReuseIdentifier: "idCellNormal")
tblExpandable.register(UINib(nibName: "TextfieldCell", bundle: nil), forCellReuseIdentifier: "idCellTextfield") // There are additional cell types that are not shown and not related to the problem
}

func loadCellDescriptors() { // Puts the data from the "test" array into the format used in the original example
for section in 0..<ACsections.count {
var sectionProps = findDict(matchSection: ACsections[section], dictArray: test)
cellDescriptors.append(sectionProps)
}
cellDescriptors.remove(at: 0) // Removes the empty row
getIndicesOfVisibleRows()
tblExpandable.reloadData() // The table
}

func getIndicesOfVisibleRows() {
visibleRowsPerSection.removeAll()
for currentSectionCells in cellDescriptors { // cellDescriptors is an array of sections, each containing an array of cell dictionaries
var visibleRows = [Int]()
let rowCount = (currentSectionCells as AnyObject).count as! Int
for row in 0..<rowCount { // Each row is a table section, and array of cell dictionaries
var testDict = currentSectionCells[row]
if testDict["isVisible"] as! Bool == true {
visibleRows.append(row)
} // Close the IF
} // Close row loop
visibleRowsPerSection.append(visibleRows)
} // Close section loop
} // end the func


func getCellDescriptorForIndexPath(_ indexPath: IndexPath) -> [String: AnyObject] {
let indexOfVisibleRow = visibleRowsPerSection[indexPath.section][indexPath.row]
let cellDescriptor = (cellDescriptors[indexPath.section])[indexOfVisibleRow]
return cellDescriptor as [String : AnyObject]
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let currentCellDescriptor = getCellDescriptorForIndexPath(indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: currentCellDescriptor["cellIdentifier"] as! String, for: indexPath) as! CustomCell
cell.textLabel?.text = nil
cell.detailTextLabel?.text = nil
cell.textField?.placeholder = nil
if currentCellDescriptor["cellIdentifier"] as! String == "idCellNormal" {
if let primaryTitle = currentCellDescriptor["primaryTitle"] {
cell.textLabel?.text = primaryTitle as? String
}

if let secondaryTitle = currentCellDescriptor["secondaryTitle"] {
cell.detailTextLabel?.text = secondaryTitle as? String
}
}
else if currentCellDescriptor["cellIdentifier"] as! String == "idCellTextfield" {
if let primaryTitle = currentCellDescriptor["primaryTitle"] {
if primaryTitle as! String == "" {
cell.textField.placeholder = currentCellDescriptor["secondaryTitle"] as? String
cell.textLabel?.text = nil

} else {
cell.textField.placeholder = nil
cell.textLabel?.text = primaryTitle as? String
}
}

if let secondaryTitle = currentCellDescriptor["secondaryTitle"] {
cell.detailTextLabel?.text = "some text"
}
cell.detailTextLabel?.text = "some text"
// This next line, when enabled, always puts the correct row number into each cell.
// cell.textLabel?.text = "cell number \(indexPath.row)."
}

cell.delegate = self

return cell
}

这是我几乎没有更改的 CustomCell 代码:

import UIKit

protocol CustomCellDelegate {
func textfieldTextWasChanged(_ newText: String, parentCell: CustomCell)
}

class CustomCell: UITableViewCell, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!

let bigFont = UIFont(name: "Avenir-Book", size: 17.0)
let smallFont = UIFont(name: "Avenir-Light", size: 17.0)
let primaryColor = UIColor.black
let secondaryColor = UIColor.lightGray

var delegate: CustomCellDelegate!

override func awakeFromNib() {
super.awakeFromNib() // Initialization code

if textLabel != nil {
textLabel?.font = bigFont
textLabel?.textColor = primaryColor
}

if detailTextLabel != nil {
detailTextLabel?.font = smallFont
detailTextLabel?.textColor = secondaryColor
}

if textField != nil {
textField.font = bigFont
textField.delegate = self
}
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}

override func prepareForReuse() { // I added this and it did not help
super.prepareForReuse()
textLabel?.text = nil
detailTextLabel?.text = nil
textField?.placeholder = nil
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if delegate != nil {
delegate.textfieldTextWasChanged(textField.text!, parentCell: self)
}
return true
}
}

最佳答案

天哪,我正在用手掌拍打额头。上面的这段代码缺少一行非常重要的内容:

override func prepareForReuse() {
super.prepareForReuse()
textLabel?.text = nil
detailTextLabel?.text = nil
textField?.placeholder = nil
}

你能看出缺少了什么吗?

textField?.text = nil

这就是全部!我正在研究标签,但没有研究文本字段文本本身。

关于swift - Swift 表中的可扩展单元格 - 单元格重用/出队?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44873284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com