Ios UITableViewCell中未按下UIButton
我正在尝试在UITableViewCell中使用UIButton。出于某种原因,它不允许用户与按钮交互,只允许用户与UITableViewCell交互。我不知道代码是否有用,但这里是CellForRowatineXpath方法Ios UITableViewCell中未按下UIButton,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我正在尝试在UITableViewCell中使用UIButton。出于某种原因,它不允许用户与按钮交互,只允许用户与UITableViewCell交互。我不知道代码是否有用,但这里是CellForRowatineXpath方法 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifi
-(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如果cell.answerButton==nil,尝试添加NSLog@why回答按钮是否为零??;在该方法的顶部,查看是否打印NSLog。我想你的插座可能连接不正确。谢谢你的回答,但我刚刚尝试过,而且answerButton不是零。你的按钮的superview是什么?我正在开源动画库Canvas中使用的是CSAnimationView。你的Canvas superview是什么?谢谢你的回答,但它仍然不起作用你可以发送源代码让我看一下吗?@PoKoBros Capitals按钮和Funfacts按钮都对我有效。我说的是QuizViewController的按钮。你是在模拟器或设备上运行应用程序,因为没有为我按下按钮。