uitableviewcell source number example data ios iphone uitableview object

source - Expandir/contraer sección en UITableView en iOS



uitableviewcell swift 3 (14)

Algunos ejemplos de código para animar una acción de expandir / contraer usando un encabezado de sección de vista de tabla son proporcionados por Apple aquí: Animaciones y gestos de la vista de tabla

La clave de este enfoque es implementar - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section y devolver una UIView personalizada que incluye un botón (generalmente del mismo tamaño que la vista del encabezado). Al subclasificar UIView y usarlo para la vista de encabezado (como lo hace esta muestra), puede almacenar fácilmente datos adicionales, como el número de sección.

¿Podría alguien decirme la forma de realizar UITableView expandibles / UITableView de UITableView en sections de UITableView como se muestra a continuación?

o


Encontré otra forma relativamente simple de resolver ese problema. Al usar este método no necesitaremos alterar nuestra celda, que casi siempre está relacionada con el índice de la matriz de datos, lo que podría causar problemas en nuestro controlador de visualización.

Primero, agregamos las siguientes propiedades a nuestra clase de controlador:

@property (strong, nonatomic) NSMutableArray* collapsedSections; @property (strong, nonatomic) NSMutableArray* sectionViews;

collapsedSections secciones colapsadas guardarán números de sección colapsados. sectionViews almacenará nuestra vista de sección personalizada.

Sintetizarlo:

@synthesize collapsedSections; @synthesize sectionViews;

Inicialízalo:

- (void) viewDidLoad { [super viewDidLoad]; self.collapsedSections = [NSMutableArray array]; self.sectionViews = [NSMutableArray array]; }

Después de eso, debemos conectar nuestra UITableView para que se pueda acceder desde nuestra clase de controlador de vista:

@property (strong, nonatomic) IBOutlet UITableView *tblMain;

Conéctelo desde XIB para ver el controlador usando ctrl + drag como habitualmente.

A continuación, creamos la vista como encabezado de sección personalizado para nuestra vista de tabla implementando este delegado UITableView:

- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // Create View CGRect frame = CGRectZero; frame.origin = CGPointZero; frame.size.height = 30.f; frame.size.width = tableView.bounds.size.width; UIView* view = [[UIView alloc] initWithFrame:frame]; [view setBackgroundColor:[UIColor blueColor]]; // Add label for title NSArray* titles = @[@"Title 1", @"Title 2", @"Title 3"]; NSString* selectedTitle = [titles objectAtIndex:section]; CGRect labelFrame = frame; labelFrame.size.height = 30.f; labelFrame.size.width -= 20.f; labelFrame.origin.x += 10.f; UILabel* titleLabel = [[UILabel alloc] initWithFrame:labelFrame]; [titleLabel setText:selectedTitle]; [titleLabel setTextColor:[UIColor whiteColor]]; [view addSubview:titleLabel]; // Add touch gesture [self attachTapGestureToView:view]; // Save created view to our class property array [self saveSectionView:view inSection:section]; return view; }

A continuación, implementamos el método para guardar nuestro encabezado de sección personalizado creado previamente en la propiedad de clase:

- (void) saveSectionView:(UIView*) view inSection:(NSInteger) section { NSInteger sectionCount = [self numberOfSectionsInTableView:[self tblMain]]; if(section < sectionCount) { if([[self sectionViews] indexOfObject:view] == NSNotFound) { [[self sectionViews] addObject:view]; } } }

Agregue UIGestureRecognizerDelegate a nuestro controlador de vista archivo .h:

@interface MyViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>

Luego creamos el método attachTapGestureToView:

- (void) attachTapGestureToView:(UIView*) view { UITapGestureRecognizer* tapAction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)]; [tapAction setDelegate:self]; [view addGestureRecognizer:tapAction]; }

El método anterior agregará el reconocedor de gestos de toque a toda la vista de sección que creamos antes. A continuación, debemos implementar onTap: selector

