Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 获取UITableview中自定义单元格的行索引_Iphone_Objective C_Ipad_Uitableview_Ios5 - Fatal编程技术网

Iphone 获取UITableview中自定义单元格的行索引

Iphone 获取UITableview中自定义单元格的行索引,iphone,objective-c,ipad,uitableview,ios5,Iphone,Objective C,Ipad,Uitableview,Ios5,我正在开发一款ipad应用程序。我在应用程序中有一个UITableview。UITableview以编程方式创建,其中一个标签和textview作为其子视图。tableview中最多可以有五行。一次向用户显示两行。在运行时,需要在textview中输入一些文本并将其保存在本地sqlite数据库中。我已经在代码中实现了textViewDidBeginEditing和TextViewDiEndediting委托。在TextViewDiEndediting委托中,我试图在NSMUtable数组中添加/

我正在开发一款ipad应用程序。我在应用程序中有一个UITableview。UITableview以编程方式创建,其中一个标签和textview作为其子视图。tableview中最多可以有五行。一次向用户显示两行。在运行时,需要在textview中输入一些文本并将其保存在本地sqlite数据库中。我已经在代码中实现了textViewDidBeginEditing和TextViewDiEndediting委托。在TextViewDiEndediting委托中,我试图在NSMUtable数组中添加/替换文本(在textview中输入)。为此,需要输入文本的textview的rowindex。这样我就可以在数组中添加/替换相应的行索引


请告诉我如何获取文本视图的行索引

您的
TextView
将包含在某种单元格中。找到此单元格后,可以向表中查询索引。从
TextView
浏览视图层次结构以查找单元格。例如:

TextView* textView = // your textView;
UITableViewCell* cell = (UITableViewCell*)[textView superview];
UITableView* table = (UITableView *)[cell superview];
NSIndexPath* pathOfTheCell = [table indexPathForCell:cell];
NSInteger rowOfTheCell = [pathOfTheCell row];
NSLog(@"rowofthecell %d", rowOfTheCell);

它非常简单,在
cellforrowatinexpath
中。只需将您的
textView
标记为

cell.txtView.tag = indexPath.row;
在textView委托方法中,使用以下代码查找行(
textViewDidBeginEditing


运行IOS 8.3在TextViewDiEndediting中发现并实现了这一点
方法;很好用

UIView *parentCell = sender
while (![parentCell isKindOfClass:[UITableViewCell class]]) {   // iOS 7 onwards the table cell hierachy has changed.
    parentCell = parentCell.superview;
}

UIView *parentView = parentCell.superview;

while (![parentView isKindOfClass:[UITableView class]]) {   // iOS 7 onwards the table cell hierachy has changed.
    parentView = parentView.superview;
}


UITableView *tableView = (UITableView *)parentView;
NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)parentCell];

NSLog(@"indexPath = %@", indexPath);

iOS 8.4上运行的Swift中:

@IBAction func signInUpBigButtonPressed(sender: SignInUpMenuTableViewControllerBigButton) {
    // We set the self.indexCellContainingButtonClicked with the cell number selected.
    var parentCell: UIView! = sender
    do {
        parentCell = parentCell.superview!
    } while !(parentCell is UITableViewCell)
    let cell = parentCell as! UITableViewCell
    self.indexCellContainingButtonClicked = self.tableView.indexPathForCell(cell)!.row
}

这是一个更优雅的方式来完成你的要求

CGPoint senderPosition = [sender convertPoint:CGPointZero toView:self.tableView];

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderPosition];

谢谢你的回复。这在某些情况下有效,在其他情况下,它会给出不适当的行索引。就像我从第0行导航到第5行,然后导航到第4行,然后导航到第0行一样,在TextViewDiEndEdit中,它将索引路径显示为0,而它应该显示3,这是我编辑的行的索引。如果在设置此标记后从tableView中添加或删除单元格,请小心-顺序将更改,使用错误的值保留标记。谢谢!在我看来,这是一个比其他办法好得多的解决办法。
CGPoint senderPosition = [sender convertPoint:CGPointZero toView:self.tableView];

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderPosition];