[英]How to make textfield functional inside a tableview on swift
我正在嘗試創建一個tableview,其中每個單元格都有多個文本字段。
我能夠創建一個帶有文本字段的簡單列表,但是當我按下文本字段時,什么也沒有發生。 鍵盤沒有彈出,並且感覺該事件正在被其他視圖捕獲。
我確保將用戶交互設置為啟用。 我試圖將視圖推到最前面,就像您在評論中看到的那樣。
我想念什么?
我的表稱為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()
。 查看蘋果文檔,我認為它將迫使它成為第一響應者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.