- (void) onTap:(UITapGestureRecognizer*) gestureRecognizer { // Take view who attach current recognizer UIView* sectionView = [gestureRecognizer view]; // [self sectionViews] is Array containing our custom section views NSInteger section = [self sectionNumberOfView:sectionView]; // [self tblMain] is our connected IBOutlet table view NSInteger sectionCount = [self numberOfSectionsInTableView:[self tblMain]]; // If section more than section count minus one set at last section = section > (sectionCount - 1) ? 2 : section; [self toggleCollapseSection:section]; }

El método anterior se invocará cuando el usuario toque cualquiera de nuestra sección de vista de tabla. Este método busca el número de sección correcto en función de nuestra matriz de sectionViews y sectionViews que creamos anteriormente.

Además, implementamos el método para obtener con qué sección de la vista del encabezado pertenece.

- (NSInteger) sectionNumberOfView:(UIView*) view { UILabel* label = [[view subviews] objectAtIndex:0]; NSInteger sectionNum = 0; for(UIView* sectionView in [self sectionViews]) { UILabel* sectionLabel = [[sectionView subviews] objectAtIndex:0]; //NSLog(@"Section: %d -> %@ vs %@", sectionNum, [label text], [sectionLabel text]); if([[label text] isEqualToString:[sectionLabel text]]) { return sectionNum; } sectionNum++; } return NSNotFound; }

A continuación, debemos implementar el método toggleCollapseSection:

- (void) toggleCollapseSection:(NSInteger) section { if([self isCollapsedSection:section]) { [self removeCollapsedSection:section]; } else { [self addCollapsedSection:section]; } [[self tblMain] reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade]; }

Este método insertará / eliminará el número de sección en nuestra matriz collapsedSections que creamos anteriormente. Cuando se inserta un número de sección en esa matriz, significa que la sección debe colapsarse y expandirse si no es así.

A continuación implementamos removeCollapsedSection: addCollapsedSection:section e isCollapsedSection:section

- (BOOL)isCollapsedSection:(NSInteger) section { for(NSNumber* existing in [self collapsedSections]) { NSInteger current = [existing integerValue]; if(current == section) { return YES; } } return NO; } - (void)removeCollapsedSection:(NSInteger) section { [[self collapsedSections] removeObjectIdenticalTo:[NSNumber numberWithInteger:section]]; } - (void)addCollapsedSection:(NSInteger) section { [[self collapsedSections] addObject:[NSNumber numberWithInteger:section]]; }

Este tres método es solo ayuda para facilitarnos el acceso a la matriz collapsedSections .

Finalmente, implemente este delegado de vista de tabla para que nuestras vistas de sección personalizadas se vean bien.

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 30.f; // Same as each custom section view height }

Espero eso ayude.


Entonces, en base a la solución ''button in header'', aquí hay una implementación limpia y minimalista:

  • realiza un seguimiento de las secciones colapsadas (o expandidas) en una propiedad
  • etiqueta el botón con el índice de la sección
  • configura un estado seleccionado en ese botón para cambiar la dirección de la flecha (como △ y ▽)

Aquí está el código:

@interface MyTableViewController () @property (nonatomic, strong) NSMutableIndexSet *collapsedSections; @end ... @implementation MyTableViewController - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (!self) return; self.collapsedSections = [NSMutableIndexSet indexSet]; return self; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // if section is collapsed if ([self.collapsedSections containsIndex:section]) return 0; // if section is expanded #warning incomplete implementation return [super tableView:tableView numberOfRowsInSection:section]; } - (IBAction)toggleSectionHeader:(UIView *)sender { UITableView *tableView = self.tableView; NSInteger section = sender.tag; MyTableViewHeaderFooterView *headerView = (MyTableViewHeaderFooterView *)[self tableView:tableView viewForHeaderInSection:section]; if ([self.collapsedSections containsIndex:section]) { // section is collapsed headerView.button.selected = YES; [self.collapsedSections removeIndex:section]; } else { // section is expanded headerView.button.selected = NO; [self.collapsedSections addIndex:section]; } [tableView beginUpdates]; [tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic]; [tableView endUpdates]; } @end


Esta es la mejor forma que encontré para crear celdas de vista de tabla expandibles

archivo .h

NSMutableIndexSet *expandedSections;

archivo .m

if (!expandedSections) { expandedSections = [[NSMutableIndexSet alloc] init]; } UITableView *masterTable = [[UITableView alloc] initWithFrame:CGRectMake(0,100,1024,648) style:UITableViewStyleGrouped]; masterTable.delegate = self; masterTable.dataSource = self; [self.view addSubview:masterTable];

Métodos de delegación de vista de tabla

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section { // if (section>0) return YES; return YES; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 4; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ([self tableView:tableView canCollapseSection:section]) { if ([expandedSections containsIndex:section]) { return 5; // return rows when expanded } return 1; // only top row showing } // Return the number of rows in the section. return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; } // Configure the cell... if ([self tableView:tableView canCollapseSection:indexPath.section]) { if (!indexPath.row) { // first row cell.textLabel.text = @"Expandable"; // only top row showing if ([expandedSections containsIndex:indexPath.section]) { UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UITableContract"]]; cell.accessoryView = imView; } else { UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UITableExpand"]]; cell.accessoryView = imView; } } else { // all other rows if (indexPath.section == 0) { cell.textLabel.text = @"section one"; }else if (indexPath.section == 1) { cell.textLabel.text = @"section 2"; }else if (indexPath.section == 2) { cell.textLabel.text = @"3"; }else { cell.textLabel.text = @"some other sections"; } cell.accessoryView = nil; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } } else { cell.accessoryView = nil; cell.textLabel.text = @"Normal Cell"; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self tableView:tableView canCollapseSection:indexPath.section]) { if (!indexPath.row) { // only first row toggles exapand/collapse [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSInteger section = indexPath.section; BOOL currentlyExpanded = [expandedSections containsIndex:section]; NSInteger rows; NSMutableArray *tmpArray = [NSMutableArray array]; if (currentlyExpanded) { rows = [self tableView:tableView numberOfRowsInSection:section]; [expandedSections removeIndex:section]; } else { [expandedSections addIndex:section]; rows = [self tableView:tableView numberOfRowsInSection:section]; } for (int i=1; i<rows; i++) { NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section]; [tmpArray addObject:tmpIndexPath]; } UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (currentlyExpanded) { [tableView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop]; UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UITableExpand"]]; cell.accessoryView = imView; } else { [tableView insertRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop]; UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UITableContract"]]; cell.accessoryView = imView; } } } NSLog(@"section :%d,row:%d",indexPath.section,indexPath.row); }


Obtuve una buena solución inspirada en las animaciones y gestos de Table View de Apple. Eliminé partes innecesarias de la muestra de Apple y las traduje rápidamente.

Sé que la respuesta es bastante larga, pero todo el código es necesario. Afortunadamente, puedes copiar y pasar la mayor parte del código y solo necesitas hacer una pequeña modificación en el paso 1 y 3

1.crear SectionHeaderView.swift y SectionHeaderView.xib

import UIKit protocol SectionHeaderViewDelegate { func sectionHeaderView(sectionHeaderView: SectionHeaderView, sectionOpened: Int) func sectionHeaderView(sectionHeaderView: SectionHeaderView, sectionClosed: Int) } class SectionHeaderView: UITableViewHeaderFooterView { var section: Int? @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var disclosureButton: UIButton! @IBAction func toggleOpen() { self.toggleOpenWithUserAction(true) } var delegate: SectionHeaderViewDelegate? func toggleOpenWithUserAction(userAction: Bool) { self.disclosureButton.selected = !self.disclosureButton.selected if userAction { if self.disclosureButton.selected { self.delegate?.sectionHeaderView(self, sectionClosed: self.section!) } else { self.delegate?.sectionHeaderView(self, sectionOpened: self.section!) } } } override func awakeFromNib() { var tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "toggleOpen") self.addGestureRecognizer(tapGesture) // change the button image here, you can also set image via IB. self.disclosureButton.setImage(UIImage(named: "arrow_up"), forState: UIControlState.Selected) self.disclosureButton.setImage(UIImage(named: "arrow_down"), forState: UIControlState.Normal) } }

el SectionHeaderView.xib (la vista con fondo gris) debería verse de esta manera en una vista de tabla (puede personalizarlo según sus necesidades, por supuesto):

Nota:

a) la acción toggleOpen debe estar vinculada a disclosureButton

b) las acciones disclosureButton y toggleOpen no son necesarias. Puede eliminar estas 2 cosas si no necesita el botón.

2.crear SectionInfo.swift

import UIKit class SectionInfo: NSObject { var open: Bool = true var itemsInSection: NSMutableArray = [] var sectionTitle: String? init(itemsInSection: NSMutableArray, sectionTitle: String) { self.itemsInSection = itemsInSection self.sectionTitle = sectionTitle } }

3. en su tabla vista

import UIKit class TableViewController: UITableViewController, SectionHeaderViewDelegate { let SectionHeaderViewIdentifier = "SectionHeaderViewIdentifier" var sectionInfoArray: NSMutableArray = [] override func viewDidLoad() { super.viewDidLoad() let sectionHeaderNib: UINib = UINib(nibName: "SectionHeaderView", bundle: nil) self.tableView.registerNib(sectionHeaderNib, forHeaderFooterViewReuseIdentifier: SectionHeaderViewIdentifier) // you can change section height based on your needs self.tableView.sectionHeaderHeight = 30 // You should set up your SectionInfo here var firstSection: SectionInfo = SectionInfo(itemsInSection: ["1"], sectionTitle: "firstSection") var secondSection: SectionInfo = SectionInfo(itemsInSection: ["2"], sectionTitle: "secondSection")) sectionInfoArray.addObjectsFromArray([firstSection, secondSection]) } // MARK: - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return sectionInfoArray.count } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if self.sectionInfoArray.count > 0 { var sectionInfo: SectionInfo = sectionInfoArray[section] as! SectionInfo if sectionInfo.open { return sectionInfo.open ? sectionInfo.itemsInSection.count : 0 } } return 0 } override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let sectionHeaderView: SectionHeaderView! = self.tableView.dequeueReusableHeaderFooterViewWithIdentifier(SectionHeaderViewIdentifier) as! SectionHeaderView var sectionInfo: SectionInfo = sectionInfoArray[section] as! SectionInfo sectionHeaderView.titleLabel.text = sectionInfo.sectionTitle sectionHeaderView.section = section sectionHeaderView.delegate = self let backGroundView = UIView() // you can customize the background color of the header here backGroundView.backgroundColor = UIColor(red:0.89, green:0.89, blue:0.89, alpha:1) sectionHeaderView.backgroundView = backGroundView return sectionHeaderView } func sectionHeaderView(sectionHeaderView: SectionHeaderView, sectionOpened: Int) { var sectionInfo: SectionInfo = sectionInfoArray[sectionOpened] as! SectionInfo var countOfRowsToInsert = sectionInfo.itemsInSection.count sectionInfo.open = true var indexPathToInsert: NSMutableArray = NSMutableArray() for i in 0..<countOfRowsToInsert { indexPathToInsert.addObject(NSIndexPath(forRow: i, inSection: sectionOpened)) } self.tableView.insertRowsAtIndexPaths(indexPathToInsert as [AnyObject], withRowAnimation: .Top) } func sectionHeaderView(sectionHeaderView: SectionHeaderView, sectionClosed: Int) { var sectionInfo: SectionInfo = sectionInfoArray[sectionClosed] as! SectionInfo var countOfRowsToDelete = sectionInfo.itemsInSection.count sectionInfo.open = false if countOfRowsToDelete > 0 { var indexPathToDelete: NSMutableArray = NSMutableArray() for i in 0..<countOfRowsToDelete { indexPathToDelete.addObject(NSIndexPath(forRow: i, inSection: sectionClosed)) } self.tableView.deleteRowsAtIndexPaths(indexPathToDelete as [AnyObject], withRowAnimation: .Top) } } }


Para implementar la sección de la tabla contraíble en iOS, la magia es cómo controlar el número de filas para cada sección, o podemos administrar el alto de las filas para cada sección.

Además, tenemos que personalizar el encabezado de la sección para que podamos escuchar el evento tap desde el área del encabezado (ya sea un botón o todo el encabezado).

¿Cómo lidiar con el encabezado? Es muy simple, ampliamos la clase UITableViewCell y hacemos una celda de encabezado personalizada así:

import UIKit class CollapsibleTableViewHeader: UITableViewCell { @IBOutlet var titleLabel: UILabel! @IBOutlet var toggleButton: UIButton! }

luego use viewForHeaderInSection para conectar la celda del encabezado:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let header = tableView.dequeueReusableCellWithIdentifier("header") as! CollapsibleTableViewHeader header.titleLabel.text = sections[section].name header.toggleButton.tag = section header.toggleButton.addTarget(self, action: #selector(CollapsibleTableViewController.toggleCollapse), forControlEvents: .TouchUpInside) header.toggleButton.rotate(sections[section].collapsed! ? 0.0 : CGFloat(M_PI_2)) return header.contentView }

Recuerde que debemos devolver el contentView porque esta función espera que se devuelva un UIView.

Ahora vamos a tratar con la parte colapsable, aquí está la función de alternar que alternar el pilar colapsable de cada sección:

func toggleCollapse(sender: UIButton) { let section = sender.tag let collapsed = sections[section].collapsed // Toggle collapse sections[section].collapsed = !collapsed // Reload section tableView.reloadSections(NSIndexSet(index: section), withRowAnimation: .Automatic) }

depende de cómo administre los datos de la sección, en este caso, tengo los datos de la sección algo como esto:

struct Section { var name: String! var items: [String]! var collapsed: Bool! init(name: String, items: [String]) { self.name = name self.items = items self.collapsed = false } } var sections = [Section]() sections = [ Section(name: "Mac", items: ["MacBook", "MacBook Air", "MacBook Pro", "iMac", "Mac Pro", "Mac mini", "Accessories", "OS X El Capitan"]), Section(name: "iPad", items: ["iPad Pro", "iPad Air 2", "iPad mini 4", "Accessories"]), Section(name: "iPhone", items: ["iPhone 6s", "iPhone 6", "iPhone SE", "Accessories"]) ]

por último, lo que tenemos que hacer se basa en el apoyo colapsable de cada sección, controlar el número de filas de esa sección:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return (sections[section].collapsed!) ? 0 : sections[section].items.count }

Tengo una demostración completamente funcional en mi Github: https://github.com/jeantimex/ios-swift-collapsible-table-section

Si desea implementar las secciones colapsables en una tabla de estilo agrupado, tengo otra demostración con código fuente aquí: https://github.com/jeantimex/ios-swift-collapsible-table-section-in-grouped-section

Espero que ayude.


Tengo una solución mejor que debe agregar un UIButton en el encabezado de sección y establecer el tamaño de este botón igual al tamaño de sección, pero ocultarlo por el color de fondo claro, después de eso puede comprobar fácilmente en qué sección se hace clic para expandir o colapsar


Terminé simplemente creando un headerView que contenía un botón (vi la solución de Son Nguyen más arriba después del hecho, pero aquí está mi código ... parece mucho, pero es bastante simple):

declara un par de bools para ti secciones

bool customerIsCollapsed = NO; bool siteIsCollapsed = NO;

...código

ahora en los métodos delegados de tu vista de tabla ...

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tblSearchResults.frame.size.width, 35)]; UILabel *lblSection = [UILabel new]; [lblSection setFrame:CGRectMake(0, 0, 300, 30)]; [lblSection setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]]; [lblSection setBackgroundColor:[UIColor clearColor]]; lblSection.alpha = 0.5; if(section == 0) { if(!customerIsCollapsed) [lblSection setText:@"Customers --touch to show--"]; else [lblSection setText:@"Customers --touch to hide--"]; } else { if(!siteIsCollapsed) [lblSection setText:@"Sites --touch to show--"]; else [lblSection setText:@"Sites --touch to hide--"]; } UIButton *btnCollapse = [UIButton buttonWithType:UIButtonTypeCustom]; [btnCollapse setFrame:CGRectMake(0, 0, _tblSearchResults.frame.size.width, 35)]; [btnCollapse setBackgroundColor:[UIColor clearColor]]; [btnCollapse addTarget:self action:@selector(touchedSection:) forControlEvents:UIControlEventTouchUpInside]; btnCollapse.tag = section; [headerView addSubview:lblSection]; [headerView addSubview:btnCollapse]; return headerView; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. if(section == 0) { if(customerIsCollapsed) return 0; else return _customerArray.count; } else if (section == 1) { if(siteIsCollapsed) return 0; else return _siteArray.count; } return 0; }

y finalmente la función a la que se llama cuando toca uno de los botones de encabezado de sección:

- (IBAction)touchedSection:(id)sender { UIButton *btnSection = (UIButton *)sender; if(btnSection.tag == 0) { NSLog(@"Touched Customers header"); if(!customerIsCollapsed) customerIsCollapsed = YES; else customerIsCollapsed = NO; } else if(btnSection.tag == 1) { NSLog(@"Touched Site header"); if(!siteIsCollapsed) siteIsCollapsed = YES; else siteIsCollapsed = NO; } [_tblSearchResults reloadData]; }


Tienes que hacer tu propia fila de encabezado personalizado y poner eso como la primera fila de cada sección. La subclasificación de UITableView o los encabezados que ya están allí será un problema. En función de la forma en que trabajan ahora, no estoy seguro de que pueda obtener acciones de ellos fácilmente. Puede configurar una celda para MIRAR como un encabezado y configurar tableView:didSelectRowAtIndexPath para expandir o colapsar manualmente la sección en la que se encuentra.

Guardaría una matriz de booleanos que corresponden al valor "gastado" de cada una de sus secciones. Entonces podría tener tableView:didSelectRowAtIndexPath en cada una de sus filas de encabezado personalizadas alternar este valor y luego volver a cargar esa sección específica.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { ///it''s the first row of any section so it would be your custom section header ///put in your code to toggle your boolean value here mybooleans[indexPath.section] = !mybooleans[indexPath.section]; ///reload this section [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]; } }

Luego configure numberOfRowsInSection para verificar el valor de mybooleans y devuelva 1 si la sección no está expandida, o 1+ la cantidad de elementos en la sección si se expande.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (mybooleans[section]) { ///we want the number of people plus the header cell return [self numberOfPeopleInGroup:section] + 1; } else { ///we just want the header cell return 1; } }

Además, deberá actualizar cellForRowAtIndexPath para devolver una celda de encabezado personalizada para la primera fila en cualquier sección.


Utilicé un NSDictionary como fuente de datos, esto parece mucho código, ¡pero es muy simple y funciona muy bien! como se ve aquí

Creé una enumeración para las secciones

typedef NS_ENUM(NSUInteger, TableViewSection) { TableViewSection0 = 0, TableViewSection1, TableViewSection2, TableViewSectionCount };

propiedad de secciones:

@property (nonatomic, strong) NSMutableDictionary * sectionsDisctionary;

Un método que devuelve mis secciones:

-(NSArray <NSNumber *> * )sections{ return @[@(TableViewSection0), @(TableViewSection1), @(TableViewSection2)]; }

Y luego configure mi sorue de datos:

-(void)loadAndSetupData{ self.sectionsDisctionary = [NSMutableDictionary dictionary]; NSArray * sections = [self sections]; for (NSNumber * section in sections) { NSArray * sectionObjects = [self objectsForSection:section.integerValue]; [self.sectionsDisctionary setObject:[NSMutableDictionary dictionaryWithDictionary:@{@"visible" : @YES, @"objects" : sectionObjects}] forKey:section]; } } -(NSArray *)objectsForSection:(NSInteger)section{ NSArray * objects; switch (section) { case TableViewSection0: objects = @[] // objects for section 0; break; case TableViewSection1: objects = @[] // objects for section 1; break; case TableViewSection2: objects = @[] // objects for section 2; break; default: break; } return objects; }

Los siguientes métodos lo ayudarán a saber cuándo se abre una sección y cómo responder al origen de datos de la tabla:

Responda la sección a la fuente de datos:

/** * Asks the delegate for a view object to display in the header of the specified section of the table view. * * @param tableView The table-view object asking for the view object. * @param section An index number identifying a section of tableView . * * @return A view object to be displayed in the header of section . */ - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ NSString * headerName = [self titleForSection:section]; YourCustomSectionHeaderClass * header = (YourCustomSectionHeaderClass *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:YourCustomSectionHeaderClassIdentifier]; [header setTag:section]; [header addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]]; header.title = headerName; header.collapsed = [self sectionIsOpened:section]; return header; } /** * Asks the data source to return the number of sections in the table view * * @param An object representing the table view requesting this information. * @return The number of sections in tableView. */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // Return the number of sections. return self.sectionsDisctionary.count; } /** * Tells the data source to return the number of rows in a given section of a table view * * @param tableView: The table-view object requesting this information. * @param section: An index number identifying a section in tableView. * @return The number of rows in section. */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ BOOL sectionOpened = [self sectionIsOpened:section]; return sectionOpened ? [[self objectsForSection:section] count] : 0; }

Herramientas:

/** Return the section at the given index @param index the index @return The section in the given index */ -(NSMutableDictionary *)sectionAtIndex:(NSInteger)index{ NSString * asectionKey = [self.sectionsDisctionary.allKeys objectAtIndex:index]; return [self.sectionsDisctionary objectForKey:asectionKey]; } /** Check if a section is currently opened @param section the section to check @return YES if is opened */ -(BOOL)sectionIsOpened:(NSInteger)section{ NSDictionary * asection = [self sectionAtIndex:section]; BOOL sectionOpened = [[asection objectForKey:@"visible"] boolValue]; return sectionOpened; } /** Handle the section tap @param tap the UITapGestureRecognizer */ - (void)handleTapGesture:(UITapGestureRecognizer*)tap{ NSInteger index = tap.view.tag; [self toggleSection:index]; }

Toggle section visibility

/** Switch the state of the section at the given section number @param section the section number */ -(void)toggleSection:(NSInteger)section{ if (index >= 0){ NSMutableDictionary * asection = [self sectionAtIndex:section]; [asection setObject:@(![self sectionIsOpened:section]) forKey:@"visible"]; [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade]; } }


Expanding on this answer written in Objective C, I wrote the following for those writing in Swift

The idea is to use sections within the table and set the number of rows in the section to 1 (collapsed) and 3(expanded) when the first row in that section is tapped

The table decides how many rows to draw based on an array of Boolean values

You''ll need to create two rows in storyboard and give them the reuse identifiers ''CollapsingRow'' and ''GroupHeading''

import UIKit class CollapsingTVC:UITableViewController{ var sectionVisibilityArray:[Bool]!// Array index corresponds to section in table override func viewDidLoad(){ super.viewDidLoad() sectionVisibilityArray = [false,false,false] } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) } override func numberOfSections(in tableView: UITableView) -> Int{ return sectionVisibilityArray.count } override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{ return 0 } // numberOfRowsInSection - Get count of entries override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { var rowsToShow:Int = 0 if(sectionVisibilityArray[section]){ rowsToShow = 3 // Or however many rows should be displayed in that section }else{ rowsToShow = 1 } return rowsToShow }// numberOfRowsInSection override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ if(indexPath.row == 0){ if(sectionVisibilityArray[indexPath.section]){ sectionVisibilityArray[indexPath.section] = false }else{ sectionVisibilityArray[indexPath.section] = true } self.tableView.reloadSections([indexPath.section], with: .automatic) } } // cellForRowAtIndexPath - Get table cell corresponding to this IndexPath override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell:UITableViewCell if(indexPath.row == 0){ cell = tableView.dequeueReusableCell(withIdentifier: "GroupHeading", for: indexPath as IndexPath) }else{ cell = tableView.dequeueReusableCell(withIdentifier: "CollapsingRow", for: indexPath as IndexPath) } return cell }// cellForRowAtIndexPath }


Some sample code for animating an expand/collapse action using a table view section header is provided by Apple at Table View Animations and Gestures .

The key to this approach is to implement

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

and return a custom UIView which includes a button (typically the same size as the header view itself). By subclassing UIView and using that for the header view (as this sample does), you can easily store additional data such as the section number.


// ------------------------------------------------------------------------------- // tableView:viewForHeaderInSection: // ------------------------------------------------------------------------------- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *mView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)]; [mView setBackgroundColor:[UIColor greenColor]]; UIImageView *logoView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 20, 20)]; [logoView setImage:[UIImage imageNamed:@"carat.png"]]; [mView addSubview:logoView]; UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom]; [bt setFrame:CGRectMake(0, 0, 150, 30)]; [bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [bt setTag:section]; [bt.titleLabel setFont:[UIFont systemFontOfSize:20]]; [bt.titleLabel setTextAlignment:NSTextAlignmentCenter]; [bt.titleLabel setTextColor:[UIColor blackColor]]; [bt setTitle: @"More Info" forState: UIControlStateNormal]; [bt addTarget:self action:@selector(addCell:) forControlEvents:UIControlEventTouchUpInside]; [mView addSubview:bt]; return mView; } #pragma mark - Suppose you want to hide/show section 2... then #pragma mark add or remove the section on toggle the section header for more info - (void)addCell:(UIButton *)bt{ // If section of more information if(bt.tag == 2) { // Initially more info is close, if more info is open if(ifOpen) { DLog(@"close More info"); // Set height of section heightOfSection = 0.0f; // Reset the parameter that more info is closed now ifOpen = NO; }else { // Set height of section heightOfSection = 45.0f; // Reset the parameter that more info is closed now DLog(@"open more info again"); ifOpen = YES; } //[self.tableView reloadData]; [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade]; } }// end addCell #pragma mark - #pragma mark What will be the height of the section, Make it dynamic - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 2) { return heightOfSection; }else { return 45.0f; }

// vKj


This action will happen in your didSelectRowAtIndexPath, when you will try to hide or show number of cell in a section first of all declare a global variable numberOfSectionInMoreInfo in .h file and in your viewDidLoad set suppose to numberOfSectionInMoreInfo = 4. Now use following logic: // More info link if(row == 3) { /*Logic: We are trying to hide/show the number of row into more information section */ NSString *log= [NSString stringWithFormat:@"Number of section in more %i",numberOfSectionInMoreInfo]; [objSpineCustomProtocol showAlertMessage:log]; // Check if the number of rows are open or close in view if(numberOfSectionInMoreInfo > 4) { // close the more info toggle numberOfSectionInMoreInfo = 4; }else { // Open more info toggle numberOfSectionInMoreInfo = 9; } //reload this section [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];

//vKj