2012-05-08 21 views

risposta

-6

Si potrebbe cercare di ottenere UITableViewCell da:

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

ecco il codice completo:

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

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

Perché questa è la risposta accettata? Il codice non è nemmeno valido Objective-C e, anche se correggi gli errori di compilazione, staresti ancora usando un metodo che restituisce 'nil' se la cella non è visibile, il che significa che non puoi scorrere fino a qualcosa che non è t già sullo schermo e l'intero punto di scorrimento è rendere visibile qualcosa che non è visibile. –

2

Se vuoi dire, 'c'è una cellula in corrispondenza dell'indice n' allora non resta che confrontare le dimensioni di voi DataSource n

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

Dove origine dati è ad esempio un NSArray.

0

È inoltre possibile utilizzare il metodo di UITableView numberOfRowsInSection.

1

Ecco come si fa:

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

Nel contesto:

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

inserito nel tuo codice:

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

Se la sezione è sbagliata, '-numberOfRowsInSection:' restituirà 'NSNotFound', quindi dovresti controllarlo anche. O meglio controllare 'indexPath.section <[self.tableView numberOfSections]', perché 'NSNotFound' non è documentato. – kpower

26

è possibile utilizzare questo. Passarla riga e la sezione di indexpath

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

Usa NSUInteger o riga> = 0 && sezione> = 0 per filtrare alcuni casi falsi. –

15

Un adattamento Swift della risposta di 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

Apparentemente sembra una risposta valida –

+0

Questa dovrebbe essere la risposta accettata per Swift 3+. Grazie per l'adattamento. – Brian

3

C'è un metodo più conveniente per dire se una indexPath è valido:

Per Swift 3.0:

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

Per Objective-C

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

Otterrete CGRectZero se l'indexPath non è valido.

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