gpt4 book ai didi

ios - 如何在 UITableViewCell 中创建 TextField?

转载 作者:行者123 更新时间:2023-11-30 11:13:28 26 4
gpt4 key购买 nike

我试图仅通过使用代码在UITableViewCell内创建一个UITextField,但目前我创建“cell.mainTextField.delegate = self”,在“cellForRowAt”内,文本字段停止允许发短信,就像它被禁用一样。此外,函数 shouldChangeCharactersIn() 未运行。

关于我做错了什么有什么想法吗?谢谢!

var publishing_TableView: UITableView?

类发布:

class Publishing: UIViewController {  

override func viewDidLoad() {

super.viewDidLoad()

publishing_TableView = {

let table = UITableView(frame: CGRect(x: 0, y: self.navigationController!.navigationBar.bounds.height, width: view.frame.width, height: view.frame.height), style: .grouped)

table.backgroundColor = UIColor(white: 1, alpha: 1)
table.delegate = self
table.dataSource = self
table.register(Publishing_MainTextField.self, forCellReuseIdentifier: "cell")
table.separatorStyle = .none
table.isScrollEnabled = true

return table

}

view.addSubview(publishing_TableView!)

}

}

表格 View 函数:

extension Publishing: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let viewForHeader: UIView = UIView.init(frame: CGRect(x:0, y:0, width: view.frame.width, height: 0));

viewForHeader.backgroundColor = .white

return viewForHeader

}


func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

return 0

}


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

switch indexPath.row {

case 0: return 60

default: return 0

}
}

func numberOfSections(in tableView: UITableView) -> Int {

return 1

}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

print("selected line: ", indexPath.row)

tableView.deselectRow(at: indexPath, animated: false)

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 1

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

switch indexPath.row {

case 0:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Publishing_MainTextField

cell.mainTextField.delegate = self

cell.selectionStyle = .none

return cell

default: return UITableViewCell()

}
}
}

表格 View 单元格功能:

class StartPublishing_MainTextField: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

setupViews()

}

required init? (coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

func setupViews() {

addSubview(mainTextField)

}

var mainTextField: UITextField = {

var myTextField = UITextField(frame: CGRect(x: 5, y: 5, width: UIScreen.main.bounds.width-2*20, height: 50))

myTextField.placeholder = "Example"

return myTextField
}()
}

TextField 委托(delegate)函数:

extension Publishing: UITextFieldDelegate {

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

var textFieldText: NSString = (textField.text ?? "") as NSString

var textAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)

print("The updated text is: ", textAfterUpdate)

return textAfterUpdate.characters.count <= 30

}
}

最佳答案

import UIKit

class Publishing: UIViewController {

var publishing_TableView : UITableView {
get {
let table = UITableView(frame: CGRect(x: 0, y: self.navigationController!.navigationBar.bounds.height, width: view.frame.width, height: view.frame.height), style: .grouped)

table.backgroundColor = UIColor(white: 1, alpha: 1)
table.delegate = self
table.dataSource = self
table.register(StartPublishing_MainTextField.self, forCellReuseIdentifier: "cell")
table.separatorStyle = .none
table.isScrollEnabled = true
return table
}
}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
self.view.addSubview(publishing_TableView)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}
extension Publishing: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let viewForHeader: UIView = UIView.init(frame: CGRect(x:0, y:0, width: view.frame.width, height: 0));
viewForHeader.backgroundColor = .white
return viewForHeader
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.row {
case 0: return 60
default: return 0
}
}

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected line: ", indexPath.row)
tableView.deselectRow(at: indexPath, animated: false)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! StartPublishing_MainTextField
cell.mainTextField.delegate = self
cell.selectionStyle = .none
return cell
default: return UITableViewCell()
}
}
}
class StartPublishing_MainTextField: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}

required init? (coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func setupViews() {
addSubview(mainTextField)
}

var mainTextField: UITextField = {
var myTextField = UITextField(frame: CGRect(x: 5, y: 5, width: UIScreen.main.bounds.width-10, height: 50))
myTextField.backgroundColor = #colorLiteral(red: 0.2181161642, green: 0.6374218464, blue: 1, alpha: 1)
myTextField.layer.cornerRadius = 4.0
myTextField.layer.borderWidth = 1.0
myTextField.layer.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
myTextField.placeholder = "Type here"
return myTextField
}()
}
extension Publishing: UITextFieldDelegate {

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var textFieldText: NSString = (textField.text ?? "") as NSString
var textAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)
print("The updated text is: ", textAfterUpdate)
return textAfterUpdate.characters.count <= 30
}
}

output

关于ios - 如何在 UITableViewCell 中创建 TextField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51958890/

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