Création d'un UITableView personnalisé

UITableView de créer un UITableView selon ces images Une tableview peut avoir plusieurs sections et ses titres. Une TableView peut avoir un nombre de sections et un nombre de lignes. Mais, dans chaque section, une seule ligne peut être sélectionnée. Et, plus tard, je peux afficher dans mon UIAlertView avec la ligne sélectionnée comme message et la section comme titre. Comment puis-je atteindre cet objective?

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GroupCell"]; if (cell == nil) { cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell"]; } if (indexPath.row == self.selectedCell) { cell.accessoryType = UITableViewCellAccessoryCheckmark; cell.selected = YES; } else { cell.accessoryType = UITableViewCellAccessoryNone; cell.selected = NO; } cell.textLabel.text = @"mytext" cell.textLabel.textColor = [UIColor blackColor]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; self.selectedCell = indexPath.row; [tableView reloadData]; // here i have to make the condition.. } 

Une idée à ce sujet? Ou au less pour afficher un UITableView comme ci-dessous l'image est assez ..

entrez la description de l'image ici

Pour que la ligne soit sélectionnée à partir de chaque section, vous devez conserver un tableau de selectedCell. Et pour le moment désélectionné toutes les cellules, vous devez gérer un autre tableau isFirsttimeSelection Et garder un boolArray pour rowCollapse.

Interface ou implémentation de file d'en-tête

 NSArray *sectionArray; NSMutableDictionary *dictionary; NSMutableArray *boolArray; NSMutableArray *selectedCell; NSMutableArray *isFirsttimeSelection; 

jetez un oeil à l'initialisation des arrays et du dictionary à utiliser.

 dictionary=[[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSArray arrayWithObjects:@"Cat",@"Dog",@"Lion",@"Tiger",@"Elephant",nil],@"Animals",[NSArray arrayWithObjects:@"Swallow",@"Parrot",@"Eagle",@"Owl",nil],@"Birds",[NSArray arrayWithObjects:@"Banana",@"Mango",@"Grapes", nil],@"Fruits",nil]; sectionArray=[[NSArray alloc] initWithArray:[dictionary allKeys]]; boolArray=[[NSMutableArray alloc] init]; selectedCell=[[NSMutableArray alloc] init]; isFirsttimeSelection=[[NSMutableArray alloc] init]; for (int i=0;i<sectionArray.count;i++) { [boolArray addObject:[NSNumber numberWithBool:NO]]; [isFirsttimeSelection addObject:[NSNumber numberWithInt:NO]]; [selectedCell addObject:[NSNumber numberWithInt:0]]; } 

Les sources de données tableView et les delegates à utiliser comme: —

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [sectionArray count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ([boolArray[section] boolValue]) { return [[dictionary valueForKey:[sectionArray objectAtIndex:section]] count]; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSsortingng *cellIdentifier=@"cellIdentifier"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell==nil) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.accessoryType=((indexPath.row == [selectedCell[indexPath.section] intValue]) && ([isFirsttimeSelection[indexPath.section] intValue]==1))?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.textLabel.text=[[dictionary valueForKey:[sectionArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44; } 

En-tête pour la section

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIButton *headerView=[UIButton buttonWithType:UIButtonTypeCustom]; [headerView setTitle:[sectionArray objectAtIndex:section] forState:UIControlStateNormal]; headerView.tag=section; headerView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [headerView setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)]; [headerView.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0f]]; [headerView setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; [headerView setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal]; [headerView.titleLabel setShadowOffset:CGSizeMake(0, -1)]; [headerView addTarget:self action:@selector(sectionTouched:) forControlEvents:UIControlEventTouchUpInside]; return headerView; } 

Action sur l'en-tête cliqué

 - (void)sectionTouched:(UIButton *)sender { [boolArray replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:([boolArray[sender.tag] boolValue])?NO:YES]]; [_tblView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:([boolArray[sender.tag] boolValue])?UITableViewRowAnimationTop:UITableViewRowAnimationBottom]; } 

Et enfin a choisi

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; selectedCell[indexPath.section] = [NSSsortingng ssortingngWithFormat:@"%i",indexPath.row]; isFirsttimeSelection[indexPath.section]=[NSNumber numberWithBool:YES]; [tableView reloadData]; } 

Télécharge le.

Où, comme je sais, votre numéro de section est fixe. Ainsi, lorsque vous select une ligne, vérifiez que le nombre de lignes se trouve dans cette section et utilisez UITableViewCellAccessoryType à zéro pour les lignes et la UITableViewCellAccessoryType à cocher accessoryType pour la ligne sélectionnée. Et à la fin de la journée, vous devez vérifier quelles lignes sont sélectionnées dans chaque section et utiliser datatables de cette section.

J'espère que cela t'aides.

Lorsque l'user sélectionne cocher l'index de la section est déjà dans le tableau et supprime s'il existe et que la sélection peut être gérée dans cellForRowAtIndexpath le tableau contient l'index rend la cellule sélectionnée ou non avec accessoryType , et recharger la table lorsqu'une nouvelle ligne est sélectionnée et la gère correctement

Essayez quelque chose comme ça.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * selectedCell = [self.tableView cellForRowAtIndexPath:indexPath]; if (indexPath.section == 1) { selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; NSNumber * selectAnswer = [self.arrayWithObjects objectAtIndex:indexPath.row]; if (![self.selectedAnswers containsObject:selectAnswer]) { [self.arrSelObj addObject:selectAnswer]; } } } 

self.ArrayWithObjects – votre tableau avec les éléments utilisés dans la méthode numberOfRows . self.arrSelObj – tableau que contient votre réponse.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.arrSelObj containsObject:[self.arrayWithObjects objectAtIndex:indexPath.row]]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } } 

Dans didDeselect:

 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * deselectedCell = [tableView cellForRowAtIndexPath:indexPath]; if (indexPath.section == 1) { deselectedCell.accessoryType = UITableViewCellAccessoryNone; NSNumber * deselectAnswer = [self.arrayWithObjects objectAtIndex:indexPath.row]; [self.arrSelObj removeObject:deselectAnswer]; } } 

Vous devez implémenter une table nestede OU si la list est (prix …) sont correctifs puis utilisez la logique 🙂 comme trois buttons lorsque vous appuyez sur le button perticular puis réglez le cadre de la table, puis le button restant. 🙂