Dynamic content cell

Hi everyone,

i’m making a custom cell in which, programmatically, I’m adding some labels and its constraints. But I’ve a problem.

in this app that i’m developing, there is a view that show the app’s settings. these settings are only about language. A user can decide if a word can be shown in all available language or choose which language activate and which switch off.

for example you can choose between english, french, spanish, german. default this value are on, available. so you can see the word also in this languages. Now the problem, i want that if I switch off, for example, spanish, you see the word in english, french and german; Or, if I switch off english and french, you can see the word in spanish and german language but not in english and french; and so on.

Have any idea about how can I do this programmatically?

my cell is this:
one note: for may app Kanji, Furigana, Romaji and Italian are locked. what change is English and other language. Actually i’ll have only english but i won’t in the future add other languages.

`CGRect imageViewJapRect = CGRectMake(16, 14, 25, 25);
_imageViewJap = [[UIImageView alloc] initWithFrame:imageViewJapRect];
[_imageViewJap setImage:[UIImage imageNamed:@“jp.png”]];
[_imageViewJap setBackgroundColor:[UIColor redColor]];
_imageViewJap.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_imageViewJap];

CGRect textFuriganaLabelRect = CGRectMake(57, 8, 510, 21);
_textFuriganaLabel = [[UILabel alloc] initWithFrame:textFuriganaLabelRect];
[_textFuriganaLabel setBackgroundColor:[UIColor greenColor]];
_textFuriganaLabel.textAlignment = NSTextAlignmentLeft;
_textFuriganaLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_textFuriganaLabel];

CGRect textKanjiLabelRect = CGRectMake(57, 27, 510, 21);
_textKanjiLabel = [[UILabel alloc]initWithFrame:textKanjiLabelRect];
[_textKanjiLabel setBackgroundColor:[UIColor orangeColor]];
_textKanjiLabel.textAlignment = NSTextAlignmentLeft;
_textKanjiLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_textKanjiLabel];

CGRect imageViewRomajiRect = CGRectMake(16, 56, 25, 25);
_imageViewRomaji = [[UIImageView alloc]initWithFrame:imageViewRomajiRect];
[_imageViewRomaji setImage:[UIImage imageNamed:@"Translation-Icon.jpg"]];
[_imageViewRomaji setBackgroundColor:[UIColor blueColor]];
_imageViewRomaji.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_imageViewRomaji];

CGRect textRomajiLabelRect = CGRectMake(57, 58, 510, 21);
_textRomajiLabel = [[UILabel alloc]initWithFrame:textRomajiLabelRect];
_textRomajiLabel.textAlignment = NSTextAlignmentLeft;
[_textRomajiLabel setBackgroundColor:[UIColor lightGrayColor]];
_textRomajiLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_textRomajiLabel];

CGRect imageViewItaRect = CGRectMake(16, 89, 25, 25);
_imageViewIta = [[UIImageView alloc]initWithFrame:imageViewItaRect];
[_imageViewIta setImage:[UIImage imageNamed:@"it.png"]];
[_imageViewIta setBackgroundColor:[UIColor purpleColor]];
_imageViewIta.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_imageViewIta];

CGRect textItaLabelRect = CGRectMake(57, 91, 510, 21);
_textItaLabel = [[UILabel alloc]initWithFrame:textItaLabelRect];
_textItaLabel.textAlignment = NSTextAlignmentLeft;
[_textItaLabel setBackgroundColor:[UIColor brownColor]];
_textItaLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_textItaLabel];


CGRect imageViewEnRect = CGRectMake(16, 122, 25, 25);
_imageViewEn = [[UIImageView alloc]initWithFrame:imageViewEnRect];
[_imageViewEn setImage:[UIImage imageNamed:@"en.png"]];
[_imageViewEn setBackgroundColor:[UIColor yellowColor]];
_imageViewEn.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_imageViewEn];

CGRect textEnLabelRect = CGRectMake(57, 124, 510, 21);
_textEnLabel = [[UILabel alloc]initWithFrame:textEnLabelRect];
_textEnLabel.textAlignment = NSTextAlignmentLeft;
[_textEnLabel setBackgroundColor:[UIColor purpleColor]];
_textEnLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_textEnLabel];

NSDictionary *viewDictionary = @{@"imageJap":_imageViewJap, @"textKanji":_textKanjiLabel, @"textFurigana":_textFuriganaLabel, @"imageRomaji":_imageViewRomaji, @"textRomaji":_textRomajiLabel, @"imageIta":_imageViewIta, @"textIta":_textItaLabel, @"imageEn":_imageViewEn, @"textEn":_textEnLabel};

NSArray *imageJap_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageJap(25)]" options:0 metrics:nil views:viewDictionary];
NSArray *imageJap_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageJap(25)]" options:0 metrics:nil views:viewDictionary];
NSArray *imageJap_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-16-[imageJap]" options:0 metrics:nil views:viewDictionary];
NSArray *imageJap_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-14-[imageJap]" options:0 metrics:nil views:viewDictionary];

NSArray *textFurigana_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageJap]-16-[textFurigana]-2-|" options:0 metrics:nil views:viewDictionary];
NSArray *textFurigana_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[textFurigana]" options:0 metrics:nil views:viewDictionary];

NSArray *textKanji_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageJap]-16-[textKanji]-2-|" options:0 metrics:nil views:viewDictionary];
NSArray *textKanji_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-27-[textKanji]" options:0 metrics:nil views:viewDictionary];

NSArray *imageRomaji_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageRomaji(25)]" options:0 metrics:nil views:viewDictionary];
NSArray *imageRomaji_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageRomaji(25)]" options:0 metrics:nil views:viewDictionary];
NSArray *imageRomaji_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-16-[imageRomaji]" options:0 metrics:nil views:viewDictionary];
NSArray *imageRomaji_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageJap]-17-[imageRomaji]" options:0 metrics:nil views:viewDictionary];

NSArray *textRomaji_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageRomaji]-16-[textRomaji]-2-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:viewDictionary];
NSArray *textRomaji_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[textKanji]-10-[textRomaji]" options:0 metrics:nil views:viewDictionary];

NSArray *imageITA_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageIta(25)]" options:0 metrics:nil views:viewDictionary];
NSArray *imageITA_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageIta(25)]" options:0 metrics:nil views:viewDictionary];
NSArray *imageITA_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-16-[imageIta]" options:0 metrics:nil views:viewDictionary];
NSArray *imageITA_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageRomaji]-8-[imageIta]" options:0 metrics:nil views:viewDictionary];

NSArray *textITA_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageIta]-16-[textIta]-2-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:viewDictionary];
NSArray *textITA_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[textRomaji]-12-[textIta]" options:0 metrics:nil views:viewDictionary];

NSArray *imageEN_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageEn(25)]" options:0 metrics:nil views:viewDictionary];
NSArray *imageEN_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageEn(25)]" options:0 metrics:nil views:viewDictionary];
NSArray *imageEN_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-16-[imageEn]" options:0 metrics:nil views:viewDictionary];
NSArray *imageEN_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageIta]-8-[imageEn]-8-|" options:0 metrics:nil views:viewDictionary];

NSArray *textEN_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageEn]-16-[textEn]-2-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:viewDictionary];
NSArray *textEN_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[textIta]-12-[textEn]-10-|" options:0 metrics:nil views:viewDictionary];

[self.imageViewJap addConstraints:imageJap_H];
[self.imageViewJap addConstraints:imageJap_V];
[self.contentView addConstraints:imageJap_POS_H];
[self.contentView addConstraints:imageJap_POS_V];

[self.contentView addConstraints:textFurigana_POS_H];
[self.contentView addConstraints:textFurigana_POS_V];

[self.contentView addConstraints:textKanji_POS_H];
[self.contentView addConstraints:textKanji_POS_V];

[self.imageViewRomaji addConstraints:imageRomaji_H];
[self.imageViewRomaji addConstraints:imageRomaji_V];
[self.contentView addConstraints:imageRomaji_POS_H];
[self.contentView addConstraints:imageRomaji_POS_V];

[self.contentView addConstraints:textRomaji_POS_H];
[self.contentView addConstraints:textRomaji_POS_V];

[self.imageViewIta addConstraints:imageITA_H];
[self.imageViewIta addConstraints:imageITA_V];
[self.contentView addConstraints:imageITA_POS_H];
[self.contentView addConstraints:imageITA_POS_V];

[self.contentView addConstraints:textITA_POS_H];
[self.contentView addConstraints:textITA_POS_V];

[self.imageViewEn addConstraints:imageEN_H];
[self.imageViewEn addConstraints:imageEN_V];
[self.contentView addConstraints:imageEN_POS_H];
[self.contentView addConstraints:imageEN_POS_V];

[self.contentView addConstraints:textEN_POS_H];
[self.contentView addConstraints:textEN_POS_V];`

