UITableViewCell 中未按下 UIButton

Posted

技术标签:

【中文标题】UITableViewCell 中未按下 UIButton【英文标题】:UIButton not being pressed in UITableViewCell 【发布时间】:2014-11-01 04:41:08 【问题描述】:

我想在 UITableViewCell 中有一个 UIButton。出于某种原因,它不允许用户与按钮交互,只与 UITableViewCell 交互。我不知道代码是否有帮助,但这里是 cellForRowAtIndexPath 方法。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *CellIdentifier = @"Answer";
    AnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell button
    cell.answerButton.buttonColor = [UIColor belizeHoleColor];
    cell.answerButton.shadowColor = [UIColor wetAsphaltColor];
    cell.answerButton.shadowHeight = 3.0f;

    //Set the tag and selector for the specific button
    cell.answerButton.tag = indexPath.row;
    [cell.answerButton addTarget:self action:@selector(submitAnswer:) forControlEvents:UIControlEventTouchDown];
    cell.answerButton.cornerRadius = 6.0f;
    [cell.answerButton setTitleColor:[UIColor cloudsColor] forState:UIControlStateNormal];
    [cell.answerButton setTitleColor:[UIColor cloudsColor] forState:UIControlStateHighlighted];

    cell.answerButton.tag = indexPath.row;

    tableView.separatorColor = [UIColor clearColor];

    cell.answerButton.layer.anchorPoint = CGPointMake(0.58f, 1.0f);

    //Cell Animation
    cell.answerButton.alpha = 0.0;
    CGRect newFrame = CGRectMake(self.view.frame.size.width, cell.frame.size.height, cell.frame.size.width, cell.frame.size.height);
     cell.answerButton.frame = newFrame;
     [UIView animateWithDuration:0.9
                      delay: 0.0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^
                     cell.answerButton.alpha = 1.0f;
                     CGRect newFrame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
                     cell.answerButton.frame = newFrame;
                 
                 completion: ^(BOOL finished) 
                 ];

    if (indexPath.row == self.correctAnswerIndex) 
        //Set up cell
        [cell configureFlatCellWithColor:[UIColor clearColor] selectedColor:[UIColor clearColor] roundingCorners:UIRectCornerAllCorners];

        cell.cornerRadius = 5.0f;
        cell.separatorHeight = 2.0f;

        //Set it so that if the string is too long there is a line break
        cell.answerButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        cell.answerButton.titleLabel.numberOfLines = 0;

        //Set the text alignment to center
        cell.answerButton.titleLabel.textAlignment = NSTextAlignmentCenter;

        //Set the text of the correct answer cell
        [cell.answerButton setTitle:capitalName forState:UIControlStateNormal];
    else
        //Set up cell
        [cell configureFlatCellWithColor:[UIColor clearColor] selectedColor:[UIColor clearColor] roundingCorners:UIRectCornerAllCorners];

        cell.cornerRadius = 5.0f;
        cell.separatorHeight = 2.0f;

        //Set it so that if the string is too long there is a line break
        cell.answerButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        cell.answerButton.titleLabel.numberOfLines = 0;

        //Set the text alignment to center
        cell.answerButton.titleLabel.textAlignment = NSTextAlignmentCenter;

        //Set the text of the incorrect answer cell
        if (self.threeIncorrectAnswers.count != 0) 
            //Generate a random number based on the indexes of the array
            int randomIndex = arc4random() % self.threeIncorrectAnswers.count;
            [cell.answerButton setTitle:[self.threeIncorrectAnswers objectAtIndex:randomIndex] forState:UIControlStateNormal];

            //Remove the incorrect answer
            [self.threeIncorrectAnswers removeObjectAtIndex:randomIndex];
        
    

    //Entering Animation
    cell.answerAnimationView.duration = 0.5;
    cell.answerAnimationView.delay    = 0.0;
    cell.answerAnimationView.type     = CSAnimationTypeFadeInLeft;

    // Kick start the animation immediately
    [cell startCanvasAnimation];

    tableView.userInteractionEnabled = YES;
    cell.userInteractionEnabled = YES;
    cell.answerAnimationView.userInteractionEnabled = YES;
    cell.answerButton.userInteractionEnabled = YES;

    return cell;
 

对这个问题有什么想法吗?我正在主情节提要中设置 UITableViewCell。如果您需要情节提要的屏幕截图,我很乐意发布。谢谢!

【问题讨论】:

尝试在该方法的顶部添加“if (cell.answerButton == nil) NSLog(@"why is answer button nil??");”并查看 NSLog 是否打印出来。我认为您的插座可能连接不正确。 感谢您的回答,但我刚试了下,answerButton 不是 nil。 你的按钮的超级视图是什么? 这是我与开源动画库 Canvas 一起使用的 CSAnimationView。 你的画布超级视图是什么? 【参考方案1】:

尝试将UIControlEventTouchDown 更改为UIControlEventTouchUpInside

【讨论】:

谢谢你的回答,但还是不行。 :( 你可以把源发过来让我看看吗? @PoKoBros capitalsButton 和 funFactsButton 都为我工作。 我说的是 QuizViewController 的按钮 您是在模拟器还是设备上运行应用程序,因为没有为我按下按钮。

以上是关于UITableViewCell 中未按下 UIButton的主要内容,如果未能解决你的问题,请参考以下文章

SVG 在 Firefox 中未按预期呈现,即

python中未按数字排序的元组列表

自动填充在 Excel 中未按预期工作

@State var 在 LazyVGrid 中未按预期更新

在 Postgres 13 中未按预期工作的表设置默认权限

PYQT5 拖放行在 TableWidget 中未按预期工作