在UITableViewCell中没有按下UIButton

时间:2014-11-01 04:41:09

标签: ios objective-c uitableview uibutton

我正在尝试在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。如果您需要故事板的屏幕截图,我很乐意发布一个。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试将UIControlEventTouchDown更改为UIControlEventTouchUpInside