gpt4 book ai didi

ios - 如何在 swift 中使文本字段在 tableview 中起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:13:23 26 4
gpt4 key购买 nike

我正在尝试创建一个 TableView ,其中每个单元格都有多个文本字段。

我能够创建一个带有文本字段的简单列表,但是当我按下文本字段时没有任何反应。键盘没有弹出,感觉就像事件正在被其他 View 捕获。

我确保用户交互设置为启用。我已尝试将 View 推到前面,就像您在评论中看到的那样。

我错过了什么?

我的表称为 MainTable 并且我的 InputviewCell 只是一个普通单元格 (xib),带有一个名为 txtFieldTemp 的文本字段

import Foundation
import UIKit

class PrioritiesGridViewController: UIViewController,UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate
{

@IBOutlet var mainTable: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

mainTable.delegate = self
mainTable.dataSource = self
//mainTable.allowsSelection = false
}

let data = AuxClass()

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 62
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.everything().count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("InputTextViewCell", owner: self, options: nil)?.first as! InputTextViewCell

cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.txtFieldTemp.delegate = self
cell.txtFieldTemp.text = "test"

cell.txtFieldTemp.allowsEditingTextAttributes = true
//self.view.bringSubview(toFront: cell.txtFieldTemp)

return cell;
}

override var canBecomeFirstResponder: Bool { return true}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}


func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}

func textFieldDidBeginEditing(_ textField: UITextField) {
self.isEditing = true

}


}

class AuxClass
{
var objectsArray = [Objects(
field1: "teste",
field2: 3),
Objects(
field1: "teste1",
field2: 4)
]

struct Objects {
var field1: String!
var field2: Int
}


func everything() -> [Objects]
{
return objectsArray
}
}

最佳答案

尝试使用 cell.txtFieldTemp.becomeFirstResponder()。看着 apple documentation我认为这将迫使它成为第一响应者。

关于ios - 如何在 swift 中使文本字段在 tableview 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42791263/

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