Custom UItableViewCell with PureLayout and dynamic number of buttons

344 Views Asked by At

I have created a custom UITableViewCell which display dynamic number of buttons in horizontal access using the following initWithStyle

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        emailBtn = [UIButton newAutoLayoutView];
        [emailBtn setImage:[UIImage imageNamed:@"emailBtn"] forState:UIControlStateNormal];
        [emailBtn setImage:[UIImage imageNamed:@"emailBtn"] forState:UIControlStateSelected];
        [emailBtn addTarget:self action:@selector(emailBtnPressed:) forControlEvents:UIControlEventTouchUpInside];

        facebookBtn = [UIButton newAutoLayoutView];
        [facebookBtn setImage:[UIImage imageNamed:@"fbBtn"] forState:UIControlStateNormal];
        [facebookBtn setImage:[UIImage imageNamed:@"fbBtn"] forState:UIControlStateSelected];
        [facebookBtn addTarget:self action:@selector(facebookBtnPressed:) forControlEvents:UIControlEventTouchUpInside];

        twitterBtn = [UIButton newAutoLayoutView];
        [twitterBtn setImage:[UIImage imageNamed:@"twBtn"] forState:UIControlStateNormal];
        [twitterBtn setImage:[UIImage imageNamed:@"twBtn"] forState:UIControlStateSelected];
        [twitterBtn addTarget:self action:@selector(twitterBtnPressed:) forControlEvents:UIControlEventTouchUpInside];

        linkedinBtn = [UIButton newAutoLayoutView];
        [linkedinBtn setImage:[UIImage imageNamed:@"linkedInBtn"] forState:UIControlStateNormal];
        [linkedinBtn setImage:[UIImage imageNamed:@"linkedInBtn"] forState:UIControlStateSelected];
        [linkedinBtn addTarget:self action:@selector(linkedinBtnPressed:) forControlEvents:UIControlEventTouchUpInside];

        self.contentView.backgroundColor = [UIColor orangeColor];

        [self.contentView addSubview:emailBtn];
        [self.contentView addSubview:facebookBtn];
        [self.contentView addSubview:twitterBtn];
        [self.contentView addSubview:linkedinBtn];


        self.btnsArray = @[emailBtn,facebookBtn,twitterBtn,linkedinBtn];
        [self setNeedsUpdateConstraints]; // bootstrap Auto Layout
    }

    return self;
}

It works well and it display 4 buttons aligned in horizontal access , the problem I'm facing is that I need to make this buttons dynamic depending in data retrieved from API , for example if there are no Facebook link , Cell should display only 3 buttons .... The problem that

[self setNeedsUpdateConstraints]; // bootstrap Auto Layout

is called in initializing the cell before I could configure cell data to determine the true number of buttons and their links.

Could any have suggestion for current situation?

0

There are 0 best solutions below