iphone - 无法在 UITableview 的单元格内单击 UIButton

标签 iphone ios uitableview uibutton 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; 
}

我还为按钮尝试了 UITapGestureRecognizer,但仍然无法正常工作。

谢谢。

最佳答案

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

我通过在一个项目上这样做设法解决了这个问题:

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
}

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

关于iphone - 无法在 UITableview 的单元格内单击 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18866420/

相关文章:

iphone - 在 iPad 应用程序中添加图像效果

c++ - 为什么这个 EXC_BAD_ACCESS 发生在 long long 而不是 int 上?

ios - PhoneRTC 64 位支持吗?

ios - TableView 问题,indexPath 为零

ios - 将图像添加到 UITableView 中的按钮 "editActionsForRowAt"

objective-c - 如何不使用 dequeueReusableCellWithIdentifier?

ios - 带部分的动态 TableView

iphone - 在 cocos2d 中使用 NSTimer 制作随机对象

iphone - 禁用特定类的 NSLog

ios - UITableView 不显示数据。