【发布时间】:2014-11-21 08:58:54
【问题描述】:
我正在开发 UITableView,它显示每个条目的价格范围值。 我正在使用循环来显示价格范围。我在寻找我的赌注在哪里,因为当我上下滚动时,每个已显示、隐藏和重新显示的单元格,叠加“美元”的图像,它丑陋,看图片。
我如何设置我的单元格:
PoiAllTableCell *cell = (PoiAllTableCell *)[tableView dequeueReusableCellWithIdentifier:@"PoiAllCell" forIndexPath:indexPath];
我正在使用此代码显示美元图像视图:
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
我在想,不是在每个滚动单元格上添加美元图像视图,我想知道如何避免这种情况。如果有人可以建议我更好的方法?
【问题讨论】:
-
试试这个@Barzull [cell.contentView addSubview:img] stackoverflow.com/a/26881504/3767017
-
谢谢,但 cell.priceRangeView 是一个 UIView,我尝试了你的建议,但它又叠加了。
-
只是一个建议,当您制作自定义单元格并为其制作 UITableViewCell 子类时,为什么要为其单元格数据制作另一个类,您应该在同一个子类中提供所有数据(据我了解查看您的代码)
-
对不起,我不明白你的问题,你能解释一下吗?
标签: ios objective-c uitableview uiimageview