无法在UITableview中的单元格内单击UIButton

Lon*_*als 11 iphone uibutton uitableview ios uitapgesturerecognizer

已经搜索了一些可能的修复,但都没有解决我的问题.

我一直在点击uitableview中的单元格,而不是单击其中的按钮.

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.backgroundColor = [UIColor blackColor];

    UIView *v  = nil;
    NSArray *array = [cell subviews];

    for (v in array) {
        NSLog(@"%@",[v class]);
        if( [v isKindOfClass:[UIView class]] ){
           [v removeFromSuperview];
        }
    }

    //tb
    if (tableView == self.tb) {

        v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tb.bounds.size.width, 120.0)];

        if([feeds count]>0){
            UIImageView *box=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"statusBox.png"]];

            box.userInteractionEnabled = YES;
            [v addSubview:box];

            AsyncImageView *imageView1 = [[AsyncImageView alloc] initWithFrame:CGRectMake(10, 20.0f, 34.0f, 34.0f)];
            imageView1.contentMode = UIViewContentModeScaleAspectFill;
            imageView1.clipsToBounds = YES;

            //cancel loading previous image for cell
            [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView1];
            if (users1 != nil && users1.imagelink != nil && (id) users1.imagelink !=   [NSNull null]){
               imageView1.imageURL = [NSURL URLWithString:users1.imagelink];
            }
            else{
                imageView1.image=[UIImage imageNamed:@"default_ProfilePic.png"];
            }

            UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
            tapped.numberOfTapsRequired = 1;
            [imageView1 addGestureRecognizer:tapped];
            [tapped release];

            imageView1.userInteractionEnabled = NO;
            [v addSubview:imageView1];


            UIFont *font     = [UIFont fontWithName:@"TrebuchetMS-Bold" size:11];
            UILabel *descLabel1 = [[UILabel alloc] initWithFrame: CGRectMake(52, 23, 160, 48)];
            descLabel1.text                 = [NSString stringWithFormat:@"%@ %@",users1.userfirstn,users1.userlastn];
            descLabel1.textColor            = [UIColor blackColor];
            descLabel1.font                 = font;
            descLabel1.adjustsFontSizeToFitWidth=YES;
            descLabel1.numberOfLines = 0;
            CGSize expectedLabelSize = [descLabel1.text sizeWithFont:descLabel1.font
                                               constrainedToSize:descLabel1.frame.size
                                                   lineBreakMode:UILineBreakModeWordWrap];

            CGRect newFrame = descLabel1.frame;
            newFrame.size.height = expectedLabelSize.height;
            descLabel1.frame = newFrame;
            descLabel1.numberOfLines = 0;

            descLabel1.userInteractionEnabled = NO;
            [v addSubview:descLabel1];


            UILabel *descLabel2= [[UILabel alloc] initWithFrame: CGRectMake(52, 43, 200, 48)];
            StatusClass *stat1=[feeds objectAtIndex:indexPath.row];
            descLabel2.text                 = [stat1 statcreate];
            descLabel2.textColor            = [UIColor blackColor];
            descLabel2.font                 = font;
            descLabel2.adjustsFontSizeToFitWidth=YES;
            descLabel2.numberOfLines = 0;
            CGSize expectedLabelSize2 = [descLabel2.text sizeWithFont:descLabel2.font
                                                constrainedToSize:descLabel2.frame.size
                                                    lineBreakMode:UILineBreakModeWordWrap];

            CGRect newFrame2 = descLabel2.frame;
            newFrame2.size.height = expectedLabelSize2.height;
            descLabel2.frame = newFrame2;
            descLabel2.numberOfLines = 0;

            descLabel2.userInteractionEnabled = NO;
            [v addSubview:descLabel2];

            UILabel *descLabel3= [[UILabel alloc] initWithFrame: CGRectMake(10, 63, 280, 80)];
            StatusClass *stat=[feeds objectAtIndex:indexPath.row];
            descLabel3.text                 = [stat stattext];
            descLabel3.textColor            = [UIColor blackColor];
            descLabel3.font                 = font;
            descLabel3.adjustsFontSizeToFitWidth=YES;
            descLabel3.numberOfLines = 0;

            descLabel3.userInteractionEnabled = NO;
            [v addSubview:descLabel3];

            //comment button
            UIButton *buttonC = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [buttonC addTarget:self action:@selector(sendComment:) forControlEvents:UIControlEventTouchUpInside];
            [buttonC setImage:[UIImage imageNamed:@"comment.png"] forState:UIControlStateNormal];
            buttonC.frame = CGRectMake(2, 160, 145, 35);

            buttonC.userInteractionEnabled = YES;
            [v addSubview:buttonC];

            //share button
            UIButton *buttonS = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            buttonS.tag = indexPath.row;
            [buttonS addTarget:self action:@selector(sendShare:) forControlEvents:UIControlEventTouchUpInside];
            [buttonS setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
            buttonS.frame = CGRectMake(150, 160, 140, 35);

            buttonS.userInteractionEnabled = YES;
            [v addSubview:buttonS];

        }
        v.userInteractionEnabled = YES;
        [cell addSubview:v];
    }
    return cell; 
}
Run Code Online (Sandbox Code Playgroud)

