2011-11-04 70 views
1

我知道,如果我有一些图片和子视图中自定义单元格添加的话,我不得不重复使用的细胞,使自定义控件将不会在其他细胞出现,但在这里我有其他问题。我只想对第一部分的第一个单元格的ImageView所以我用IndexPath.Section == 0和IndexPath.Row == 0的条件在下面的代码,但问题是,当我滚动表格,其他单元将满足这一条件,我的代码也会在该单元上创建imageview。我试过标记它,并使用相同的标记cellView,但它也没有帮助。单元问题是禁用少数单元的用户交互。最终在滚动后禁用所有单元的用户交互。无论如何要解决这个问题吗?UITableView的滚动和重绘问题

谢谢。

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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
} 

if(indexPath.section == 0 && indexPath.row == 0) { 
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease]; 
    UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease]; 
    [imageView setFrame: CGRectMake(10, 10, 54, 54)]; 
    [cellView addSubview:imageView]; 
    cell.backgroundView = cellView; 

    return cell; 
} else if(indexPath.row == 0) { 
    NSString * title = [NSString string]; 
    switch (indexPath.section) { 
     case 1: 
      title = @"Friends"; 
      break; 
     case 2: 
      title = @"Accounts"; 
      break; 
     case 3: 
      title = @"Stats"; 
      break; 
     default: 
      title = nil; 
      break; 
    } 
    cell.textLabel.text = title; 
    cell.userInteractionEnabled = NO; 
    return cell; 
} 

cell.textLabel.text = @"Test"; 
return cell; 
} 

【解析】正确的代码:

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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 

if(indexPath.section == 0 && indexPath.row == 0) { 
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease]; 
    cell.imageView.image = imageView.image; 
    cell.textLabel.text = nil; 
    cell.textLabel.textColor = [UIColor clearColor]; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.userInteractionEnabled = YES; 
    return cell; 
} else if(indexPath.row == 0) { 
    NSString * title = [NSString string]; 
    switch (indexPath.section) { 
     case 1: 
      title = @"Friends"; 
      break; 
     case 2: 
      title = @"Accounts"; 
      break; 
     case 3: 
      title = @"Stats"; 
      break; 
     default: 
      title = nil; 
      break; 
    } 

    cell.imageView.image = nil; 
    cell.textLabel.text = title; 
    cell.textLabel.textColor = [UIColor redColor]; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.userInteractionEnabled = NO; 

    return cell; 
} 


cell.imageView.image = nil; 
cell.textLabel.text = [cellItems objectAtIndex:(rows+indexPath.row-1)]; 
cell.textLabel.textColor = [UIColor blueColor]; 
cell.textLabel.backgroundColor = [UIColor clearColor]; 
cell.userInteractionEnabled = YES; 
return cell; 
} 

[改进的代码]

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

static NSString *NormalCellIdentifier = @"NormalCell"; 
static NSString *TitleCellIdentifier = @"TitleCell"; 
NSString *neededCellType; 

if(indexPath.section == 0 && indexPath.row == 0) { 
    neededCellType = TitleCellIdentifier; 
} else { 
    neededCellType = NormalCellIdentifier; 
} 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType]; 

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease]; 

    //Only add content to cell if it is new 
    if([neededCellType isEqualToString: TitleCellIdentifier]) { 
     UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease]; 
     cell.imageView.image = imageView.image; 
    } 
} 

if([neededCellType isEqualToString: NormalCellIdentifier]) { 
    NSString * title; 
    if(indexPath.row == 0) { 
     switch (indexPath.section) { 
      case 1: 
       title = @"Friends"; 
       break; 
      case 2: 
       title = @"Accounts"; 
       break; 
      case 3: 
       title = @"Stats"; 
       break; 
      default: 
       title = nil; 
       break; 
     } 
     cell.textLabel.text = title; 
     cell.textLabel.textColor = [UIColor redColor]; 
     cell.userInteractionEnabled = NO; 
    } else { 

     cell.userInteractionEnabled = YES; 
     cell.textLabel.textColor = [UIColor blueColor]; 
     cell.textLabel.text = @"Test"; 
    } 
} 

return cell; 
} 

回答

0

的问题是,你是不是允许在于用正确显示图像的单元被后来重用和图像视图还是在那里的可能性。

这里有两种解决方案:

  1. 设置图像视图的标签值,当你创建它,那么当你设置的细胞,包括代码来检查,如果需要删除旧的ImageView的。

  2. 分配不同的重用标识符到需要的图像视图和那些不细胞。然后确保你只是在创建单元格时添加一个新的图像视图,而不是在它们被重用时。

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

static NSString *NormalCellIdentifier = @"NormalCell"; static NSString *TitleCellIdentifier = @"TitleCell"; NSString *neededCellType;

if(indexPath.section == 0 && indexPath.row == 0) { neededCellType = TitleCellIdentifier; } else { neededCellType = NormalCellIdentifier; }

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];

if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];

//Only add content to cell if it is new if([neededCellType isEqualToString: TitleCellIdentifier]) { UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease]; UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease]; [imageView setFrame: CGRectMake(10, 10, 54, 54)]; [cellView addSubview:imageView]; cell.backgroundView = cellView; } }

if([neededCellType isEqualToString: NormalCellIdentifier]) { NSString * title; if(indexPath.row == 0) {

switch (indexPath.section) { 
    case 1: 
     title = @"Friends"; 
     break; 
    case 2: 
     title = @"Accounts"; 
     break; 
    case 3: 
     title = @"Stats"; 
     break; 
    default: 
     title = nil; 
     break; 
} 
cell.textLabel.text = title; 
cell.userInteractionEnabled = NO; 

}

else { cell.textLabel.text = @"Test"; return cell; } } }

(那些最后几行跌出码框)。这应该做到这一点。

+0

嗨Dancreek,我试过使用标签,但它没有奏效!刚刚试过用(IndexPath.Section = 0和IndexPath.Row = 0)为单元格使用不同的标识符,但那也不起作用。 – applefreak

+0

的问题是,当我滚动,如UITableView的使用延迟加载,它重新分配的部分= 0和行= 0当前可见的细胞,并当细胞被调用上的cellForRowAtIndexPath,它创建的图像!我只需要在第一节的第一个单元格中显示图像。对于其余部分,第一个单元格将禁用用户交互。由于每当我滚动时单元格的索引被重置,这个问题不会被解决,除非我从代码中删除(indexPath.section == 0 && indexPath.row == 0)条件并使用其他内容! – applefreak

+1

单元索引不变。否则,他们将是无用的。问题是你的第一个单元正在被重用。 – Dancreek

1

我觉得你的问题是,电池的再利用使得它,使得AREN细胞”由于新单元格有一些必须重新定义的属性集,所以不会创建它。例如,尝试将cell.userInteractionEnabled = YES分配给所有其他情况,并查看结果是什么。

+0

完美Stavash!非常感谢你。发布正确的代码。 – applefreak