簡體   English   中英

UITableViewCell中未按下UIButton

[英]UIButton not being pressed in UITableViewCell

我試圖在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,如果您需要故事板的屏幕截圖,我很樂意發布一個。 謝謝!

嘗試將UIControlEventTouchDown更改為UIControlEventTouchUpInside

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM