21

すでにいくつかの可能な修正を検索しましたが、すべてが解決しませんでした。

内部のボタンではなく、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が、まだ機能していません。

ありがとう。

4

14 に答える 14

12

私の場合、UIButtonをセルに直接追加しようとしたself.addSubview(myButton)ので、に変更しself.contentView.addSubview(myButton)て機能しました

contentViewが の前にあったようmyButtonで、ボタンはタップ ジェスチャを受け取りません。

于 2020-08-31T10:36:54.050 に答える
4

UITableViewCell は、デフォルトですべてのジェスチャを処理しています。cell.selectionStyle = UITableViewCellSelectionStyleNone;デフォルトの動作を無効にするように設定します。

于 2013-09-18T07:47:07.907 に答える
2

私はこの問題に遭遇しましたが、私の場合はテーブルビューに関連していませんでした。代わりに、ボタンでサブビューを使用していたため、userInteractionEnabledtrue であり、すべてのボタンのサブビューに対してその値を false (NO) に設定すると、私の問題。

于 2016-04-14T09:41:22.717 に答える
1

私は同じ問題を抱えており、どういうわけか自分の間違いを理解しました。withをwith of
に変更した後、セルのボタンが再びクリック可能になりました。constraintsself(UITableViewCell)constraintscontentViewself(UITableViewCell)

于 2017-10-18T09:43:01.570 に答える
1

Swift 4.2 をアップデートする

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) で -> UITableViewCell

それを追加します:cell.buttonCheckBox.isUserInteractionEnabled = false

または、User Interaction Enabled インスペクターのチェックを外します ここに画像の説明を入力

ボタンとセルを選択できるようになりました。これは私にとってはうまくいきます:)

于 2020-02-13T10:37:16.740 に答える
0

カスタム UITableViewCell を作成します。このリンクを参照してください。

displayButton という名前のがあるUIButtonとします。次のようなことができます。

の中- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathで、セルを作成した後、

[cell.dislayButton addTarget: self
                             action: @selector(replyButtonPressed:withEvent:)
                   forControlEvents: UIControlEventTouchUpInside];

そして、ViewController で次のようにメソッドを作成します。

- (void) replyButtonPressed: (id) sender withEvent: (UIEvent *) event
{
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: self.tableView];
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
}
于 2013-09-18T07:49:43.123 に答える