我正在努力解决以下问题。
我有一张船的选项表视图。每个选项(单元格)都有一个名称,一个价格和一个按钮,用于指示是否已选择该选项(参见附件截图) -
问题在于,当我点击第1节中的几个选项时,我也得到了最后一节中单元格的button_pressed状态。可能这是因为细胞被重复使用了吗?
这是在点击单元格时调用的函数,用于添加选项。
- (IBAction)cellButtonSelection:(id)sender {
float prezzoParziale;
UIButton * button = sender;
//NSLog(@"%@",button.superview.superview);
if ([button.superview.superview isKindOfClass:[CellOptions class]] ) {
CellOptions * cellOpt =(CellOptions *)button.superview.superview;
//BOOL selectionState = NO;
cellOpt.selected =! cellOpt.selected;
if(cellOpt.selected){
//UIButton *button = cellOpt.button1;
[button setImage:[UIImage imageNamed:@"checkedButton.png"] forState:UIControlStateNormal];
prezzoParziale = [cellOpt.label2.text floatValue];
[accessoriSelezionati addObject:cellOpt.label1.text];
[accessoriSelezionatiPrezzi addObject: cellOpt.label2.text];
NSLog(@"stampa: %@", accessoriSelezionati);
}else{
//UIButton *button = cellOpt.button1;
[button setImage:[UIImage imageNamed:@"uncheckedButton.png"] forState:UIControlStateNormal];
prezzoParziale = [cellOpt.label2.text floatValue] * -1;
[accessoriSelezionati removeObject:cellOpt.label1.text];
[accessoriSelezionatiPrezzi removeObject: cellOpt.label2.text];
}
[self updateTotal:prezzoParziale]; }
和.....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CellOptions * cellOpt;
cellOpt = [tableView dequeueReusableCellWithIdentifier:CellOption forIndexPath:indexPath];
cellOpt.label1.text = [[[accessoriXtipologia objectForKey:tipologia] objectAtIndex:indexPath.row] valueForKey:@"nome"];
cellOpt.label2.text = [[[[accessoriXtipologia objectForKey:tipologia] objectAtIndex:indexPath.row] valueForKey:@"prezzo"] stringValue];
return cellOpt;
}
编辑:现在它正在运作
在cellForRowAtIndexPath中添加:
cellOpt.selected = [accessoriSelezionati containsObject:cellOpt.label1.text];
if (cellOpt.selected) {
[cellOpt.button1 setImage:[UIImage imageNamed:@"checkedButton.png"] forState:UIControlStateNormal];
}
else {
[cellOpt.button1 setImage:[UIImage imageNamed:@"uncheckedButton.png"] forState:UIControlStateNormal];
}
return cellOpt;
答案 0 :(得分:2)
是的,这是由于细胞重用。在cellForRowAtIndexPath
中,除了设置标签之外,您还必须通过执行以下操作来设置所选状态:
cellOpt.selected = [accessoriSelezionati containsObject:cellOpt.label1.text];