繁体   English   中英

带有可重复使用单元的ImageView

[英]ImageView with Reusable Cell

我必须创建自定义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请给我你的建议。

检查此代码对您有所帮助。

 - (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并使用prepareForReuse

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM