2012-09-23 139 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; 
}