2013-02-15 30 views
-2

我有一個UITableView稱爲symptomtable ......並有remedytable當UISwitch是在remedytable我對symptomtableviewcell顯示UIImage以突出UISwitch爲ON該小區。當我切換ON在remedytable第一症狀的我顯示UISwitch但所述細胞得到再利用,並且圖像被在其他symptomtableview細胞表示爲好。任何人都可以幫我解決這個問題嗎?UITableViewCell中得到重用

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


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

     static NSString *[email protected]"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其含有remedyID的該特定symptomID的值。 這是symptomtable

- (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; 
} 

回答

0

你在哪裏把UISwitch進入細胞的自定義UITableViewCell?每次出列或初始化單元后,都需要確保設置交換機的狀態。

+0

我在上customcell – 2013-02-16 15:55:24

+0

稱爲remedyTableView另一個tableview中你使用兩個UITableViews您的應用程序的同一部分添加UISwitch? – bdesham 2013-02-16 17:47:34

+0

是的,我有兩個UITableviews ......當我在一個點擊的UITableView它導航到另一個UITableView的 – 2013-02-23 16:20:36

7

細胞被重用,因爲你要求它被再利用(dequeueReusableCellWithIdentifier:)。由於電池可重複使用,則必須明確設置小區的所有的功能,其中包括開關和狀態的存在與否。

+0

我該怎麼做呢? – 2013-02-17 04:06:21

+0

u能幫助我 – 2013-02-17 15:55:29

+1

我寫你如何iOS的方案,其中包括一個約章一整本書的問題是如何表視圖的工作:http://www.apeth.com/iOSBook/ch21.html編寫_your_代碼因爲你的特定情況是_you_。 – matt 2013-02-17 17:16:28