首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在UITableViewCell内编辑UITextField失败

在 UITableViewCell 内编辑 UITextField 失败的问题通常是由于 UITableView 的重用机制引起的。当你滚动 UITableView 时,为了提高性能和减少内存占用,UITableView 会将不再显示的单元格进行重用。这就导致了 UITextField 的状态被错误地更新或者重置。

为了解决这个问题,你可以采取以下步骤:

  1. 为 UITextField 添加一个代理(UITextFieldDelegate),并实现代理方法。
  2. 在 UITableViewCell 的重用队列中注册 UITextField 的通知。
  3. 在 UITableViewCell 的重用队列中移除 UITextField 的通知。
  4. 在 UITableView 的代理方法中设置 UITextField 的代理为当前 UITableViewCell。

以下是一个简单的示例代码:

代码语言:swift
复制
class CustomTableViewCell: UITableViewCell, UITextFieldDelegate {
    @IBOutlet weak var textField: UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()
        textField.delegate = self
        NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange(_:)), name: UITextField.textDidChangeNotification, object: textField)
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        NotificationCenter.default.removeObserver(self, name: UITextField.textDidChangeNotification, object: textField)
    }

    @objc func textFieldDidChange(_ notification: Notification) {
        guard let textField = notification.object as? UITextField else { return }
        // 处理文本更改事件
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // 处理键盘的回车键事件
        return true
    }
}

在 UITableView 的代理方法中设置 UITextField 的代理为当前 UITableViewCell:

代码语言:swift
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
    cell.textField.tag = indexPath.row
    cell.textField.delegate = cell
    return cell
}

这样,你就可以在 UITableViewCell 内编辑 UITextField 而不会遇到失败的问题。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

史上最全的iOS之访问自定义cell的textField.text的N种方法

问题背景:自定义cell中有一个UITextField类型的子控件。我们经常要在tableView中拿到某个cell内textField的文本内容进行一些操作。比如某些app的注册界面就是以tableView的形式存在的,注册时往往需要注册姓名、昵称、邮箱、地址、联系方式等信息。然后点击注册或者提交,这些信息就会被提交到远程服务器。有人说,注册页面就那么固定的几行cell,没必要搞得那么复杂,完全可以用静态cell实现。但还有一些情况,当前页面的tableView的cell的行数是不确定的(比如当前页面显示多好行cell由上一个页面决定或者由用户决定),这种情况下不太适合使用静态cell。也不能够通过分支语句的方式一一枚举出各个case。所以需要一中通用的动态的方法。那么我们怎么在tableView中准确的拿到每一行cell中textField的text呢?以下我将要分四个方法分别介绍并逐一介绍他们的优缺点,大家可以在开发中根据实际情况有选择的采用不同的方法。 如下图,就是我之前开发的一个app中用xib描述的一个cell,当用户点击“注册”或者“提交”button时候,我需要在控制器中拿到诸如“法人姓名”这一类的信息:

04

IOS UITableView UITableViewCell控件

import UIKit class ViewController:UIViewController,UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. let screenRect = UIScreen.main.bounds let tableRect = CGRect(x:0, y:20, width: screenRect.size.width, height:screenRect.size.height - 20) let tableView = UITableView(frame:tableRect) tableView.dataSource = self self.view.addSubview(tableView) } func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) -> Int{ return 20 } func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) -> UITableViewCell { let identifier = “reusedCell” var cell =tableView.dequeueReusableCell(withIdentifier:identifier) if(cell == nil) { cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier:identifier) } cell?.textLabel?.text = “命运负责洗牌,玩牌的是我们自己!” return cell! } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

03

RxSwift介绍(一)——RxSwift初探

之前介绍了RAC在Objective-C环境下RACSignal信号订阅使用流程、宏定义以及各种信号的操作使用。作为函数式响应编程的代表,就不得不提RxSwift。 在swift环境下,RAC的孪生兄弟RxSwift同样提供了相同的框架使用,并且基于swift语言的优点,RxSwift甚至能够更简洁地开发业务代码。关于RxSwift的优点,大把大把的人在夸。我自己的感受是,虽然学习曲线比较陡峭,学习成本很高,一旦掌握了其开发技巧,收获要比想象中多,值得去学习并实践的框架。 接下来先看一个最常用的例子,swift环境中搭建一个简单的tableView。这里往往需要遵循TableView相关的各种代理方法,下面是使用结构体生成一串简单的数组并放入tableView中显示内容。

04
领券