salve a tutti,
ho cercato di creare un checkbox tramite UITableView e UIButton,
ma succede una cosa anomala che non riesco a risolvere.
Di seguto posto il codice di come creo la mia tabella:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSUInteger row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.imageView.image = [UIImage imageNamed:@"checkbox.png"];
//pulsante per checkbox
UIButton *checkButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
checkButton.frame = CGRectMake(10.0f, 12.0f, 18.0f, 18.0f);
checkButton.backgroundColor = [UIColor clearColor];
checkButton.tag = kCheckTag;
[checkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//immagine checkbox non selezionato
UIImage *buttonImageNormal = [UIImage imageNamed:@"checkbox.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[checkButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
//immagine checkbox selezionato
UIImage *buttonImageChecked = [UIImage imageNamed:@"checkbox-checked.png"];
UIImage *strechableButtonImageChecked = [buttonImageChecked stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[checkButton setBackgroundImage:strechableButtonImageChecked forState:UIControlStateSelected];
[checkButton addTarget:self action:@selector(check:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:checkButton];
}
// Set up the cell...
cell.textLabel.text = [list objectAtIndex:row];
cell.textLabel.textAlignment;
return cell;
}
-(void)check:(id)sender{
UIButton* checkButton = (UIButton*)sender;
if(checkButton.selected){
checkButton.selected=NO;
}else{
checkButton.selected = YES;
}
}
Allora, la tabella me la costruisce bene e in ogni cella viene inserito un bottone. L’anomalia succede quando la tabella è formata da più di 12 elementi. Selezionando il bottone della i-esima viene chiamato correttamente il metodo check associato. Quando clicco su un bottone cambia l’immagine associata come in un checkbox. La cosa strana è che cliccando sul pulsante, per esempio, della prima riga cambia immagine anche quella della 12, 24, 36 ecc.
Qualcuno mi può aiutare a risolvere questo problema?
Grazie