Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如果填写了上一个单元格的文本字段,则将单元格添加到UITableView_Ios_Swift_Xcode_Uitableview - Fatal编程技术网

Ios 如果填写了上一个单元格的文本字段,则将单元格添加到UITableView

Ios 如果填写了上一个单元格的文本字段,则将单元格添加到UITableView,ios,swift,xcode,uitableview,Ios,Swift,Xcode,Uitableview,我的ViewController中有一个UITableView。每个单元格都包含一个textfield,用户可以在其中输入新文本(待办事项列表)。现在,我想添加一个新的单元格,它已经包含一个新的textfield,前提是前一个单元格的textfield包含文本 目前,我使用ui按钮添加一个新单元格,这可以工作并调整表格视图的大小,使所有单元格都可见,但我希望在填充前一个单元格后自动执行此操作,使其变得更加用户友好 这是我当前用于添加新行的代码: @IBAction func btnAd

我的
ViewController
中有一个
UITableView
。每个单元格都包含一个
textfield
,用户可以在其中输入新文本(待办事项列表)。现在,我想添加一个新的单元格,它已经包含一个新的
textfield
,前提是前一个单元格的
textfield
包含文本

目前,我使用
ui按钮添加一个新单元格,这可以工作并调整表格视图的大小,使所有单元格都可见,但我希望在填充前一个单元格后自动执行此操作,使其变得更加用户友好

这是我当前用于添加新行的代码:

    @IBAction func btnAddRow_TouchUpInside(_ sender: Any) {

    toDoTableView.beginUpdates()
    amountCells.append("")
    self.toDoTableView.insertRows(at: [IndexPath.init(row: self.amountCells.count - 1, section: 0)] , with: .automatic)
    toDoTableView.endUpdates()
    tblHeight.constant = toDoTableView.contentSize.height

}
你知道我怎样才能做到这一点吗?提前谢谢

编辑:我尝试使用EditingDidEnd

    class TextFieldsCell: UITableViewCell {

    @IBOutlet weak var txtToDo: UITextField!

    @IBAction func txtToDo_EditingDidEnd(_ sender: Any) {
            if(!(txtToDo.text?.isEmpty)! {
                print("Insert Code to Add New Row")
            }
        }
}

执行此操作时,我无法从我的
ViewController
访问
toDoTableView
。这可能导致的另一个问题是,当已经有5行并且第一行被简单地编辑时,另一行将插入,而我不希望插入。

将此添加到您的
textfield
委托并用您的tableview名称替换
tableview

func textFieldDidEndEditing(textField: UITextField!){

    var cell: UITableViewCell = textField.superview.superview as UITableViewCell
    var table: UITableView = cell.superview as UITableView
    let textFieldIndexPath = table.indexPathForCell(cell)
    if textFieldIndexPath.row == (yourdataArray.count - 1)
    {
        print("came to last row")

        if ((textField.text?.count)! > 0){

            print("text available")

            toDoTableView.beginUpdates()
            amountCells.append("")
            self.toDoTableView.insertRows(at: [IndexPath.init(row:
                self.amountCells.count - 1, section: 0)] , with: .automatic)
            toDoTableView.endUpdates()
            tblHeight.constant = toDoTableView.contentSize.height
        }
    }
}

将此项添加到您的
textfield
委托中,并将
tableview
替换为您的tableview名称

func textFieldDidEndEditing(textField: UITextField!){

    var cell: UITableViewCell = textField.superview.superview as UITableViewCell
    var table: UITableView = cell.superview as UITableView
    let textFieldIndexPath = table.indexPathForCell(cell)
    if textFieldIndexPath.row == (yourdataArray.count - 1)
    {
        print("came to last row")

        if ((textField.text?.count)! > 0){

            print("text available")

            toDoTableView.beginUpdates()
            amountCells.append("")
            self.toDoTableView.insertRows(at: [IndexPath.init(row:
                self.amountCells.count - 1, section: 0)] , with: .automatic)
            toDoTableView.endUpdates()
            tblHeight.constant = toDoTableView.contentSize.height
        }
    }
}

您可以检查viewController本身中的textfield是否为空。您只需确认来自故事板cellForRowAt中文本字段的委托,并将标记赋予该文本字段

e、 g,

并在textfield delegate方法中检查textfield是否为空。在方法中创建单元格的对象,如

func textFieldDidEndEditing(_ textField: UITextField) {
     rowBeingEditedInt = textField.tag
     let indexPath = IndexPath(row:rowBeingEdited! , section:0 )
     let cell = yourTableView.cellForRow(at: indexPath) as! yourTableViewCell
  // check cell.yourtextField.text isempty and do your condition
 }

您可以检查viewController本身中的textfield是否为空。您只需确认来自故事板cellForRowAt中文本字段的委托,并将标记赋予该文本字段

e、 g,

并在textfield delegate方法中检查textfield是否为空。在方法中创建单元格的对象,如

func textFieldDidEndEditing(_ textField: UITextField) {
     rowBeingEditedInt = textField.tag
     let indexPath = IndexPath(row:rowBeingEdited! , section:0 )
     let cell = yourTableView.cellForRow(at: indexPath) as! yourTableViewCell
  // check cell.yourtextField.text isempty and do your condition
 }
你可以试试这个

cellForRowAt
methodset
UITableViewCell
textField中,首先委派

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:TableViewCell = self.tblvw.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    cell.txtField.delegate = self
    cell.txtField.tag = indexPath.row
    cell.txtField?.text = amountCells[indexPath.row]

    return cell
}
textfielddidediting
中,您只需在下面进行检查,并根据检查结果执行进一步的任务

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    return textField.resignFirstResponder()
}

