从自定义单元格返回 NSIndexPath ? (UI表格视图)

发布于 2024-12-03 22:20:35 字数 650 浏览 0 评论 0原文

我需要获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不是你的备胎 2024-12-10 22:20:35

只需为单元格的 tag 属性指定一个值即可。然后你可以通过调用这个来获取该单元格

UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];

,一旦你拥有了该单元格,你就可以得到这样的 NSIndexPath

NSIndexPath *indexPath = [self.tableView indexPathForCell:nextResponderCell];

因为你的自定义单元格中有一个 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 类中。

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];        

}

上面的 UITextField 委托方法应该为您提供当前所选单元格的索引路径。

Just give the cell a value for its tag property. Then you can get that cell by calling this

UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];

then once you have the cell you can get the NSIndexPath like this

NSIndexPath *indexPath = [self.tableView indexPathForCell:nextResponderCell];

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 this cell.tag = indexPath.row;

Now that you have set the UITextField delegate, you can now place this method in your UITableViewController class

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];        

}

The above UITextField delegate method should get you the indexPath for the cell you have currently selected.

走走停停 2024-12-10 22:20:35

试试这个代码..

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    UITableView *tableView;
    UITableViewCell* superViewCell = [self returnSuperCellViewOfTextField:textField];
    if(superViewCell)[tableView scrollToRowAtIndexPath:[tableView indexPathForCell:superViewCell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

-(id)returnSuperCellViewOfTextField:(id)viewToCheck
{
    id superView = [viewToCheck superview];
    if([viewToCheck isKindOfClass:[UITableViewCell class]] && superView)return superView;
    else if(([viewToCheck respondsToSelector:@selector(superview)] && superView)) return [self returnSuperCellViewOfTextField:superView];
    return nil;
}

try this code..

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    UITableView *tableView;
    UITableViewCell* superViewCell = [self returnSuperCellViewOfTextField:textField];
    if(superViewCell)[tableView scrollToRowAtIndexPath:[tableView indexPathForCell:superViewCell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

-(id)returnSuperCellViewOfTextField:(id)viewToCheck
{
    id superView = [viewToCheck superview];
    if([viewToCheck isKindOfClass:[UITableViewCell class]] && superView)return superView;
    else if(([viewToCheck respondsToSelector:@selector(superview)] && superView)) return [self returnSuperCellViewOfTextField:superView];
    return nil;
}
雨后咖啡店 2024-12-10 22:20:35

如果每个单元格中有多个文本字段,这(您在下面给出的答案)仍然有效吗?:

UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];
then once you have the cell you can get the NSIndexPath like this

NSIndexPath *indexPath = [self.tableView indexPathForCell:nextResponderCell];

由于您的自定义单元格中有一个 UITextField ,因此您可以放置​​ cell.textField.delegate = self;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

数据源方法中。同样,在相同的方法中,您可以标记两个单元格文本字段,如下所示 cell.textField.tag = indexPath.row;和像这样的单元格 cell.tag = indexPath.row;

现在您已经设置了 UITextField 委托,您现在可以将此方法放入 UITableViewController 类中

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

我有一个包含 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?:

UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];
then once you have the cell you can get the NSIndexPath like this

NSIndexPath *indexPath = [self.tableView indexPathForCell:nextResponderCell];

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. 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

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

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?

爱你是孤单的心事 2024-12-10 22:20:35

我喜欢像这样标记我的单元格:

/* Create a tag based on the section and row of a cell(used to retrive the cell for the beginEditingNextCell method. */
-(NSInteger)tagForIndexPath:(NSIndexPath*)path {
    return path.section * 1000 + path.row + 1;
}

另一种方法是使用自定义委托传递 beginEditing 数据,并包含一个单元格模型,然后您可以使用该模型根据您存储数据的方式来确定您的路径。

查看我的 github 项目,它有处理这种情况的代码,以及调整键盘大小和切换到下一个可编辑单元格的代码: https://github.com/andrewzimmer906/XCell

I like to tag my cells like so:

/* Create a tag based on the section and row of a cell(used to retrive the cell for the beginEditingNextCell method. */
-(NSInteger)tagForIndexPath:(NSIndexPath*)path {
    return path.section * 1000 + path.row + 1;
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文