ios – 带可重复使用单元的ImageView

ios – 带可重复使用单元的ImageView,第1张

概述我必须创建自定义UITableView,并且通常单元格具有UI ImageView,当UI ImageView的图像依赖于我从服务器获得的内容时,两个UILabel.所以有些单元格有图像,有些没有图像. 问题是,当我使用单元格的可重用性时,之前的UIImageView的图像保持原样..我如何删除它并在单元格上正确实现我的内容. 以下是我的代码. - (UITableViewCell *)table 我必须创建自定义UItableVIEw,并且通常单元格具有UI ImageVIEw,当UI ImageVIEw的图像依赖于我从服务器获得的内容时,两个UILabel.所以有些单元格有图像,有些没有图像.

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

以下是我的代码.

- (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所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1022151.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存