ios - 如何在 swift 中使文本字段功能在 tableview 中
<p><p>我正在尝试创建一个表格 View ,其中每个单元格都有多个文本字段。</p>
<p>我能够创建一个带有文本字段的简单列表,但是当我按下文本字段时没有任何反应。键盘没有弹出,感觉好像事件正在被其他 View 捕获。</p>
<p>我确保将用户交互设置为启用。
我尝试将 View 推到前面,就像您在评论中看到的那样。</p>
<p>我错过了什么?</p>
<p>我的表叫做 MainTable 并且
我的 InputviewCell 只是一个普通单元格 (xib),带有一个名为 txtFieldTemp 的文本字段</p>
<pre><code>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() ->
{
return objectsArray
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>尝试使用 <code>cell.txtFieldTemp.becomeFirstResponder()</code>。看着<a href="https://developer.apple.com/reference/uikit/uiresponder/1621113-becomefirstresponder" rel="noreferrer noopener nofollow">apple documentation</a>我认为这将迫使它成为第一响应者。</p></p>
<p style="font-size: 20px;">关于ios - 如何在 swift 中使文本字段功能在 tableview 中,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/42791263/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/42791263/
</a>
</p>
页:
[1]