ios - 将文本字段添加到 UITableview 单元格

标签 ios uitableview swift2

我是使用 swift 2.2 进行 Ios 编程的新手

我想要实现的目标是在 uitableviewcell 或 uitableviewcells 中插入两个文本字段

例如单元格中的文本将采用这种格式

“A vs B”

我想在 VS 两侧插入文本字段 所以我希望它看起来像这样“A[Textfield] vs [Textfield]B”

任何形式的帮助将不胜感激

最佳答案

  1. 创建具有两个文本字段导出的 UITableViewCell 子类:

    class TextFieldsCell: UITableViewCell {
        @IBOutlet var textfield1: UITextField!
        @IBOutlet var textfield2: UITextField!
    }
    
  2. 在 IB 中,选择表格 View ,然后在属性检查器中将“原型(prototype)单元格”设置为“1”,将原型(prototype)单元格添加到表格 View 中:

enter image description here

  • 在 IB 中,使用自动布局在单元格内布局文本字段。 (跳过自动布局实现的细节)
  • enter image description here

  • 在 IB 中,选择表格 View 单元格,然后在属性检查器中将其“标识符”更改为“TextFieldsCell”:
  • enter image description here

  • 在 IB 中,选择表格 View 单元格,然后在身份检查器中将其“Class”更改为“TextFieldsCell”:
  • enter image description here

  • 在 IB 中,将 TextFieldsCell 的 IBOutlet 与相应的文本字段连接:
  • enter image description here

  • 在包含 TableView 的 View Controller 中,实现以下内容:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: TextFieldsCell = tableView.dequeueReusableCellWithIdentifier("TextFieldsCell") as! TextFieldsCell
        return cell
    }
    
  • 如果您正确设置了所有内容,您应该在下次运行时看到以下内容:

  • enter image description here

    关于ios - 将文本字段添加到 UITableview 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51590653/

    相关文章:

    ios - UITableView 单元格中的 UITextField 与 shouldChangeCharactersInRange 行为异常

    swift - 在 ViewController 和 ContainerViewController 之间传递数据

    ios - 基于另一个 UIPickerView 显示 UIPickerView

    ios - 从 Facebook 读取信息

    ios - 将 UITableViewDataSource 与具有 subview 的自定义单元格一起使用

    ios - 如何让单元格中的内容 View 占据整个单元格?

    ios - 删除时多个原型(prototype)单元格标题会移动

    swift - 类型 'OSType' 不符合 Swift 2.0 中的协议(protocol) 'AnyObject'

    iphone - Facebook 整合问题

    ios - 将 UIImages 数组缩放到指定的百分比会导致 UI 在几秒钟内没有响应?