HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 带可重复使用单元的ImageView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须创建自定义UITableView,并且通常单元格具有UI ImageView,当UI ImageView的图像依赖于我从服务器获得的内容时,两个UILabel.所以有些单元格有图像,有些没有图像.

问题是,当我使用单元格的可重用性时,之前的UIImageView的图像保持原样..我如何@L_801_4@它并在单元格上正确实现我的内容.

以下是我的代码.

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

大佬总结

以上是大佬教程为你收集整理的ios – 带可重复使用单元的ImageView全部内容,希望文章能够帮你解决ios – 带可重复使用单元的ImageView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。