如何在swift上的tableview中使textfield功能正常

时间:2017-03-14 16:20:40

标签: ios swift uitableview uitextfield uitextfielddelegate

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

我能够创建一个带有文本字段的简单列表但是当我按下文本字段时没有任何反应。键盘没有弹出,感觉就像某个其他视图正在捕获事件。

我确保将用户交互设置为启用。 我试图将视图推到前面,就像你在评论中看到的一样。

我错过了什么?

我的表名为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
}
}

1 个答案:

答案 0 :(得分:0)

尝试使用$table = ''; $table .= '<table>'; foreach($response['follows'] as $entry) { $table .= '<tr>'; $table .= '<td>'.$entry['created_at'].'</td>'; $table .= '<td>'.$entry['user']['display_name'].'</td>'; $table .= '<td><a class="your-custom-class" href="'.$entry['_links']['self'].'">View channel</a></td>'; $table .= '</tr>'; } $table .= '</table>'; echo $table; 。看apple documentation我认为它会迫使它成为第一个响应者。