【问题标题】:ImageView with Reusable Cell具有可重用单元格的 ImageView
【发布时间】:2013-12-19 14:46:25
【问题描述】:

我必须创建自定义UITableView,通常单元格有UIImageView,当UIImageView 的图像取决于我从服务器获得的内容时,还有两个UILabel。所以有些单元格有图像,有些单元格没有图像。

问题是,当我使用单元格的可重用性时,之前的 UIImageView's 图像仍然保持原样。我如何才能删除它并以正确的方式在单元格上实现我的内容。

以下是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *isImgOfTable =  [[self.listOfEvents objectAtIndex:indexPath.row] objectForKey:@"EventImage"];
    NSString *CellIdentifier = @"Cell";
    //NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d", indexPath.section, indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if([isImgOfTable length] > 0)
        {
            UIImageView *imgOfEvent = [[UIImageView alloc] init];
            imgOfEvent.tag = 101;
            [cell.contentView addSubview: imgOfEvent];
        }

        UILabel *lblEventTitle = [[UILabel alloc] init];
        lblEventTitle.tag = 102;
        [cell.contentView addSubview:lblEventTitle];

        UILabel *lblDescription = [[UILabel alloc] init];
        lblDescription.tag = 103;
        [cell.contentView addSubview:lblDescription];

    }

    if([isImgOfTable length] > 0)
    {
        NSString *newPath = @"";
        UIImageView *imgEvent = (UIImageView *) [cell.contentView viewWithTag:101];
        imgEvent.userInteractionEnabled = YES;
        newPath = [[GeneralClass getDocumentDirectory] stringByAppendingPathComponent:[[self.listOfEvents objectAtIndex:indexPath.row] objectForKey:@"EventImage"]];
        imgEvent.frame = CGRectMake(mainX, 4, 45, 45);
        imgEvent.backgroundColor = [UIColor colorWithRed:(187/255.f) green:(226/255.f) blue:(255/255.f) alpha:1.0f];
        imgEvent.layer.cornerRadius = 7;
        imgEvent.layer.borderColor = [UIColor colorWithRed:(224/255.f) green:(224/255.f) blue:(224/255.f) alpha:1.0f].CGColor;
        imgEvent.layer.borderWidth = 1;
        imgEvent.clipsToBounds = YES;
        imgEvent.image = [UIImage imageWithContentsOfFile:newPath];
    }

    UILabel *lblEventTitle = (UILabel*) [cell.contentView viewWithTag:102];
    .
    .
    // Code of UILabel *lblEventTitle

    UILabel *lblEventDescription = (UILabel*) [cell.contentView viewWithTag:103];
    .
    .
    // Code of UILabel *lblEventDescription

    return cell;
}

注意:我必须要使用单元格的可重用性。我不想修复这样的问题

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d", indexPath.section, indexPath.row]; 

而且我没有使用 UITableViewCell 类,所以可能无法调用/覆盖prepareForReuse 请给我你的建议。

【问题讨论】:

  • 只需发布您需要注意的代码。即UIImageview 仅相关操作。编辑将用于访问者的代码。
  • UILabel 相关代码对我来说非常完美,只有以前的图像不会被删除。
  • 我认为更简单的方法是在情节提要或 xib 中创建两个自定义单元格(一个带有图像视图,一个没有),然后根据您是否将正确的单元格出列有没有图片。
  • 我猜你的问题是UIImageView * imgEvent。它会显示在每一行上吗?
  • @Ranju Patel:见 Bhumeshwerkatre 的评论。他对您的代码(完整代码)感到困惑。这就是为什么我问你,放置你的代码不工作(你假设不工作),这足以让你的代码轻松查看。

标签: ios iphone objective-c uitableview


【解决方案1】:

检查此代码,它对您很有帮助。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *isImgOfTable =  [[self.listOfEvents objectAtIndex:indexPath.row] objectForKey:@"EventImage"];
        NSString *CellIdentifier = @"Cell";
        //NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d", indexPath.section, indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

              UIImageView *imgOfEvent = [[UIImageView alloc] init];
              imgOfEvent.tag = 101;
              [cell.contentView addSubview: imgOfEvent];

            UILabel *lblEventTitle = [[UILabel alloc] init];
            lblEventTitle.tag = 102;
            [cell.contentView addSubview:lblEventTitle];

            UILabel *lblDescription = [[UILabel alloc] init];
            lblDescription.tag = 103;
            [cell.contentView addSubview:lblDescription];

        }

        UIImageView *imgEvent = (UIImageView *) [cell.contentView viewWithTag:101];
        imgEvent.hidden=YES;
        if([isImgOfTable length] > 0)
        {
            NSString *newPath = @"";
            imgEvent.hidden=NO;
            imgEvent.userInteractionEnabled = YES;
            newPath = [[GeneralClass getDocumentDirectory] stringByAppendingPathComponent:[[self.listOfEvents objectAtIndex:indexPath.row] objectForKey:@"EventImage"]];
            imgEvent.frame = CGRectMake(mainX, 4, 45, 45);
            imgEvent.backgroundColor = [UIColor colorWithRed:(187/255.f) green:(226/255.f) blue:(255/255.f) alpha:1.0f];
            imgEvent.layer.cornerRadius = 7;
            imgEvent.layer.borderColor = [UIColor colorWithRed:(224/255.f) green:(224/255.f) blue:(224/255.f) alpha:1.0f].CGColor;
            imgEvent.layer.borderWidth = 1;
            imgEvent.clipsToBounds = YES;
            imgEvent.image = [UIImage imageWithContentsOfFile:newPath];
        }

        UILabel *lblEventTitle = (UILabel*) [cell.contentView viewWithTag:102];
        .
        .
        // Code of UILabel *lblEventTitle

        UILabel *lblEventDescription = (UILabel*) [cell.contentView viewWithTag:103];
        .
        .
        // Code of UILabel *lblEventDescription

        return cell;
    }

【讨论】:

    【解决方案2】:

    我可以建议继承 UITableViewCell 并使用prepareForReuse

    -(void)prepareForReuse
    {
        labelOne = @"Default text 1";
        labelTwo = @"Default label 2";
        [imageView setImage:nil];      //Or you could set a default image?
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      相关资源
      最近更新 更多