带有自定义单元格和分隔符的UITableView.Alpha值在滚动时发生变化



我有一个带有Custom CellUITableView。一些自定义单元格正在加载图像。我正在为有图像的单元格添加一个自定义分隔符。滚动时出现问题。每当我滚动时,UIView opacity都会发生变化。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
}  
if (cell && (NSNull*) imageString != [NSNull null])
{
    UIView* separatorLineView; = [[UIView alloc] initWithFrame:CGRectMake(0, -1, 320, 5)];
    separatorLineView.backgroundColor = [UIColor blackColor];
    separatorLineView.alpha = 0.2;
    [cell.contentView addSubview:separatorLineView];
}  

给定视图的Alpha0.2,并且在滚动时它变得越来越厚。我该如何解决这个问题?

按照@rmaddy和@rdelmar的建议操作,但由于您无论如何都在使用自定义单元格,最快的方法imho是:

  1. 通过接口生成器直接将此行添加到单元格的contentView
    • @property (strong, nonatomic) IBOutlet UIView *vwLine;
      • 也是@synthesize vwLine;
    • 设置它的框架,背景颜色&α值(明显
    • 将其hidden属性设置为YES
  2. 进行imageString != [NSNull null]检查时
    • 如果为TRUE,则[cell.vwLine setHidden:NO];

示例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
                                                                 owner:self
                                                               options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }  
    if (imageString != [NSNull null]) {
        [cell.vwLine setHidden:NO];
    }
} 

我认为问题是,当你滚动时,你正在向已经有分隔符的单元格添加分隔符LineView,所以你看到的是其他行之上的行。您可以通过在行视图中添加一个标记来解决此问题,并检查单元格是否有带有该标记的子视图,如果有,则不要添加另一行。

在您的代码中可能存在单元的可重用性问题。所以尝试使用以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    . 
    .
    .
    .
    if(cell == nil)
    {
       NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
       cell = [topLevelObjects objectAtIndex:0];
      if(cellHasImage == YES)
      {
         //// just initialize UIView and add as addSubview of cell.contentView
         UIView* separatorLineView; = [[UIView alloc] init];
         separatorLineView.frame = CGRectMake(0, -1, 320, 5);
         separatorLineView.tag = 111; // set whatever you like
         [cell.contentView addSubview:separatorLineView];
      }
    }
    .
    .
    .
    /// get UIView base on it's tag
    if(cellHasImage == YES)
    { 
       UIView* separatorLineView; = (UIView *) [cell.contentView viewWithTag:111];
       separatorLineView.backgroundColor = [UIColor blackColor];
       separatorLineView.alpha = 0.2;
     }
    .
    .
    .
    return cell;
}

最新更新