I have two custom cells in my table view that I can display correctly. MyCustomCellB
has a button and when the user taps this button I call a segue programatically, that also works, but imports wrong variables. It takes different data which "belongs" to a MyCustomCellA
typed cell. But, if I first select the cell and then tap the button it works fine, somehow after the selection it gets the right data.
Is my cellForRowAtIndexPath:
is incorrect?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([object[@"num"] isEqualToString:@"one"]) {
MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//... cell display
cell.acceptLabel.tag = indexPath.row;
[cell.acceptLabel addTarget:self action:@selector(didTapBttn:) forControlEvents:UIControlEventTouchUpInside];
return cell;
} else {
MyCustomTableViewCellB *cellMessage = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
//... cell display
return cellMessage;
}
}
- (void)didTapBttn:(id)sender {
[self performSegueWithIdentifier:@"info" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"info"]) {
if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DViewController *datap = [segue destinationViewController];
PFObject *passObj = [self.objects objectAtIndex:indexPath.row];
datap.infoName = passObj[@"infoName"];
datap.infoId = passObj[@"id"];
}
if ([[segue identifier] isEqualToString:@"desc"]) {
if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DViewController *datap = [segue destinationViewController];
PFObject *passObj = [self.objects objectAtIndex:indexPath.row];
datap.infoN = passObj[@"descName"];
datap.infoId = passObj[@"descId"];
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…