func textFieldDidEndEditing(_ textField: UITextField) {
    if textField.text?.count == 0 || textField.tag != self.amountCells.count - 1 {

    }
    else {
        self.tblvw.beginUpdates()
        amountCells.append("")
        self.tblvw.insertRows(at: [IndexPath.init(row: self.amountCells.count - 1, section: 0)] , with: .automatic)
        self.tblvw.endUpdates()
    }
}
你可以试试这个

cellForRowAt
methodset
UITableViewCell
textField中,首先委派

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:TableViewCell = self.tblvw.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    cell.txtField.delegate = self
    cell.txtField.tag = indexPath.row
    cell.txtField?.text = amountCells[indexPath.row]

    return cell
}
textfielddidediting
中,您只需在下面进行检查,并根据检查结果执行进一步的任务

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    return textField.resignFirstResponder()
}

func textFieldDidEndEditing(_ textField: UITextField) {
    if textField.text?.count == 0 || textField.tag != self.amountCells.count - 1 {

    }
    else {
        self.tblvw.beginUpdates()
        amountCells.append("")
        self.tblvw.insertRows(at: [IndexPath.init(row: self.amountCells.count - 1, section: 0)] , with: .automatic)
        self.tblvw.endUpdates()
    }
}

通过结合@Mauli和@Vicky_Vignesh/@Kuldeep的答案最终解决了这个问题

    func textFieldDidEndEditing(_ textField: UITextField) {
    let rowBeingEditedInt = textField.tag
    let indexPath = IndexPath(row:rowBeingEditedInt , section:0 )
    let cell = toDoTableView.cellForRow(at: indexPath) as! TextFieldsCell
    let table: UITableView = cell.superview as! UITableView

    let textFieldIndexPath = table.indexPath(for: cell)
    if textFieldIndexPath?.row == (amountCells.count - 1) {

        if(!(cell.txtToDo.text?.isEmpty)!) {
            toDoTableView.beginUpdates()
            amountCells.append("")
            self.toDoTableView.insertRows(at: [IndexPath.init(row: self.amountCells.count - 1, section: 0)] , with: .automatic)
            toDoTableView.endUpdates()
            tblHeight.constant = toDoTableView.contentSize.height
        }

    }

}

谢谢大家!非常感谢。

通过结合@Mauli和@Vicky_Vignesh/@Kuldeep的答案最终解决了这个问题

    func textFieldDidEndEditing(_ textField: UITextField) {
    let rowBeingEditedInt = textField.tag
    let indexPath = IndexPath(row:rowBeingEditedInt , section:0 )
    let cell = toDoTableView.cellForRow(at: indexPath) as! TextFieldsCell
    let table: UITableView = cell.superview as! UITableView

    let textFieldIndexPath = table.indexPath(for: cell)
    if textFieldIndexPath?.row == (amountCells.count - 1) {

        if(!(cell.txtToDo.text?.isEmpty)!) {
            toDoTableView.beginUpdates()
            amountCells.append("")
            self.toDoTableView.insertRows(at: [IndexPath.init(row: self.amountCells.count - 1, section: 0)] , with: .automatic)
            toDoTableView.endUpdates()
            tblHeight.constant = toDoTableView.contentSize.height
        }

    }

}


谢谢大家!非常感谢。

在文本字段中编辑。。您需要检查textfield是否已填充?如果是fillup,则在numberofRowInSection中添加一行并重新加载表格。@HardikThakkar我曾尝试使用DiEndediting,然后检查textfield是否为空,但这是在我的单元格子类中(因为textfield在那里),我无法访问ToTableView..@PennyWise,您需要在ViewController中选中此项,它将变为easy@Kuldeep我也试过了。但是,当我在ViewController中添加textfield时,会出现错误:非法配置:从ViewController到UITextField的TXTODO出口无效。出口无法连接到重复内容。-这就是为什么我在cellIn textfield didend editing的子类中设置了这个。。您需要检查textfield是否已填充?如果是fillup,则在numberofRowInSection中添加一行并重新加载表格。@HardikThakkar我曾尝试使用DiEndediting,然后检查textfield是否为空,但这是在我的单元格子类中(因为textfield在那里),我无法访问ToTableView..@PennyWise,您需要在ViewController中选中此项,它将变为easy@Kuldeep我也试过了。但是,当我在ViewController中添加textfield时,会出现错误:非法配置:从ViewController到UITextField的TXTODO出口无效。出口无法连接到重复内容。-这就是为什么我在单元格的一个子类中设置了此选项。您的答案在
目标C
Swift
中?这似乎是Swift 2;OP看起来像是在用Swift 3或4,的确是Swift 4。如何转换它?@JohnAyers、
origin
point
indexpath
和其他都在目标C中。让textFieldIndexPath=table。indexpathfell(cell)
应该是
表。indexpath(for:cell)
你的答案在
目标C
Swift
中?这似乎是Swift 2;OP看起来像是在用Swift 3或4,的确是Swift 4。如何转换它?@JohnAyers、
origin
point
indepath
和其他都在目标C中。让textfieldIndepath=table.indepath(cell)应该是
table.indepath(for:cell)
我已经实现了这段代码,对它做了一点修改,并且可以正常工作。谢谢我现在唯一的问题是,当编辑结束时,即使下面已经有一行,也会插入一行。因此,如果我编辑上一行,也会插入另一行。我将查看其他答案以了解如何解决此问题。您可以在对象中设置标志,以检查该单元格是否已编辑。若标志为“真”,那个么只需更新单元格,若标志为“假”,那个么插入新的。我已经实现了这段代码,对它做了一些修改,它可以正常工作。谢谢我现在唯一的问题是,当编辑结束时,即使下面已经有一行,也会插入一行。因此,如果我编辑上一行,也会插入另一行。我将查看其他答案以了解如何解决此问题。您可以在对象中设置标志,以检查该单元格是否已编辑。如果标志为“true”