UITableViewCell被重用

时间:2013-02-15 18:00:03

标签: iphone ios objective-c uitableview

我有一个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的remedyID值。 这是症状

的自定义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;
}

2 个答案:

答案 0 :(得分:7)

正在重复使用该单元格,因为您要求重用该单元格(dequeueReusableCellWithIdentifier:)。由于可以重复使用该单元,因此必须明确设置所有 每个单元的功能,包括交换机的存在与否及其状态。

答案 1 :(得分:0)

您将UISwitch放入单元格的哪个位置?每次出列或初始化单元格后,都需要确保设置开关的状态。