-3

我有一个UITableView称为症状表......并且当在可补救表UISwitch中打开时有可补救我在症状表视图单元上显示一个UIImage以突出显示UISwitch该单元格的打开。当我在第一个症状的可补救中打开时,我正在显示UISwitch但单元格正在被重复使用,并且图像也显示在其他症状表视图单元格中。谁能帮我解决这个问题?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    if (tableView==symptomsTableView)
    {
        PPsymptomTableCell *cell;

        static NSString *cellIdentifier=@"cellIdentifier";
        cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell==nil)
        {
            cell=[[PPsymptomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        int symptomIDSelected;

        symptomIDSelected = [[[mainsymptArray objectAtIndex:indexPath.row]objectForKey:@"SymptID"]intValue];

        NSLog(@"%d",[[activeNotificationDictionary objectForKey:[NSNumber numberWithInt:symptomIDSelected]]intValue]);

        for (int i = 0; i<activeNotificationDictionary.count; i++)
        {
            if([[activeNotificationDictionary objectForKey:[NSNumber numberWithInt:symptomIDSelected]]intValue] == symptomIDSelected)
            {
                cell.selectedCellImageDisplay.hidden=NO;
            }
            else
            {
                cell.selectedCellImageDisplay.hidden=YES;
            }
        }

        NSLog(@"end of symptoms cell for row");
        if (searching==YES)
        {
            cell.symptomCellLabel.text=[[searchSymptomsArray objectAtIndex:indexPath.row] objectForKey:@"SymptName"];
            cell.backgroundColor=[UIColor clearColor];
            return cell;
        }
        else
        {
            cell.backgroundColor=[UIColor clearColor];
            NSArray *sectionArray=[mainIndexDictionary objectForKey:[allKeysArray objectAtIndex:indexPath.section]];
            cell.symptomCellLabel.text=[[sectionArray objectAtIndex:indexPath.row] objectForKey:@"SymptIndexName"];
            return cell;
        }
    }

这里的 activenotification 字典是一个NSMutableDictionary包含特定症状 ID 的补救 ID 值的地方。这是症状表的UITableViewCell习惯

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill;

        selectedCellImageDisplay = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"selectedSymptomImage.png"]];
        selectedCellImageDisplay.frame = CGRectMake(230.0, 8.0, 30.0, 30.0);
        selectedCellImageDisplay.hidden=YES;

        symptomCellLabel=[[UILabel alloc]initWithFrame:CGRectMake(15.0,0.0 ,280.0,40.0)];
        symptomCellLabel.font=[UIFont fontWithName:@"Rockwell" size:17];
        symptomCellLabel.textColor=[UIColor blackColor];
        symptomCellLabel.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:symptomCellLabel];
        [self.contentView addSubview:selectedCellImageDisplay];
             // Initialization code
    }
    return self;
}
4

2 回答 2

7

正在重用单元格,因为您要求重用它 ( dequeueReusableCellWithIdentifier:)。因为单元格可以重复使用,所以必须显式设置每个单元格的所有特性,包括开关的存在与否及其状态。

于 2013-02-15T18:03:50.663 回答
0

你把它UISwitch放进牢房的什么地方?每次出列或初始化单元格后,您都需要确保设置开关的状态。

于 2013-02-15T18:03:39.477 回答