UITableView仅加载设备上可见的自定义单元格,并在添加一个单元格时崩溃

问题描述 投票:0回答:3

我使用一个空的UITableView与自定义单元格,我一个接一个地添加新项目没有任何问题。 tableView是可滚动的,但是当我向单元格添加一个项目时,应用程序崩溃时,该项目距离最后一个可见单元格更多一个索引。

当应用程序加载时,numberOfRowsinSection为1,每增加一个新条目,它增长1.如果设备有10个可见单元格,它会在11上崩溃。如果设备有6个可见单元格,它会在7上崩溃。应用程序在展开时意外发现nil一个可选值。

使用标题为UITableview Not scrolling?的问题中的建议我在viewDidLoad和我的函数中尝试了以下每一行:

   self.myTableView.delegate = self

  self.myTableView.autoresizingMask = UIView.AutoresizingMask.flexibleHeight;

  self.myTableView.rowHeight = UITableView.automaticDimension

   self.myTableView.bounces = true;

  self.myTableView.reloadData()

没有任何积极的结果。

这是代码:

var enterCounts: Int = 1 

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

  public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TextInputCell") as! TextInputTableViewCell

        return cell
    }        


  @IBAction func enter(_ sender: Any) {

    let activeRow = self.enterCounts - 1
    let index = IndexPath(row: activeRow, section: 0)
    let cell: TextInputTableViewCell  = self.myTableView.cellForRow(at: index) as! TextInputTableViewCell


    if cell.myTextField.text == ""  {
         "DO NOTHING"
    } else {

        "DO STUFF"

        enterCounts += 1

        self.myTableView.reloadData()

        let nextIndex = IndexPath(row: activeRow + 1, section: 0)


 "This is the line that finds nil and crashes when row is out of view"

        let nextCell: TextInputTableViewCell = self.myTableView.cellForRow(at: nextIndex) as! TextInputTableViewCell
        nextCell.myTextField.text = ""
        nextCell.myTextField.becomeFirstResponder()
    }

}

我希望UITableView滚动并继续加载用户输入的细胞,就像它对第一个/可见细胞一样。谢谢。

在2个答案之后,代码是:

    @IBAction func enter(_ sender: Any) {

    let activeRow = self.enterCounts - 1
    let index = IndexPath(row: activeRow, section: 0)
    let cell: TextInputTableViewCell  = self.myTableView.cellForRow(at: index) as! TextInputTableViewCell


    if cell.myTextField.text == ""  {
         "DO NOTHING"
    } else {

        "DO STUFF"

      enterCounts += 1
      let nextIndex = IndexPath(row: activeRow + 1, section: 0) 
      self.myTableView.insertRows(at: [nextIndex], with: .automatic)
      self.myTableView.scrollToRow(at: nextIndex,at: .middle, animated: true)

     //These lines are executed only when I am in visible cells 
     //when a new cell is added it is not ready to become first responder it is skipped and entered text is getting mixed up.
 if let nextCell: TextInputTableViewCell = self.myTableView.cellForRow(at: nextIndex) as? TextInputTableViewCell {
            nextCell.myTextField.text = ""
            nextCell.myTextField.becomeFirstResponder()
           }
    }

}

使用上面的代码,新单元格显得非常漂亮,但textField只能成为第一个响应者,对于出现在视图中的第一个单元格。

我在下面声明我的自定义单元格类

          @IBOutlet weak var myTextField: UITextField!

 public func configure(text: String?, placeholder: String) {


    myTextField.text = text
  //  myTextField.placeholder = placeholder

    myTextField.accessibilityValue = text
   // myTextField.accessibilityLabel = placeholder

}


override public func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

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

    // Configure the view for the selected state
}

如果我在tableView外部使用textField并保持tableView仅用于显示我输入的值,那么事情很简单但是当我尝试将第一个响应者作为新插入单元格的textField时,tableView的最后一个单元格会产生问题。

ios swift uitableview
3个回答
© www.soinside.com 2019 - 2024. All rights reserved.