2012-05-08 11 views

Respuesta

-6

Se podría tratar de conseguir UITableViewCell por:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
// returns nil if cell is not visible or index path is out of range 

aquí está el código completo:

UITableViewCell *cell = [cellForRowAtIndexPath:appDelegate.currentMainIndexPath]; 

if (appDelegate.currentMainIndexPath != nil && cell !=nil) 
{ 
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
    appDelegate.currentMainIndexPath = nil; 
} 
+16

¿Por qué es esta la respuesta aceptada? El código ni siquiera es válido Objective-C e incluso si corrige los errores de compilación, aún estaría utilizando un método que devuelve 'nil' si la celda no está visible, lo que significa que no puede desplazarse a algo que no es Ya está en la pantalla y el objetivo del desplazamiento es hacer visible algo que no está visible. –

2

Si usted quiere decir, 'no es una célula en el índice n', entonces sólo hay que comparar el tamaño de su dataSource a n

if (appDelegate.currentMainIndexPath != nil [datasource count] > n) 
{ 
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
    appDelegate.currentMainIndexPath = nil; 
} 

Donde datasource es, por ejemplo, un NSArray.

0

También puede utilizar el método de UITableView numberOfRowsInSection.

1

Esta es la forma de hacerlo:

indexPath.row < [self.tableView numberOfRowsInSection:indexPath.section] 

En contexto:

if (indexPath.row < [self.tableView numberOfRowsInSection:indexPath.section]) { 
    [self.tableView scrollToRowAtIndexPath:indexPath 
          atScrollPosition:UITableViewScrollPositionTop 
            animated:YES]; 
} 

insertado en su código:

if (appDelegate.currentMainIndexPath != nil && 
    indexPath.row < [tblView numberOfRowsInSection:indexPath.section]) { 
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath 
        atScrollPosition:UITableViewScrollPositionTop 
          animated:NO]; 
    appDelegate.currentMainIndexPath = nil; 
} 
+2

Si la sección es incorrecta, '-numberOfRowsInSection:' devolverá 'NSNotFound', por lo que debe verificarlo también. O mejor, compruebe 'indexPath.section <[self.tableView numberOfSections]', porque 'NSNotFound' no está documentado. – kpower

26

Se puede usar esta. Pasarlo fila y la sección de indexpath

-(BOOL) isRowPresentInTableView:(int)row withSection:(int)section 
{ 
    if(section < [self.tableView numberOfSections]) 
    { 
     if(row < [self.tableView numberOfRowsInSection:section]) 
     { 
      return YES; 
     } 
    } 
    return NO; 
} 
+1

Use NSUInteger o la fila> = 0 && section> = 0 para filtrar algunos casos falsos. –

15

Una rápida adaptación de la respuesta de Kamran Khan:

extension UITableView { 
    func hasRowAtIndexPath(indexPath: NSIndexPath) -> Bool { 
    return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRowsInSection(indexPath.section) 
    } 
} 

Swift 4:

extension UITableView { 
    func hasRow(at indexPath: IndexPath) -> Bool { 
     return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section) 
    } 
} 
+0

Aparentemente parece una respuesta válida –

+0

Esta debería ser la respuesta aceptada para Swift 3+. Gracias por la adaptación – Brian

3

Hay un método más conveniente de saber si una indexPath es válido:

para Swift 3.0:

open func rectForRow(at indexPath: IndexPath) -> CGRect 

Para Objective-C

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath; 

Usted recibirá CGRectZero si el indexPath no es válido.

func isIndexPathValid(indexPath: IndexPath) -> Bool { 
    return !tableView.rectForRow(at: indexPath).equalTo(CGRect.zero) 
} 
Cuestiones relacionadas