简体   繁体   English

在点击识别器上识别表格单元格

[英]Identifying the table cell, when tapping on a Tap Recogniser

I have a tableview in my View. 我的视图中有一个tableview。 The cells are created using custom Cells. 使用自定义单元格创建单元格。 I need to display a large string in the table view cells So I had added the text Label in a Scrollview. 我需要在表格视图单元格中显示一个大字符串,因此我已在Scrollview中添加了文本Label。 Also I need to execute some code when the user taps on table view cell. 当用户点击表格视图单元格时,我还需要执行一些代码。 Please see the below code: 请看下面的代码:

 [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;

The problem is when the user touches above the Scroll View, the Tableview did select method will not be called. 问题是,当用户触摸滚动视图上方时,不会调用Tableview的select方法。 The solution I found for this problem is to add a gesture recogniser to the scroll view. 我针对此问题找到的解决方案是将手势识别器添加到滚动视图。 But in this solution, we have no ways to check which cell(or which gesture recogniser) was selected. 但是在这种解决方案中,我们无法检查选择了哪个单元格(或哪个手势识别器)。 Could anyone help me to find a solution for this problem? 有人可以帮我找到解决这个问题的方法吗?

You can get to know the cell by the following code 您可以通过以下代码了解单元格

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];

                    ...
    }
}

It's generally a bad idea putting scroll views inside scroll views. 将滚动视图放在滚动视图中通常是一个坏主意。 UITableView is also just a UIScrollView. UITableView也是UIScrollView。 That only kind of works if they are scrolling on different axis, ie the outer scroll view scrolling vertically and the inner scrolling horizontally. 只有当它们在不同的轴上滚动时(即外部滚动视图垂直滚动而内部滚动水平滚动),这才起作用。

For your specific scenario you would have to trigger the selection yourself. 对于您的特定情况,您必须自己触发选择。 Once you have a reference to the cell you can ask the table view for the indexPath of it. 一旦有了对单元格的引用,就可以向表视图询问其indexPath。 Then you would call the delegate method for didSelectRow... yourself. 然后,您自己调用didSelectRow的委托方法。

In the solution with the scrollview you are not able to scroll in the scrollview because the gestureRecognizer 'gets' the touch. 在带有滚动视图的解决方案中,您无法在滚动视图中滚动,因为gestureRecognizer会“获得”触摸。 Therefor I would not use the scrollview at all. 因此,我根本不会使用scrollview。

Make the label resize to its content like: 使标签大小调整为其内容,例如:

    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);

You also need to implement this in the heightForRowAtIndexPath 您还需要在heightForRowAtIndexPath中实现它

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGSize cellSize = [bigTextString sizeWithFont:customTextLabel.font constrainedToSize:CGSizeMake(generalCellWidth, 999999)];
    return cellSize.height;
}

This way you can just use the didSelectRowAtIndex method. 这样,您可以只使用didSelectRowAtIndex方法。



If you really want to use the scrollview, add a button to your cell in the cellForRowAtIndexPath: method. 如果您确实要使用滚动视图,请在cellForRowAtIndexPath:方法的单元格中添加一个按钮。 Make the button just as big as the cell and add a button tag like this: 使按钮与单元格一样大,并添加一个按钮标签,如下所示:

    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];

Then add: 然后加:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM