动态添加自定义UITableViewCell

时间:2016-12-16 01:57:38

标签: ios swift uitableview

我想用自定义视图(UITableViewCell)创建一个列表(UITableView),该列表包含三个带有用户数据的标签,当你点击添加按钮(列表外)时,可以说是名字和姓氏,部门,新的用户自定义视图将添加到列表中,您可以在其中输入另一个用户详细信息。 我知道如何在tableView中创建customView 我知道如果它只是一个文本,如何附加到tableView,但我想要附加到tableview的是单击按钮时的customView。

将一个文本添加到tableView,我做了类似下面的事情。

datas.insert("new data", at: 0)
        jobTableView.beginUpdates()
        jobTableView.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .automatic)
        jobTableView.endUpdates()

我如何为customView执行此操作?

以下是我的tableview

UITableviewClass

public class MiddlePartCell: BaseCell, UITableViewDelegate, UITableViewDataSource {

let tableCell = "tableCell"

lazy var jobTableView: UITableView = {
   let tv = UITableView()
    tv.backgroundColor = .brown
    tv.dataSource = self
    tv.delegate = self
    return tv
}()

lazy var addMoreButton: UIButton = {
    let button = UIButton(type: .system)
    button.setBackgroundImage(UIImage(named: "plus"), for: .normal)
    button.tintColor = .black
    button.addTarget(self, action: #selector(handleAddJob), for: .touchUpInside)
    button.layer.cornerRadius = 20
    return button
}()

@objc private func handleAddJob(){
    //what should i do here 
    //i need to insert the custom view to the top index
    jobTableView.beginUpdates()
    jobTableView.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .automatic)
    jobTableView.endUpdates()
}

override func setupView() {
    super.setupView()
    backgroundColor = .green
    addSubview(jobTableView)
    addSubview(addMoreButton)

    jobTableView.register(UITableViewCell.self, forCellReuseIdentifier: tableCell)

    _ = addMoreButton.anchor(nil, left: leftAnchor, bottom: bottomAnchor, right: nil, topConstant: 0, leftConstant: 10, bottomConstant: 10, rightConstant: 0, widthConstant: 50, heightConstant: 50)

    _ = jobTableView.anchor(topAnchor, left: leftAnchor, bottom: addMoreButton.topAnchor, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: frame.width, heightConstant: frame.height)
}

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

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: tableCell, for: indexPath)

    cell.textLabel?.text = datas[indexPath.item]

    return cell
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return datas.count
  }
}

1 个答案:

答案 0 :(得分:1)

首先,您必须使用UITextField为UITableViewCell创建UITableViewCell的子类。当然,您可以在cellForRowAt委托函数中添加UITextField,但我更喜欢使用自定义UITableViewCell。

class UITableViewCellWithTextField: UITableViewCell {

    var textField: UITextField!

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

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

    func setup() {

        let textField = UITextField(frame: self.contentView.frame)
        self.contentView.addSubview(self.textField)
    }
}

接下来,您必须将此自定义UITableViewCell注册到UITableView中。将其添加到现有的UITableViewCell注册代码行中。

jobTableView.register(UITableViewCell.self, forCellReuseIdentifier: tableCell)
jobTableView.register(UITableViewCellWithTextField.self, forCellReuseIdentifier: "TextFieldCell")

下一步是声明一个变量以指示用户已启动添加新作业功能。

public class MiddlePartCell: BaseCell, UITableViewDelegate, UITableViewDataSource {

    var isHandlingAddJob = false

    ...
}

然后在你的handleAddJob函数

func handleAddJob(){

    self.isHandlingAddJob = true
    datas.insert("new data", at: 0)
    jobTableView.beginUpdates()
    jobTableView.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .automatic)
    jobTableView.endUpdates()
}

最后是让UITableView知道要处理添加新作业的传入新单元格,并且应该使用自定义文本字段单元格。

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

    if indexPath.row == 0 && self.isHandlingAddJob {

        // Handling new job cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldCell", for: indexPath) as! UITableViewCellWithTextField
        return cell
    }

    let cell = tableView.dequeueReusableCell(withIdentifier: tableCell, for: indexPath)

    cell.textLabel?.text = datas[indexPath.item]
    return cell
}

P.S。希望我能正确理解你的问题大声笑。

要在自定义表格视图单元格中保留文本字段的数据,您必须使用委托模式。首先,您必须将自定义表视图单元格或MiddlePartCell类符合UITextFieldDelegate。我会选择后者。

class MiddlePartCell: BaseCell, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

    ...
}

然后在您的cellForRowAt函数中,将自定义单元格的文本字段委托设置为您的MiddlePartCell。

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

    if indexPath.row == 0 && self.isHandlingAddJob {

        // Handling new job cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldCell", for: indexPath) as! UITableViewCellWithTextField
        cell.returnKeyType = .Done
        cell.textField.delegate = self
        return cell
    }

    ...
}

最后,使用UITextFieldDelegate函数。当在键盘上完成使用点击时,您的单元格将被重新加载为没有UITextField的普通UITableViewCell。

func textFieldShouldReturn(textField: UITextField) -> Bool {

    self.isHandlingAddJob = false
    self.datas.first! = textField.text!
    self.jobTableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Automatic)
    return true
}
相关问题