reloadData没有调用cellForRowAtIndexPath

时间:2015-04-17 17:55:25

标签: ios uitableview swift

我知道这个话题已经提出了很多次,但我似乎无法通过。

这是一个简单的UITableViewController,其单元格是正常UITableViewCells或自定义单元格。

这允许用户在单元格中的textField中输入文本,并将其直接添加到tableView

当用户完成输入文本后,他将返回并且新条目将添加到listOfItems。此时我希望我的tableView重新加载其数据以显示最后添加的项目。

似乎不起作用。

class CreateListTableViewController: UITableViewController {

    // MARK: - Model

    var listOfItems = [String]()
        {
        didSet {
            tableView.reloadData()
            println(listOfItems)
        }
    }

    // MARK: - View LifeCycle

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBarHidden = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listOfItems.count + 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.row + 1 == listOfItems.count{
            let cell = tableView.dequeueReusableCellWithIdentifier("tableCellSearch", forIndexPath: indexPath) as! ListItemTableViewCell
            cell.configure(text: "", placeholder: "Enter an item")
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as! UITableViewCell
            println(indexPath.row)
            cell.textLabel?.text = listOfItems[indexPath.row]
            return cell

        }
    }
}

以下是自定义UITableViewCell的代码:

class ListItemTableViewCell: UITableViewCell, UITextFieldDelegate {

@IBOutlet weak var cellTextField: UITextField!

var controllerArray = CreateListTableViewController()

func configure(#text: String?, placeholder: String){
    cellTextField.text = text
    cellTextField.placeholder = placeholder

    cellTextField.accessibilityValue = text
    cellTextField.accessibilityLabel = placeholder
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    textField.text = ""
    return true
}

override func awakeFromNib() {
    super.awakeFromNib()
    cellTextField.delegate = self
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    controllerArray.listOfItems += [textField.text]
    textField.text = ""
    return true
}

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    return true
}


override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

有趣的是reloadData调用'numberOfRows'而不是'cellForRow'。

2 个答案:

答案 0 :(得分:1)

您的索引/大小已关闭,因此您的代码仅在listOfItems'大小为0.试试这个:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return listOfItems.count + 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == listOfItems.count {
        let cell = tableView.dequeueReusableCellWithIdentifier("tableCellSearch", forIndexPath: indexPath) as! ListItemTableViewCell
        cell.configure(text: "", placeholder: "Enter an item")
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as! UITableViewCell
        println(indexPath.row)
        cell.textLabel?.text = listOfItems[indexPath.row]
        return cell

    }
}

更新

好的,问题是在ListItemTableViewCell中,您正在创建CreateListTableViewController的新实例并将其分配给controllerArray。这意味着当您调用controllerArray.listOfItems + = [textField.text]时,您将在不在屏幕上的CreateListTableViewController的单独第二个实例上调用它。

相反,你应该在ListItemTableViewCell中保持controllerArray未初始化,然后像这样从cellForRowAtIndexPath设置它:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row + 1 == listOfItems.count{
        let cell = tableView.dequeueReusableCellWithIdentifier("tableCellSearch", forIndexPath: indexPath) as! ListItemTableViewCell
        cell.configure(text: "", placeholder: "Enter an item")
        cell.controllerArray = self
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as! UITableViewCell
        println(indexPath.row)
        cell.textLabel?.text = listOfItems[indexPath.row]
        return cell

    }
}

答案 1 :(得分:0)

您能确保正确更新内部列表吗?numberOfRowsInSection函数是否返回0或1以外的数字?

相关问题