Avigit
Avigit

Reputation: 294

UIScrollView is not behaving smoothly after adding customized buttons

I have a UIScrollView in which i have more than 500 customized buttons and it can be up to a thousand depending on the requirement of a user. Actually, a subclass of UIButton. The reason I am using UIButton class is I have to perform some action on tap. The object looks like the following image

enter image description here

The arrangement of these buttons is like m*n matrix where row (m) is fixed to 20 but column (n) is not fixed. There can have 500 columns.

My problem is after adding buttons scrollView is not behaving smoothly. I used with the following code to create each each object

- (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];
}

The scrollView is not even working for 10*10 matrix.

Thanks in advance

Upvotes: 2

Views: 516

Answers (1)

Richard Brown
Richard Brown

Reputation: 11436

I'd look into UICollectionView. You're going to run into memory issues as it stands now. UICollectionView allows you to create a UICollectionViewCell similar to UITableViewCells that can be reused.

See the Apple Documentation.

Upvotes: 1

Related Questions