I have designed tableView through the storyboard, in one cell i have one button & one Label.
button is has tag-1 & Label has tag-2 on the storyboard.in cellForRowAtIndexPath i am accessing these like below
- (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;
}
first time it works fine but when i came back to this viewcontroller, i have reloaded this again, but this time for two rows it works well. but for third rows it returns UIButton instead of UILabel for this line UILabel *topicScoreLabel=(UILabel *)[cell viewWithTag:2]; & because of this it is giving exception “unrecognised selector “[UIButton setText]””;
i got answer ,because i am updating button tag through the setTag method in cellForRowAtIndexPath according to row number, it is setting tag to button as number 2 thats why it returning UIButton