【发布时间】:2018-09-24 05:29:21
【问题描述】:
如何根据标签存储文本。
我在UITableViewCell 中使用了UITextField。
我的视图控制器:-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "Cell"
var cell: NH_QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell
if cell == nil {
tableView.register(UINib(nibName: "NH_QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell
}
cell.contentView.backgroundColor = UIColor.clear
cell.textadd = {[weak self] in
if let i = self?.tableview.indexPath(for: $0) {
print("the selected text\(cell.textname ?? "")")
print(i)
print("the Selected button is \(cell.textname ?? "")")
print(i)
cell.setText(Options:(self?.questionViewModel.datafordisplay(atindex: indexPath))!)
let model = self?.questionViewModel.datafordisplay(atindex: indexPath)
print(model?.values ?? "")
if model?.isSelected == true{
cell.texttype.tag = indexPath.row
// cell.texttype.text = questionViewModel.datafordisplay(atindex: indexPath)
}
return cell
}
tableviewcell:-
@IBOutlet weak var texttype: UITextField!
var textname:String?
var textadd : ((NH_QuestionListCell) -> Void)?
@IBAction func TextAction(_ sender: UITextField) {
print(texttype.text)
// self.question.text = texttype.text
let index = texttype.tag
self.textname = texttype.text
self.textname = texttype.text
print(self.textname)
print(texttype.text)
textadd?(self)
}
func setText(Options:NH_OptionsModel)
{
self.displaySmiley.isHidden = true
self.viewdisplay.isHidden = false
self.imagebutton.isHidden = true
self.question.isHidden = true
self.displayRatingView.isHidden = true
print(Options.values)
Options.values = self.textname
print(Options.values)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
texttype.text = ""
}
func textFieldShouldClear(_ textField: UITextField) -> Bool
{
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.TextAction(texttype)
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true;
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return true;
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print(texttype.text)
texttype.resignFirstResponder()
return true
}
我得到了输出。在第一个单元格中,我在文本字段中输入了一些文本,然后点击“输入”。然后我滚动。当时我发现我输入的文本在其他单元格中可用。如何解决这个问题?
【问题讨论】:
-
如何解决问题?
-
不清楚你在问什么,代码很杂乱无章。
-
@Rigo 我以这种方式编码,所以现在我的问题是首先我在文本字段中输入一个文本,然后在下一个单元格中我需要输入文本但这里显示第一个文本第二个单元格中的单元格。因此,在滚动时,每个单元格中都会显示相同的文本。那么如何修复它?
-
@Rigo 如何解决问题?
标签: ios uitableview