【问题标题】:Return NSIndexPath from custom cell ? (UITableView)从自定义单元格返回 NSIndexPath ? (UITableView)
【发布时间】:2011-09-08 20:32:21
【问题描述】:

我需要为UITableView 中的自定义单元格获取NSIndexPath。这就是问题所在:当我的自定义单元格中的editingDidBegin 被从UITextField 调用时,我将一个NSNotification 从我的自定义单元格发送到我的UITableViewController。在我的UITableViewController 中,当UITextField 开始编辑时,我调整了UITableView 的大小,然后希望表格视图滚动到UITextField 是第一响应者的单元格。但我不知道如何返回正在编辑UITextField 的单元格的indexPath。我已经尝试了很多方法,但它仍然无法正常工作。一种方法是:在我的 cstomCell 类中,我使用 [self setSelected:YES] 选择行,然后在我的电视控制器中,如果我 NSLog [self.tableV indexPathForSelectedRow] 的行,它总是返回 0,即使它始终不是 0。

【问题讨论】:

  • 你不想让他的frame吗?

标签: objective-c ios uitableview nsindexpath


【解决方案1】:

只需为单元格的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;这样的单元格

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

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

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

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

}

上面的 UITextField 委托方法应该会为您获取您当前选择的单元格的 indexPath。

【讨论】:

  • 我不明白,请提供更多代码。这两行代码没有关系,请告诉我哪一行代码放在哪里。
  • @Maxner 我已经编辑了我的答案。希望这能消除任何困惑。
【解决方案2】:

试试这个代码..

- (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;
}

【讨论】:

    【解决方案3】:

    如果您在每个单元格中有多个 texfields,这(您在下面给出的答案)是否仍然有效?:

    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 个 texfields,并且表格视图是动态的,因此用户可以插入更多单元格...但是我在如何保存文本时遇到了麻烦用户在每个 texfield 中输入。是否有可能使用“cell.textField.tag = indexPath.row”,控制器识别我所在的 texfield?

    【讨论】:

      【解决方案4】:

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

      /* 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

      【讨论】:

      • 你的 github 项目一团糟,构建时至少有 20 个错误和警告。您能否提供更多与我的问题相关的代码?我有 50 个未结赏金。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      相关资源
      最近更新 更多