我通过情节提要设计了 tableView,在一个单元格中我有一个按钮和一个标签。按钮在 storyboard.in cellForRowAtIndexPath 上有标签 1 和标签有标签 2 我正在访问这些,如下所示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@" i am back in cellForRowAtIndexPath");
static NSString *CellIdentifier = nil;
// UILabel *topicLabel;
NSInteger rows=indexPath.row;
NSLog(@"row num=%i",rows);
if (rows % 2==0)
{
CellIdentifier=@"LeftTopicCell";
}
else
{
CellIdentifier=@"RightTopicCell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *topicButton=(UIButton *)[cell viewWithTag:1];
UILabel *topicScoreLabel=(UILabel *)[cell viewWithTag:2];
NSString *topicNameLabel=[[NSString alloc]init];
//some logic processing
topicNameLabel= topicItem.topicTitle;
[topicButton setTitle:topicNameLabel forState:UIControlStateNormal];
[topicButton setTag:indexPath.row ];
[topicButton setTitle:topicNameLabel forState:UIControlStateHighlighted];
[topicButton addTarget:self action:@selector(topicButtonSelected:) forControlEvents:UIControlEventTouchDown];
[topicScoreLabel setText:[NSString stringWithFormat:@"%d/%d",correctAnswers,[answerResults count]]];
}
return cell;
}
第一次它工作得很好,但是当我回到这个视图控制器时,我又重新加载了这个,但这次它工作了两行。但是对于第三行,它为此行返回 UIButton 而不是 UILabel ,UILabel *topicScoreLabel=(UILabel *)[cell viewWithTag:2];
因此它给出异常“无法识别的选择器“[UIButton setText]””;