Okay I know I'm running up against my limits of understanding as regards objective-c, cocoa, xcode, and blah blah and so on. But here's what I'm trying to do:
I have a tableview in a viewcontroller. The tableview's delegate is the viewcontroller. Viewcontroller has an outlet to the tableview. The table is put together using custom cells (with IB xib) and data from an xml file. In the custom cell there are two buttons - and when the cell is created the button action is added as an addTarget to self (the viewcontroller) which then goes to an action. The viewcontroller button action method gets the row of the button pressed in the table and then changes the cell's text and the button's title.
But of course when I scroll that particular cell out of view and back into view it's been reset to the beginning state. I assume this has to do with cell dequeueing etc. Any ideas?
here's the relevant bits and code:
custom cell: has outlets to buttons and textlabel.
cell create code in the datasource cellForRowAtIndexPath... {
static NSString *dialogueCellIdentifier = @"dialogueCellIdentifier";
dialogue_cell *cell = (dialogue_cell *)[tableView dequeueReusableCellWithIdentifier:dialogueCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"dialogue_cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
[[cell lButton] addTarget:self action:@selector(lButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
[[cell lButton] setTag:[indexPath row]];
NSString *row = [NSString stringWithFormat:@"%i",[indexPath row]];
NSString *en = [[self.dataArray objectForKey:row] valueForKey:@"en"];
cell.mainText.text = en;
return cell;
}
and the lButton method...
NSIndexPath *thisCellPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
dialogue_cell *thisCell = (dialogue_cell *)[self.dialogueTable cellForRowAtIndexPath:thisCellPath];
NSString *row = [NSString stringWithFormat:@"%i",[sender tag]];
if ([thisCell.languageButton.currentTitle isEqualToString:@"en"]) {
[thisCell.languageButton setTitle:@"zc" forState:UIControlStateNormal];
thisCell.mainText.text = [[self.lineArray objectForKey:row] valueForKey:@"lineText_zc"];
} else {
[thisCell.languageButton setTitle:@"en" forState:UIControlStateNormal];
thisCell.mainText.text = [[self.lineArray objectForKey:row] valueForKey:@"lineText_en"];
}
so two questions:
1. is there a way to make the cell retain what is in the label and button name even though it's scrolled offscreen?
2. is there a way to set the label and button name within the cell custom class instead of sending the button action to the viewcontroller that the table is in?
Thanks!