带有可重复使用单元的ImageView

时间:2013-12-19 05:46:59

标签: ios iphone objective-c uitableview

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

问题在于,当我使用单元格的可重用性时,之前的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 请给我你的建议。

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:0)

我建议继承UITableViewCell并使用prepareForReuse

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