OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 swift 中使文本字段功能在 tableview 中 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:14
标题: ios - 如何在 swift 中使文本字段功能在 tableview 中

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

我能够创建一个带有文本字段的简单列表,但是当我按下文本字段时没有任何反应。键盘没有弹出,感觉好像事件正在被其他 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
}
}



Best Answer-推荐答案


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

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4