知道自定义 UITableViewCell 中的行号 - 新皱纹 [英] Know the row number inside custom UITableViewCell - new wrinkle

查看:19
本文介绍了知道自定义 UITableViewCell 中的行号 - 新皱纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的 UITableViewCell 类.

I have a custom UITableViewCell Class.

单元格具有可编辑的 UITextField.编辑此文本字段时,我会在

The cell has editable UITextField. when this textfield is edited I trap the end of edit inside

func textFieldDidEndEditing(_ textField: UITextField)

我的自定义单元格类实现了 UITextFieldDelegate,所以我确实收到了回电.

my custom cell class implements UITextFieldDelegate so I do indeed get the call back.

与对类似问题的所有其他回答类似 - 我首先尝试在加载表格时将 UITextField 上的标签设置为行号.

Similar to all the other responses to similar questions - I first tried to just set the tag on the UITextField to the row number when loading the table.

这有效 - 但是 - 现在有皱纹......我的表允许移动行.当行被移动时,它会保留表加载时的标签,所以我的标签不会从插入开始向下全部关闭.我可以通过在移动后重新加载表格来解决这个问题 - 但这会使列表跳回到顶部.

This works - BUT - now there is a wrinkle .... My table allows the rows to be moved. When the row is moved it retains the tag from when the table was loaded so not my tags are all off from the insert on down. I could solve this by reloading the table after a move - but that jumps the list back to the top.

1) 是否有一种更简洁的方法可以在不使用 .tag 的情况下获取自定义单元格类中的行号?因为当我允许移动行时这会失败.

1) is there a cleaner way to get the row number inside the custom cell class without using .tag? because this fails when I allow rows to be moved.

2) 如果不是 #1 那么有没有办法继续并重新加载显示以首先抓取当前显示的顶部行,然后在表重新加载后重置为相同的顶部当前行,这样用户就不会跳回顶?

2) if no to #1 then is there a way to go ahead and reload the display for first grab the current top displayed row and then after table reload reset to the same top current row so the users does not jump back to the top?

我也试过只用这个

  func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?)

并在包含 UITableView 的 UIViewController 中捕获编辑结束,因此我将在编辑后获得行号 BUT 这永远不会被调用.所以解决这个问题的另一种方法可能是强制 didEndEditingRowAt 被调用.由于某种原因,这不会被调用.即使我把客户单元类塞进了强制结束编辑的请求

and trap the end of editing inside the UIViewController that contains the UITableView so I would have the row number after the edit BUT this never gets called. so the other way to solve this might be to force didEndEditingRowAt to get called. For some reason this wont get called. even if I tuck in to the customer cell class the request to force end of editing

var textFieldHandle : UITextField?
func textFieldDidBeginEditing(_ textField: UITextField) {
    print("editing field in cell")
    textFieldHandle = textField
}

func textFieldDidEndEditing(_ textField: UITextField) {
    print("done editing text field in cell with text -\(textField.text!)-")
    if textFieldHandle != nil {
        textFieldHandle?.endEditing(true)   // force end editing so get table event - does not work
        if let index = textFieldHandle?.tag {
            if index >= 0 && index < quizListItems.count {
                // if we have a proper index then store back in answer array so we dont lose it
                quizListItems[index].text = textField.text!
                print("--set value at index \(index)")

            }
        }

    }

}

推荐答案

我做到了....

func textFieldDidEndEditing(_ textField: UITextField) {
    let cell: UITableViewCell = textField.superview as! UITableViewCell
    let textFieldIndexPath = tableView.indexPath(for: cell)
    if let index = textFieldIndexPath?.row {
        if index >= 0 && index < quizListItems.count {
            // Stuff the input textField value in to the array that loads the table
            quizListItems[index].text = textField.text!
        }
    }
}

注意:UITextFieldDelegate 在我的 ViewController(包含 UITableView)上,而不是在我的自定义 UITableViewCell 类上

Note: the UITextFieldDelegate is on my ViewController (that contains the UITableView) and not on my custom UITableViewCell class

还有什么失败的---尝试使用 didSelectRow 和 didDeselectRow - 但这些永远不会被调用,因为 UITextField 吃掉了点击.在与 didSelect 路径的组合中,我试图禁用或使 UITextField 不接受用户操作,直到我单击该字段.这是一团糟.出于某种原因,尝试将单元格放入 didSelect 和 didDeselect 时会出现错误.把它拿回来

what else failed --- Tried using didSelectRow and didDeselectRow - BUT these never get called because the UITextField eats the click. In combo with didSelect path I tried to disable or make the UITextField not accept user action until i click in to the field. This was a mess. for some reason there are errors trying to get the cell inside of didSelect and didDeselect. Took that back out and

什么工作(上面的代码片段)---回到 textFieldDidEndEditing 的 UITextField 委托方法,并返回到单元格以获取索引.这有效并且即使在我移动一行时仍然有效.

what worked (the code snip above) --- went back to the UITextField delegate method for textFieldDidEndEditing and worked backwards to the cell to get the index. This works and still works even in I move a row.

此路径从 UITextField 返回到单元格,然后从单元格获取索引.

This path gets from the UITextField back to the cell then fro the cell get the index.

这篇关于知道自定义 UITableViewCell 中的行号 - 新皱纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