Ios 具有可重用单元的ImageView

Ios 具有可重用单元的ImageView,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,我必须创建自定义的UITableView,当UIImageView的图像取决于我从服务器获取的内容时,通常cell有UIImageView,还有两个UILabel。所以有些细胞有图像,有些没有图像 问题是,当我使用单元格的可重用性时,上一个UIImageView的图像保持不变。。我如何删除它并以正确的方式在cell上实现我的内容 下面是我的代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPat

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

问题是,当我使用单元格的可重用性时,上一个
UIImageView的
图像保持不变。。我如何删除它并以正确的方式在cell上实现我的内容

下面是我的代码

- (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类,因此可能无法调用/重写
请给我您的建议。

检查此代码,它对您完全有帮助

 - (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;
    }

我是否可以建议将UITableViewCell子类化并使用
prepareforeuse

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

只需发布需要注意的代码。ie
UIImageview
仅限相关操作。编辑将用于vistiors.UILabel相关代码的代码对我来说非常适合只有上一张图像不会被删除。我认为更简单的方法是在故事板或xib中创建两个自定义单元格(一个带有图像视图,另一个没有图像视图),并根据您是否有图像将正确的图像排在队列中。我猜您的问题是
UIImageView*imgEvent
。它会显示在每一行吗?@Ranju Patel:见Bhumeshwerkatre的评论。他混淆了你的代码(完整代码)。这就是为什么我问你,把你的代码放在不起作用的地方(你假设它不起作用),这样就可以很容易地在你的代码中找到它。