2014-11-01 4 views
1

Я пытаюсь использовать UIButton в UITableViewCell. По какой-то причине это не позволяет пользователю взаимодействовать с кнопкой, только с UITableViewCell. Я не знаю, поможет ли код, но вот метод cellForRowAtIndexPath.UIButton не нажата в UITableViewCell

-(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 в Main Storyboard. Если вам нужен скриншот раскадровки, я был бы рад опубликовать его. Спасибо!

+0

попробуйте добавить в «' если (cell.answerButton == ноль) NSLog (@»почему кнопка ответа ноль ??«); '» в верхней части этого метода и проверьте, распечатывается ли NSLog. Я думаю, что ваш выход может быть неправильно подключен. –

+0

Спасибо за ответ, но я просто попробовал, и ответБатон не ноль. – PoKoBros

+0

какая у вас кнопка? –

ответ

0

Попробуйте изменить UIControlEventTouchDown к UIControlEventTouchUpInside

+0

Спасибо за ответ, но он по-прежнему не работает. :( – PoKoBros

+0

Возможно ли отправить вас через источник, чтобы я мог посмотреть? – Sreejith

+0

https://github.com/pokobros/Do-You-Know-Your-Countries- – PoKoBros

Смежные вопросы