Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.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
Ios 在点击识别器时识别表格单元格_Ios_Uitableview_Uiscrollview_Uigesturerecognizer - Fatal编程技术网

Ios 在点击识别器时识别表格单元格

Ios 在点击识别器时识别表格单元格,ios,uitableview,uiscrollview,uigesturerecognizer,Ios,Uitableview,Uiscrollview,Uigesturerecognizer,我的视图中有一个表视图。这些单元格是使用自定义单元格创建的。我需要在表格视图单元格中显示一个大字符串,因此我在Scrollview中添加了文本标签。当用户点击表视图单元格时,我还需要执行一些代码。请参阅以下代码: [cell.textLabelLine2 setFrame:CGRectMake(cell.textLabelLine2.frame.origin.x, cell.textLabelLine2.frame.origin.y, 500, cell.textLabelLine2.fram

我的视图中有一个表视图。这些单元格是使用自定义单元格创建的。我需要在表格视图单元格中显示一个大字符串,因此我在Scrollview中添加了文本标签。当用户点击表视图单元格时,我还需要执行一些代码。请参阅以下代码:

 [cell.textLabelLine2 setFrame:CGRectMake(cell.textLabelLine2.frame.origin.x, cell.textLabelLine2.frame.origin.y, 500, cell.textLabelLine2.frame.size.height)];
   cell.scrollView.contentSize = CGSizeMake(cell.textLabelLine2.text.length*10 , 10);
   cell.scrollView.pagingEnabled = NO;

问题是,当用户触摸滚动视图上方时,将不会调用Tableview did select方法。我发现解决这个问题的方法是在滚动视图中添加一个手势识别器。但在这个解决方案中,我们无法检查选择了哪个单元格(或哪个手势识别器)。有人能帮我找到解决这个问题的办法吗

您可以通过以下代码了解单元格

if(gestureRecognizer.state == UIGestureRecognizerStateBegan) {
    CGPoint p = [gestureRecognizer locationInView:[self tableView]];

    NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:p];

    if(indexPath != nil) {

        UITableViewCell *cell = [[self tableView] cellForRowAtIndexPath:indexPath];

                    ...
    }
}

您可以通过以下代码了解该单元格

if(gestureRecognizer.state == UIGestureRecognizerStateBegan) {
    CGPoint p = [gestureRecognizer locationInView:[self tableView]];

    NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:p];

    if(indexPath != nil) {

        UITableViewCell *cell = [[self tableView] cellForRowAtIndexPath:indexPath];

                    ...
    }
}

将滚动视图放在滚动视图中通常是个坏主意。UITableView也只是一个UIScrollView。只有当它们在不同的轴上滚动时才有效,即外部滚动视图垂直滚动,内部滚动视图水平滚动


对于您的特定场景,您必须自己触发选择。一旦有了对单元格的引用,就可以向表视图询问它的indexPath。然后调用didSelectRow的委托方法。。。你自己。

将滚动视图放在滚动视图中通常是个坏主意。UITableView也只是一个UIScrollView。只有当它们在不同的轴上滚动时才有效,即外部滚动视图垂直滚动,内部滚动视图水平滚动


对于您的特定场景,您必须自己触发选择。一旦有了对单元格的引用,就可以向表视图询问它的indexPath。然后调用didSelectRow的委托方法。。。您自己。

在使用scrollview的解决方案中,您无法在scrollview中滚动,因为手势识别器“获得”触摸。因此,我根本不会使用scrollview

使标签按其内容调整大小,如:

    CGSize customTextLabelSize = [cell.customTextLabel.text sizeWithFont:cell.customTextLabel.font constrainedToSize:CGSizeMake(cell.customTextLabel.frame.size.width, 999999)];
    cell.customTextLabel.frame = CGRectMake(cell.customTextLabel.frame.origin.x, cell.customTextLabel.frame.origin.y, cell.customTextLabel.frame.size.width, customTextLabelSize.height);
您还需要在heightForRowAtIndexPath中实现这一点

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGSize cellSize = [bigTextString sizeWithFont:customTextLabel.font constrainedToSize:CGSizeMake(generalCellWidth, 999999)];
    return cellSize.height;
}
这样,您就可以使用didselectrowatinex方法



如果确实要使用scrollview,请在cellForRowAtIndexPath:方法中的单元格中添加一个按钮。使按钮与单元格一样大,并添加如下按钮标记:

    UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cellButton.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
    cellButton.tag = indexPath.row;
    [cellButton addTarget:self action:@selector(cellButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:cellButton];
然后添加:

-(void)cellButtonAction:(UIButton*)sender
{
    //do something with sender.tag
}

在使用scrollview的解决方案中,您无法在scrollview中滚动,因为手势识别器“获得”触摸。因此,我根本不会使用scrollview

使标签按其内容调整大小,如:

    CGSize customTextLabelSize = [cell.customTextLabel.text sizeWithFont:cell.customTextLabel.font constrainedToSize:CGSizeMake(cell.customTextLabel.frame.size.width, 999999)];
    cell.customTextLabel.frame = CGRectMake(cell.customTextLabel.frame.origin.x, cell.customTextLabel.frame.origin.y, cell.customTextLabel.frame.size.width, customTextLabelSize.height);
您还需要在heightForRowAtIndexPath中实现这一点

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGSize cellSize = [bigTextString sizeWithFont:customTextLabel.font constrainedToSize:CGSizeMake(generalCellWidth, 999999)];
    return cellSize.height;
}
这样,您就可以使用didselectrowatinex方法



如果确实要使用scrollview,请在cellForRowAtIndexPath:方法中的单元格中添加一个按钮。使按钮与单元格一样大,并添加如下按钮标记:

    UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cellButton.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
    cellButton.tag = indexPath.row;
    [cellButton addTarget:self action:@selector(cellButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:cellButton];
然后添加:

-(void)cellButtonAction:(UIButton*)sender
{
    //do something with sender.tag
}