我需要得到 NSIndexPath
对于 UITableView
中的自定义单元格.问题是:我发送了 NSNotification
从我的自定义单元格到我的 UITableViewController
什么时候editingDidBegin
从 UITextField
调用在我的自定义单元格中。在我的 UITableViewController
, 我调整了 UITableView
的大小当UITextField
开始编辑,然后想让table view滚动到UITextField
所在的Cell是第一响应者。但我不知道如何返回 indexPath
UITextField
所在的单元格正在编辑中。我已经尝试了很多方法,但仍然无法正常工作。一种方法是:在我的 cstomCell 类中,我使用 [self setSelected:YES]
选择行然后在我的电视 Controller 中,如果我 NSLog
[self.tableV indexPathForSelectedRow]
的行它总是返回 0,即使它始终不是 0。
最佳答案
只需为单元格的 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 委托(delegate),您现在可以将此方法放在您的 UITableViewController 类中
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
上面的 UITextField 委托(delegate)方法应该为您提供当前所选单元格的 indexPath。
关于objective-c - 从自定义单元格返回 NSIndexPath ? (UITableView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7354121/