我也尝试了UITapGestureRecognizer按钮,仍然没有工作.

谢谢.

Mat*_*266 26

我对这个问题感到困惑....

我设法通过在一个项目上执行此操作来解决此问题:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! CustomCell

    cell.contentView.userInteractionEnabled = false // <<-- the solution

    return cell
}
Run Code Online (Sandbox Code Playgroud)

但同样不适用于不同的项目.我不知道为什么...


Ash*_*Ash 22

还有另外一个像这样的问题的潜在原因,那就是UITableViewCells现在有了contentView.这可以并将放置在自定义表格视图单元格中的其他视图之前,以便拾取触摸并检测单元格选择.这样做可能会阻止其后面的任何视图的触摸.在使用笔尖创建单元格时,我特别注意到这种烦恼.

处理此问题的可接受方法是确保所有子视图不是添加到tableview单元本身,而是添加到其中contentView.这是一个只读属性,可在某些情况下调整自己的帧,例如,当侧向滑动以显示删除按钮时,或在编辑模式期间.因此,您应确保您添加到的任何项目contentView都具有智能大小并具有正确的调整大小行为.

还要注意,通过添加单元格contentView,您可以自己阻止对它的触摸,从而防止选择单元格.我个人尝试不允许在包含具有自定义用户启用控件的单元格的tableviews中选择单元格.它只会导致混乱和尴尬的界面.事实上,我正在认真考虑用UITableViews我的未来项目替换所有项目UICollectionViews,这些项目更具适应性.

-灰


Lor*_*esh 13

我是这样做的:

let plusButton: UIButton = {
    let v = UIButton()
    v.setImage(plusImg, for: .normal)
    v.tintColor = .dark
    v.translatesAutoresizingMaskIntoConstraints = false


    // adding targets here did not work
    v.addTarget(self, action: #selector(plusButtonHandler), for: .touchUpInside)


    return v
}()
Run Code Online (Sandbox Code Playgroud)

并且似乎调用addTarget闭包内部的方法不起作用。

当我将addTarget方法分解出来并将其放入初始化程序时,一切正常!

  • 在经历了很多解决方案之后,这对我有用。多谢 :) (2认同)

Tai*_* Le 12

在我的情况下,我尝试通过 将 UIButton 直接添加到单元格self.addSubview(myButton),所以我更改为self.contentView.addSubview(myButton)并且它起作用了

似乎contentView是在前面myButton所以按钮不会收到点击手势。


HRM*_*HRM 5

您的UIButton='sy 偏移量被指定为 160,UIView's高度仅被指定为 120。请更改UIButton'sy 偏移量并尝试。


Ric*_*kye 4

UITableViewCell 默认处理所有手势。设置cell.selectionStyle = UITableViewCellSelectionStyleNone;以使其禁用默认行为。