【问题标题】:Custom UITableViewCell not showing textfield自定义 UITableViewCell 不显示文本字段
【发布时间】:2016-09-26 03:26:02
【问题描述】:

我创建了一个自定义 UITableViewCell,左侧有一个标签,右侧有一个文本字段。但是,在桌子上,标签会显示出来,但文本字段不会。为什么会发生这种情况,我该如何解决?

class TableViewCell: UITableViewCell {
    var cellLabel: UILabel!
    var textField: UITextField!

    let tableViewController:TableViewController = TableViewController()

    init(frame: CGRect, title: String) {
    super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

        cellLabel = UILabel(frame: CGRectMake(0, 0, 100, 40))

        let cellTextField = UITextField(frame: CGRectMake(bounds.width*0.2, 100, bounds.width*0.75, 50))
        cellTextField.delegate = tableViewController

        self.addSubview(cellLabel)
        self.addSubview(cellTextField)
    }

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

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

}

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
    var tableView: UITableView  =   UITableView()
    let cellLabels : [String] = ["ID","Number of Times Infected"]

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        var cell = TableViewCell(frame: CGRectMake(0, 0, self.view.frame.width, 50), title: cellLabels[indexPath.row])
        cell.cellLabel!.text = cellLabels [indexPath.row]
        return cell;
    }
}

【问题讨论】:

  • 你单元格的高度是多少?您将 UITextField origin.Y 设置为 100,可能不可见
  • 为文本字段设置背景颜色并检查。
  • 检查您的界面生成器,可能是您没有“连接”您的自定义单元格
  • 您应该将视图添加到self.contentView,而不是self 本身。

标签: ios swift uitableview uitextfield


【解决方案1】:
//TableViewCell Class
class TableViewCell: UITableViewCell {

    var cellLabel: UILabel!
    var textField: UITextField!

    let tableViewController:ViewController = ViewController()

    init(frame: CGRect, title: String) {
        super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

        cellLabel = UILabel(frame: CGRectMake(0, 0, bounds.width/2, 40))

        let cellTextField = UITextField(frame: CGRectMake(cellLabel.frame.size.width+5, 0, bounds.width*0.75, 50))

        cellTextField.placeholder = "Enter Your Text here"
        cellTextField.delegate = tableViewController

        self.addSubview(cellLabel)
        self.addSubview(cellTextField)
    }

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

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
//View Controller Class
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate
{
    var tableView: UITableView  =   UITableView()
    let cellLabels : [String] = ["ID","Number of Times Infected"]

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return cellLabels.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = TableViewCell(frame: CGRectMake(0, 0, self.view.frame.width, 50), title: cellLabels[indexPath.row])
        cell.cellLabel!.text = cellLabels [indexPath.row]
        return cell;
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


}

注意:根据您的要求设置框架。

输出:

【讨论】:

  • 谢谢!我使用了您为 cellLabel 和 cellTextField 的框架设置的尺寸,它起作用了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-22
  • 2017-05-22
  • 2014-09-05
相关资源
最近更新 更多