ストーリーボードを介してtableViewを設計しました。1つのセルに1つのボタンと1つのラベルがあります。ボタンにはタグ1があり、ラベルにはストーリーボードにタグ2があります。cellForRowAtIndexPathで私は以下のようにこれらにアクセスしています
- (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;
}
初めては正常に動作しますが、このビューコントローラに戻ったときに、これを再度リロードしましたが、今回は2行で正常に動作します。ただし、3行目では、この行のUILabelではなくUIButtonが返されます。このUILabel *topicScoreLabel=(UILabel *)[cell viewWithTag:2];
ため、「認識されないセレクター"[UIButtonsetText]"」という例外が発生します。