gpt4 book ai didi

ios - 如何在不重叠 TableView 中的其他单元格的情况下重用 TableView 单元格 ios swift

转载 作者:行者123 更新时间:2023-11-28 14:26:58 25 4
gpt4 key购买 nike

当我向下或向上滚动我的表格 View 时,我得到的小部件/数据在每个单元格中重叠。我正在做的是,我有一个元素数组 wCheckinArray = ["Date", "TextFeild", "TextView", "Check", "Sign"]。现在我正在检查 wCheckinArray 中是否有元素 "anyElement" 然后我在一个单元格中创建一个 anyElement 并将其添加为 subview 单元格的内容 View 。

示例:

在 UITableView 的 cellForRowAtIndexPath 方法下:

  let w = wCheckinArray[indexPath.row]
if w.contains(Date) {
// make a date picker and add it as a subview to content view of cell
}
// and so on



真实代码:

var Cell : UITableViewCell?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cellforrowatindexpathruns")
var instockSignView : Int = 1
Cell = tempCheckinTable.dequeueReusableCell(withIdentifier: "tempCheckinCell")! as UITableViewCell

if wCheckinArray.count == 0 && lCheckinArray.isEmpty {
print("no textFields in Checkin")
}
else
{
print("widget process i in winstock loop")
let w = wCheckinArray[indexPath.row]

if w.contains(wTextField) {
print("textField was placed")
newLabel = UITextField(frame: CGRect(x: 10, y: 10, width: 250, height: 30))
newLabel.textColor = UIColor.gray
newLabel.tag = widgetTagOk
print("You have a instock textLabel with tag \(newLabel.tag)")

if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
newLabel.text = " "
} else {
newLabel.placeholder = lCheckinArray[indexPath.row]
newLabel.layer.cornerRadius = 3
newLabel.layer.borderColor = UIColor.gray.cgColor
newLabel.layer.borderWidth = 1

//instockTextField += 1
widgetTagOk += 1
print("\(wCheckinArray.count)")
Cell?.contentView.addSubview(newLabel)
}
}

if w.contains(wTextView) {
print("textView was placed")
newTextView = UITextView(frame: CGRect(x:10, y: 35, width: 250, height: 50))
newTextView.textAlignment = NSTextAlignment.justified
newTextView.textColor = UIColor.black
newTextView.backgroundColor = UIColor.white
newTextView.font = UIFont.systemFont(ofSize: 15)
newTextView.tag = widgetTagOk //instockTextView

print("You have a instock textView with tag \(newTextView.tag)")
if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
} else {
textViewLabel = UILabel(frame: CGRect(x:10, y: 10, width: 250, height: 20))
textViewLabel.text = "\(lCheckinArray[indexPath.row]):"
textViewLabel.textColor = .black
textViewLabel.tag = widgetTagOk
print("here is the TextView place text \(lCheckinArray[indexPath.row])")
newTextView.layer.cornerRadius = 3
newTextView.layer.borderColor = UIColor.gray.cgColor
newTextView.layer.borderWidth = 1
widgetTagOk += 1 //instockTextView += 1
print("\(wCheckinArray.count)")
Cell?.contentView.addSubview(newTextView)
Cell?.contentView.addSubview(textViewLabel)

}
}

if w.contains(wSign) { //wInstockArray.contains(wSign) {
print("sign was place d")
newSignView = UIView(frame: CGRect(x:10, y: 35, width: 250, height: 50))
newSignView.backgroundColor = UIColor.white
newSignView.tag = instockSignView
print("You have a instock signView with tag \(newSignView.tag)")
if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
print("signview can nit be added as a widget")
} else {
textViewLabel = UILabel(frame: CGRect(x:10, y: 10, width: 250, height: 20))
textViewLabel.text = "\(lCheckinArray[indexPath.row]):"
textViewLabel.textColor = .black
textViewLabel.tag = widgetTagOk
print("here is the TextView place text \(lCheckinArray[indexPath.row])")
newSignView.layer.cornerRadius = 3
newSignView.layer.borderColor = UIColor.gray.cgColor
newSignView.layer.borderWidth = 1
instockSignView += 1
print("\(wCheckinArray.count)")
print("signview has added as a instock widget")
Cell?.contentView.addSubview(newSignView)
Cell?.contentView.addSubview(textViewLabel)
}
}


if w.contains(wDate) { //wInstockArray.contains(wSign) {
print("date was placed")
newDatePicker = UIDatePicker(frame: CGRect(x:10, y: 35, width: 250, height: 50))
newDatePicker.backgroundColor = UIColor.white
newDatePicker.tag = instockSignView
print("You have a checkin date picker with tag \(newSignView.tag)")
if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
print("date picker can nit be added as a widget")
} else {
textViewLabel = UILabel(frame: CGRect(x:10, y: 10, width: 250, height: 20))
textViewLabel.text = "\(lCheckinArray[indexPath.row]):"
textViewLabel.textColor = .black
textViewLabel.tag = widgetTagOk
print("here is the date picker place text \(lCheckinArray[indexPath.row])")
newDatePicker.layer.cornerRadius = 3
newDatePicker.layer.borderColor = UIColor.gray.cgColor
newDatePicker.layer.borderWidth = 1
instockSignView += 1
print("\(wCheckinArray.count)")
print("date picker has added as a checkin widget")
Cell?.contentView.addSubview(newDatePicker)
Cell?.contentView.addSubview(textViewLabel)
}
}

// } //for i in instockarr comment bracket
print("could not add widgets")
print("here is w id \(idCheckinArray),here is w name: \(wCheckinArray), here is w data \(lCheckinArray)")
}
return Cell!
}


这是当我向上或向下滚动 tableView 时 tableView 的样子

enter image description here


我用 Google 搜索过的内容

我用谷歌搜索的是This question这向我建议我应该检查 clear graphics context 这在 Xcode 中默认完成

还搜索了 Here这建议我更改每个单元格的 cell 标识符,我该怎么做?如何更改我在 Xcode 的 Tableview 单元格的 attributes inspector 中提到的单元格标识符。是否有任何解决方案可以以编程方式重用它。

更新

当我从底部向上滚动表格 View 时,像 textview.tag 或 textfield.tag 这样的小部件的标签正在增加这里是我打印文本字段标签的方式

enter image description here

最佳答案

您最好删除创建 subview 的所有 subview 。并确保您对每个元素的y 位置不相同

Cell = tempCheckinTable.dequeueReusableCell(withIdentifier: "tempCheckinCell")! as UITableViewCell

for subView in cell.contentView.subviews{
subView .removeFromSuperview()
}

关于ios - 如何在不重叠 TableView 中的其他单元格的情况下重用 TableView 单元格 ios swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51491717/

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