我有一个UIScrollView,其中我有超过500个自定义按钮,根据用户的要求,它可以达到一千个。实际上,是UIButton的子类。我使用UIButton类的原因是我必须执行一些操作。该对象如下图所示
这些按钮的排列类似于m * n矩阵,其中行(m)固定为20但列(n)不固定。可以有500列。
我的问题是在添加按钮后,scrollView表现不顺畅。我使用以下代码创建每个对象
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
sizeLabel = [[UILabel alloc] initWithFrame:sizeLabelRect];
sizeLabel.font = [UIFont boldSystemFontOfSize:sizeLabelFontSize];
sizeLabel.textAlignment = UITextAlignmentCenter;
// sizeLabel.backgroundColor = [self colorWithHexString:LABEL_BG_COLOR];
// sizeLabel.layer.cornerRadius = CORNER_RADIUS;
[self addSubview:sizeLabel];
nFilesLabel = [[UILabel alloc] initWithFrame:nFilesLabelRect];
nFilesLabel.font = [UIFont boldSystemFontOfSize:nFilesFontSize];
nFilesLabel.textAlignment = UITextAlignmentCenter;
// nFilesLabel.backgroundColor = [self colorWithHexString:LABEL_BG_COLOR];
// nFilesLabel.layer.cornerRadius = CORNER_RADIUS;
[self addSubview:nFilesLabel];
maxDisimilarity = [[UILabel alloc] initWithFrame:disimilarityRect];
// maxDisimilarity.layer.cornerRadius = CORNER_RADIUS;
[self addSubview:maxDisimilarity];
// nFragment = [[UILabel alloc] init];
// nFragment.font = [UIFont systemFontOfSize:nFragmentFontSize];
// [self addSubview:nFragment];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void)setObj:(CloneClass *)obj
{
NSString *colorCode;
if (obj != nil) {
_obj = obj;
if ([_obj.type isEqualToString:@"1"]) {
colorCode = COLOR;
} else if ([_obj.type isEqualToString:@"2"]) {
colorCode = COLOR;
} else if ([_obj.obj isEqualToString:@"Type-3"]) {
colorCode = COLOR;
}
self.backgroundColor = [self colorWithHexString:colorCode];
[self setTitle:[NSString stringWithFormat:@"%d", obj.nFragments] forState:UIControlStateNormal];
// set size of clone class
if (_obj.size > LARGE_SIZE) {
sizeLabel.text = LARGE_SIZE_TEXT;
} else if (_obj.size > MEDIUM_SIZE && _obj.size <= LARGE_SIZE) {
sizeLabel.text = MEDIUM_SIZE_TEXT;
} else {
sizeLabel.text = SMALL_SIZE_TEXT;
}
// set number of files within a clone class
nFilesLabel.text = [NSString stringWithFormat:@"%d", _cloneClass.nFiles];
// set color for disimilarity
CGFloat similarity = 1.0 - _cloneClass.maxDisimilarity;
maxDisimilarity.backgroundColor = [UIColor colorWithRed:1.0 green:similarity blue:similarity alpha:1.0];
} else {
self.backgroundColor = [UIColor grayColor];
[self setTitle:@"/" forState:UIControlStateNormal];
}
self.titleLabel.textColor = [UIColor blackColor];
}
scrollView甚至不适用于10 * 10矩阵。
提前致谢
答案 0 :(得分:1)
我会调查UICollectionView
。你现在要遇到内存问题。 UICollectionView
允许您创建与UICollectionViewCell
类似的UITableViewCell
,可以重复使用。