2012-09-23 135 views
0

我的UITableView細胞從其他細胞獲取內容(圖像)。我不知道爲什麼會發生,但我相信我的錯誤是愚蠢的。在下面的圖片中,您可以看到它的外觀。頂部的單元格從底部的單元格(Facebook和Twitter圖標)獲取圖像。UITableView細胞從其他細胞獲取內容

這裏是我的的cellForRowAtIndexPath代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSDictionary *dictionary = [mainArray objectAtIndex:indexPath.section]; 
    NSArray *array1 = [dictionary objectForKey:@"stuff"]; 

    NSString *cellValue = [array1 objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; // загаловок клетки 
    cell.textLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16]; 
    cell.textLabel.textColor = [UIColor colorWithRed:145/256.0 green:0/256.0 blue:0/256.0 alpha:1.0];  
    cell.backgroundColor = [UIColor whiteColor]; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.textLabel.textAlignment = UITextAlignmentLeft; 

    if (indexPath.section == 0 && indexPath.row == 1) 
    { 
     if([[NSUserDefaults standardUserDefaults]boolForKey:@"password"] == YES) 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 
    } 

    switch(indexPath.section) 
    { 

     case 2: 
     { 
      cell.textLabel.textAlignment = UITextAlignmentCenter; 
     } 
     break; 

     case 3: 
     { 
      if (indexPath.row == 0) 
       cell.imageView.image = [UIImage imageNamed:@"facebook"]; 
      else if (indexPath.row == 1) 
       cell.imageView.image = [UIImage imageNamed:@"twitter"]; 
      else if (indexPath.row == 2) 
       cell.imageView.image = [UIImage imageNamed:@"youtube"]; 
      else 
       cell.imageView.image = [UIImage imageNamed:@"vk"]; 
     } 
     break; 
      default: 
      break; 
    } 




    return cell; 
} 

enter image description here

我怎樣才能解決呢?

+0

我看不到在這部分代碼中的錯誤,但我會提出一種方法來追蹤它。爲值'1'和'0'(沒有圖像的情況)添加一個'case',並將'cell.imageView.image'設置爲'nil'。在這種情況下,我們會知道單元格是從這個代碼還是從其他地方獲取圖像。 – antf

回答

1

單元格被重複使用,所以您必須將單元格的imageView設置爲零,只要不希望圖像出現即可。試試這個:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSDictionary *dictionary = [mainArray objectAtIndex:indexPath.section]; 
    NSArray *array1 = [dictionary objectForKey:@"stuff"]; 

    NSString *cellValue = [array1 objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; // загаловок клетки 
    cell.textLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16]; 
    cell.textLabel.textColor = [UIColor colorWithRed:145/256.0 green:0/256.0 blue:0/256.0 alpha:1.0];  
    cell.backgroundColor = [UIColor whiteColor]; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.textLabel.textAlignment = UITextAlignmentLeft; 
    cell.imageView.image = nil; 

    if (indexPath.section == 0 && indexPath.row == 1) 
    { 
     if([[NSUserDefaults standardUserDefaults]boolForKey:@"password"] == YES) 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 
    } 

    switch(indexPath.section) 
    { 

     case 2: 
     { 
      cell.textLabel.textAlignment = UITextAlignmentCenter; 
     } 
     break; 

     case 3: 
     { 
      if (indexPath.row == 0) 
       cell.imageView.image = [UIImage imageNamed:@"facebook"]; 
      else if (indexPath.row == 1) 
       cell.imageView.image = [UIImage imageNamed:@"twitter"]; 
      else if (indexPath.row == 2) 
       cell.imageView.image = [UIImage imageNamed:@"youtube"]; 
      else 
       cell.imageView.image = [UIImage imageNamed:@"vk"]; 
     } 
     break; 
     default: 
     break; 
    } 




    return cell; 
}