You can hide the cells of the languages the user switches off by either removing the cell from the tableview or by hiding the cell from view by making its height 0.

To remove it you call:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        [self.moduleTitleStack removeObjectAtIndex:indexPath.row];
        [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

by calling:

[self.myTableView setEditing:YES animated:YES];

@marciokoko thank you very much for your answer but the problem is different. maybe I’ve explained not very well.

the cell that contains the languages is one. It’s not one cell per language. understand? This is problem.

in fact if you see the code, this code set up all image views and all labels with constraints. this code I’ve written in the method - (void)awakeFromNib of my custom cell file.

Question:what i have to do? i insert an condition in which check the status of language and if is on add image view and label?

if the answer is ‘yes’ when i’ll have more language, what I do? insert more condition to manage the languages?

Well you could change the properties of the individual imageviews that contain the languages you want and either remove them from view or set their alpha to 0.

I would really put each one in a cell by itself.

@marciokoko thanks for answer, [quote=“marciokoko, post:4, topic:13919”]
Well you could change the properties of the individual imageviews that contain the languages you want and either remove them from view or set their alpha to 0.
[/quote]

but how can I do it? if the language is only one, the problem is easy: check in the usersettingsdefault the language and if it is on I add the image view, labels, add the constrains. and is done. but if i have more of one language, to manage them become a few hard.

because if i have 4 languages, english, french, spanish,and german, for example, an user can choose french and spanish be on and others off or vice versa. or change putting in on english and french and others in off. and other cases.

I would make something of automatic, so I make this code and can I add language without problem. understand?

however, if you need an image to understand well this is image
i’m trying to copy this cell.
ok?

I thought to put the image view and label in array of dictionary, loop on this checking if the language is on and if it is add it with constraints. what do you think about it? do you think to be the solution at the problem? or have any suggestion?