从自定义单元格返回 NSIndexPath ? (UI表格视图)
我需要获取 UITableView 中自定义单元格的 NSIndexPath 。问题是这样的:当从 UITextField
调用 editingDidBegin
时,我从自定义单元格发送 NSNotification
到我的 UITableViewController
在我的自定义单元格中。在我的 UITableViewController
中,当 UITextField
开始编辑时,我调整了 UITableView
的大小,然后希望表格视图滚动到 UITableView
所在的单元格。 code>UITextField 是第一响应者。但我不知道如何返回正在编辑 UITextField
的单元格的 indexPath
。我已经尝试了很多方法,但仍然不起作用。一种方法是:在我的 cstomCell 类中,我使用 [self setSelected:YES]
选择行,然后在我的电视控制器中,如果我 NSLog
则选择 [ 的行self.tableV indexPathForSelectedRow]
它总是返回 0,即使它总是不为 0。
I need to get the NSIndexPath
for a custom cell in a UITableView
. Here's the problem: I send a NSNotification
from my custom cell to my UITableViewController
when editingDidBegin
gets called from a UITextField
in my custom cell. In my UITableViewController
, I resize the UITableView
when the UITextField
began editing, and then want the table view to scroll to the Cell in which the UITextField
is first responder. But I can't figure out how to return the indexPath
of the cell where the UITextField
is being edited. I have tried so many ways, but its still not working. One way is this: in my cstomCell class i select the row using [self setSelected:YES]
and in my TV controller then if I NSLog
the row of [self.tableV indexPathForSelectedRow]
it always returns 0 even though its always not 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需为单元格的
tag
属性指定一个值即可。然后你可以通过调用这个来获取该单元格,一旦你拥有了该单元格,你就可以得到这样的 NSIndexPath
因为你的自定义单元格中有一个 UITextField,你可以将
cell.textField.delegate = self;
放置在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
数据源方法。这样您就不必在自定义单元中设置 NSNotification。同样,在相同的方法中,您可以标记单元格文本字段(如
cell.textField.tag = indexPath.row;
)和单元格(如cell.tag = indexPath.row;
) code>现在您已经设置了 UITextField 委托,现在可以将此方法放入 UITableViewController 类中。
上面的 UITextField 委托方法应该为您提供当前所选单元格的索引路径。
Just give the cell a value for its
tag
property. Then you can get that cell by calling thisthen once you have the cell you can get the NSIndexPath like this
Since you have a UITextField in your custom cell you can place
cell.textField.delegate = self;
in the- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
data source method. That way you will not have to setup a NSNotification in the Custom Cell. Also in this same method you can tag both your cells text field like this
cell.textField.tag = indexPath.row;
and the cell like thiscell.tag = indexPath.row;
Now that you have set the UITextField delegate, you can now place this method in your UITableViewController class
The above UITextField delegate method should get you the indexPath for the cell you have currently selected.
试试这个代码..
try this code..
如果每个单元格中有多个文本字段,这(您在下面给出的答案)仍然有效吗?:
由于您的自定义单元格中有一个
UITextField
,因此您可以放置cell.textField.delegate = self;
在数据源方法中。同样,在相同的方法中,您可以标记两个单元格文本字段,如下所示 cell.textField.tag = indexPath.row;和像这样的单元格 cell.tag = indexPath.row;
现在您已经设置了 UITextField 委托,您现在可以将此方法放入 UITableViewController 类中
我有一个包含 8 个单元格的表格视图,在每个单元格中我有 6 个文本字段,而且表格视图是动态的,因此用户可以插入更多单元格...但是我在如何保存用户在每个文本字段中输入的文本时遇到了麻烦。是否有可能通过“cell.textField.tag = indexPath.row”,控制器识别出我所在的文本字段?
Does this (your answer that you gave before below)still works if you have multiple texfields in each cell?:
Since you have a
UITextField
in your custom cell you can placecell.textField.delegate = self;
in thedata source method. Also in this same method you can tag both your cells text field like this cell.textField.tag = indexPath.row; and the cell like this cell.tag = indexPath.row;
Now that you have set the UITextField delegate, you can now place this method in your UITableViewController class
I have a tableview with 8 cells, and in each cell i have 6 texfields, and also the tableview is dynamic, so the user can insert more cells...but i´m having trouble in how to save the text the user enters in each texfield. Is it possible that with "cell.textField.tag = indexPath.row", the controller identify´s the texfield i´m in?
我喜欢像这样标记我的单元格:
另一种方法是使用自定义委托传递 beginEditing 数据,并包含一个单元格模型,然后您可以使用该模型根据您存储数据的方式来确定您的路径。
查看我的 github 项目,它有处理这种情况的代码,以及调整键盘大小和切换到下一个可编辑单元格的代码: https://github.com/andrewzimmer906/XCell
I like to tag my cells like so:
The other way is to pass the beginEditing data using a custom delegate, and include a cell model, which you can then use to figure out your path depending on how you are storing data.
Check out my github project, it has the code to handle this case, as well as resizing for the keyboard and tabbing to the next editable cell: https://github.com/andrewzimmer906/XCell