2014-11-05 10 views
7

Sto lavorando a un progetto che sia iOS7 e iOS8. E mi sono imbattuto in questo bug su iOS8, in cui il tableview "salta" quando si seleziona un oggetto.Errore UITableView in iOS8 quando si selezionano elementi con stimaHeight diverso da rowHeight

Questo errore è presente solo quando è impostata l'altezza stimata e differisce dall'altezza della riga.

Ecco due gif dello stesso codice in iOS8 e iOS7. Prima iOS 8:

TableView in iOS8

E ora in iOS7:

TableView in iOS7

ho fatto un progetto di esempio che mostra il bug qui: https://github.com/bjarkehs/TableViewiOS8Bug

Non sono sicuro se sono semplicemente manca qualcosa, ma sono bloccato con questo problema e non sono stato in grado di trovare nulla su questo.

Qui sono i miei metodi tableView:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 3; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == 1) { 
     return 30; 
    } 
    return 20; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"%@ %ld", @"Wat", indexPath.row]; 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 45.f; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 50.f; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.navigationController pushViewController:[UIViewController new] animated:YES]; 
} 

Ecco un esempio viewcontroller dove l'altezza calcolata non è codificato, che mostra lo stesso problema:

#import "ViewController.h" 

@interface ViewController() 

@property (weak, nonatomic) IBOutlet UITableView *tableView; 

@end 

@implementation ViewController { 
    NSMutableArray *_items; 
    CGFloat _estimatedHeight; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 

    _estimatedHeight = 70.f; 

    CGFloat randomOffset = 30.f; 

    _items = [NSMutableArray new]; 
    for (NSInteger i = 0; i < 3; i++) { 
     NSMutableArray *tempItems = [NSMutableArray new]; 
     for (NSInteger j = 0; j < 30; j++) { 
      NSInteger offset = arc4random_uniform(randomOffset); 
      [tempItems addObject:@(_estimatedHeight + (randomOffset/2) - offset)]; 
     } 
     [_items addObject:tempItems]; 
    } 
    // Do any additional setup after loading the view. 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 3; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == 1) { 
     return 30; 
    } 
    return 20; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"%@ %ld", @"Wat", indexPath.row]; 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return _estimatedHeight; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return [[[_items objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] floatValue]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.navigationController pushViewController:[UIViewController new] animated:YES]; 
} 

@end 
+0

Potrebbe essere necessario inviare i vostri metodi Tableview qui. – latenitecoder

+0

Sì, potrei pure. I metodi tableview ci sono ora. –

+0

Forse sono ritardato ma non riesco a vedere alcuna differenza tra 7 e 8. Qual è il problema esatto? –

risposta

-1

L'implementazione di altezza stimata per A Indice non è qualcosa che ho visto prima. Date un'occhiata a questo, dove ho particolareggiate utilizzando questo schema ...

Estimated Height Example

+0

Questo è solo un progetto di esempio. Sto semplicemente mostrando il caso più semplice, in cui l'altezza stimata non è uguale all'altezza effettiva della cella. L'altezza effettiva può essere qualsiasi cosa, diciamo da 40 a 60 pixel, ma implementarla nel progetto di esempio dovrebbe solo aggiungere più complessità senza aggiungere all'esempio. –

+0

Non sei sicuro di cosa intendi, ma l'altezza di stima funziona sui principi che crei una cella di 'prototipo' e le sue proprietà di sottoview (etichette, posizioni, ecc.) Ottengono l'altezza. Il tuo esempio ha valori hardcoded che non danno alcun vantaggio reale, rendendo il tavolo strano. – latenitecoder

+0

Per di più hai hardcoded l'altezza che annulla solo i calcoli stimati – latenitecoder

Problemi correlati