2014-11-21 74 views
0

我正在处理UITableView谁显示每个价格范围值条目。 我使用循环来显示价格范围。我在哪里寻找我的灵魂,因为当我上下滚动时,每个已经显示,隐藏并重新显示的细胞都叠加了“美元”的图像,而且它很丑,看到图片。Objective-c UITableView单元格图像叠加

enter image description here

我如何设置初始化我的手机:

PoiAllTableCell *cell = (PoiAllTableCell *)[tableView dequeueReusableCellWithIdentifier:@"PoiAllCell" forIndexPath:indexPath]; 

我使用此代码显示美元的ImageView:

for (int i =0; i < 4;) { 
     PriceRangeImageView *img = [PriceRangeImageView new]; 


     if (i < _currentPoi.priceRange) { 
      [img initWithXMultiple:i withColor:_appDelegate.mainColor]; 
     } 
     else{ 
      [img initWithXMultiple:i withColor:_appDelegate.blackColor]; 
     } 

     [cell.priceRangeView addSubview:img]; 
     i++; 
    } 

我如何显示图像:

#import "PriceRangeImageView.h" 

@implementation PriceRangeImageView 


- (void)initWithXMultiple:(int)xMultiple withColor:(UIColor *)color{ 
    if (self.image == nil) { 

     self.frame = CGRectMake(xMultiple*16,0,16,16); 
     self.image = [UIImage imageNamed:@"dollar_icon"]; 
     self.image = [self.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 

     self.tintColor = color; 
    } 
} 

@end 

这里是我的手机:

#import <UIKit/UIKit.h> 
#import "PriceRangeView.h" 

@interface PoiAllTableCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UILabel *poiTitleLabel; 
@property (weak,nonatomic) IBOutlet UIImageView *poiImageView; 
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel; 
@property (weak, nonatomic) IBOutlet UILabel *kmStaticLabel; 

@property (weak, nonatomic) IBOutlet UILabel *separatorLabel; 
@property (weak, nonatomic) IBOutlet UILabel *categoriesLabel; 

@property (weak, nonatomic) IBOutlet UIView *priceRangeView; 


@property (nonatomic) CGSize distanceLabelSize; 
@property (nonatomic) CGFloat distanceLabelWidth; 

@end 

我在想比在每个滚动细胞的细胞兑美元imageview的,我想知道如何避免这种情况。如果有人可以建议我一个更好的方法?

+0

试试这个@Barzull [cell.contentView addSubview:IMG] http://stackoverflow.com/a/26881504/3767017 – 2014-11-21 09:26:23

+0

感谢,但cell.priceRangeView是一个UIView,我试图用你的建议,但它再次叠加。 – Jeffrey 2014-11-21 09:37:20

+0

只是一个建议,当你制作自定义单元格并为其制作UITableViewCell子类时,为什么你要为其单元格数据创建另一个类,则应该将所有数据填充到同一个子类中 (因为我明白看到您的代码) – 2014-11-21 11:50:56

回答

0

,因为你使用

UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier]; 

所以电池重复使用,它已经有一个ImageView的。因此,使用此代码添加图像视图

for (int i =0; i < 4;) { 
    [[cell.priceRangeView viewWithTag:i] removeFromSuperview]; 
    PriceRangeImageView *img = [PriceRangeImageView new]; 

    img.tag = i; 
    if (i < _currentPoi.priceRange) { 
     [img initWithXMultiple:i withColor:_appDelegate.mainColor]; 
    } 
    else{ 
     [img initWithXMultiple:i withColor:_appDelegate.blackColor]; 
    } 

    [cell.priceRangeView addSubview:img]; 
    i++; 
} 
+0

谢谢,但它不起作用,我的美元的imageview不以这种方式显示。 – Jeffrey 2014-11-21 09:16:57

0

您不应该在每次添加一个新的img的时候初始化一个新的img。如果initWith ..代码不是太长,您应该为图像创建一个属性并调用一个方法来在setter中设置或设置。但是,不要创建一个新的实例加班,它会熄灭屏幕。

@property(strong,nonatomic)PriceRangeImageView* img; 


-(PriceRangeImageView*)img{ 
     if(!_img){ 
      _img = PriceRangeImageView *img = [PriceRangeImageView new]; 
     } 

    //do what you need to do to prepare it here.. omg = some image or whatever 

    return img; 


    } 

就用self.img为您的变量

0

,我发现这个解决方案和它的作品很好,我探微添加新的ImageView之前取消对我的priceRangeView所有子视图。

[cell.priceRangeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

    //affichage priceRange 
    for (int i =0; i < 4;) { 

     PriceRangeImageView *img = [PriceRangeImageView new]; 

     if (i < _currentPoi.priceRange) { 
      [img initWithXMultiple:i withColor:_appDelegate.mainColor]; 
     } 
     else{ 
      [img initWithXMultiple:i withColor:_appDelegate.blackColor]; 
     } 

     [cell.priceRangeView addSubview:img]; 
     i++; 
    } 
+0

我也说过相同 – BHASKAR 2014-11-21 10:15:10

+0

可能,但我不明白你的做事方式;)无论如何,谢谢 – Jeffrey 2014-11-21 11:10:40

+0

我只是设置imageview的标签,当新的单元格调用时,我通过这个标签删除旧的imageview。但是你删除了所有的子视图 – BHASKAR 2014-11-21 11:14:22