objective-c - 从自定义单元格返回 NSIndexPath ? (UITableView)

标签 objective-c ios uitableview nsindexpath

我需要得到 NSIndexPath对于 UITableView 中的自定义单元格.问题是:我发送了 NSNotification从我的自定义单元格到我的 UITableViewController什么时候editingDidBeginUITextField 调用在我的自定义单元格中。在我的 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/

相关文章:

ios - presentViewController 隐藏 presentingViewController

iphone - 使用 skpsmtpmessage 问题从 iphone 应用程序发送邮件

ios - 在 GCD 中,是否保证串行队列中的所有任务都在同一个线程中运行?

ios - 不可重现的 webcore 崩溃

ios - 选择语句 SQLite 和 iOS

iphone - 返回时取消选择 TableView 行

ios - 一旦在TableView中选择了单元格,就更新单元格的渐变颜色

objective-c - iOS UITableView : Custom sections using information from a JSON object, 不只是计算它

iphone - 从 NSOperation 中取消 NSOperationQueue

ios - xcode 7 类型参数不能应用于非参数化类