2011-02-15 11 views
16

necesito colocar a través de todas las células en un TableView y permite establecer una imagen para cell.imageView cuando se presiona un botón. Estoy tratando de obtener cada celda por¿Cómo encontrar el número de células en UITableView

[[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 

Pero necesito el recuento de las celdas.

¿Cómo encontrar el recuento de células en TableView?

Respuesta

15

el recuento total de todas las células (en una sección) debe ser lo está siendo devuelto por

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

sin embargo este método es cada recuento, puede hacerlo en sus propios métodos también. Probablemente algo así como return [myArrayofItems count];

+0

¿Por qué no presentar el código para swift3 también? – user44776

6

UITableView está concebida solamente como una manera de ver sus datos, tomados de la fuente de datos. El número total de celdas es una información que pertenece a la fuente de datos y debe acceder a ella desde allí. UITableView mantiene suficientes células para ajustarse a la pantalla que se puede acceder utilizando

- (NSArray *)visibleCells

Una solución sucia sería mantener una matriz separada de cada UITableViewCell se crea. Funciona, y si tienes pocas células no es tan malo.

Sin embargo, esto no es una solución muy elegante y personalmente no elegiría esto a menos que no hay absolutamente ninguna otra manera. Es mejor que no modifique las celdas reales en la tabla sin un cambio correspondiente en la fuente de datos.

41
int sections = [tableView numberOfSections]; 

int rows = 0; 

for(int i=0; i < sections; i++) 
{ 
    rows += [tableView numberOfRowsInSection:i]; 
} 

Número total de filas = filas;

4

basado en el código de Biranchi, aquí hay un pequeño fragmento que recupera todos a través de la célula. ¡Espero que esto te ayude!

UITableView *tableview = self.tView; //set your tableview here 
int sectionCount = [tableview numberOfSections]; 
for(int sectionI=0; sectionI < sectionCount; sectionI++) { 
    int rowCount = [tableview numberOfRowsInSection:sectionI]; 
    NSLog(@"sectionCount:%i rowCount:%i", sectionCount, rowCount); 
    for (int rowsI=0; rowsI < rowCount; rowsI++) { 
     UITableViewCell *cell = (UITableViewCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowsI inSection:sectionI]]; 
     NSLog(@"%@", cell); 
    } 
} 
+0

gracias que es definitivamente cierto, pero como se discutió anteriormente ... las tablas de vista se cargan siempre desde una fuente de datos ... digamos una matriz ... ¡¡entonces es mucho más fácil encontrar esta cuenta !! – sujith1406

0

Swift 3 ejemplo equivalente

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     if section == 0 { 
      return 1 
     }else if section == 1 {  
      return timesArray.count // This returns the cells equivalent to the number of items in the array. 
     } 
     return 0 
    } 
1

Swift 3,1(Como del 13 de julio 2017)

let sections: Int = tableView.numberOfSections 
var rows: Int = 0 

for i in 0..<sections { 
    rows += tableView.numberOfRows(inSection: i) 
} 
0

extensión para UITableView para obtener el número total de filas . Escrito en Swift 4

extension UITableView { 

    var rowsCount: Int { 
     let sections = self.numberOfSections 
     var rows = 0 

     for i in 0...sections - 1 { 
      rows += self.numberOfRows(inSection: i) 
     } 

     return rows 
    } 
} 
Cuestiones relacionadas