2014-04-17 2 views
1

J'utilise une cellule personnalisée dans mon UITableView, où je reçois des valeurs des objets que j'ajouter à cette cellule comme ceci:Attention - types de pointeurs incompatibles, comment supprimer?

NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow]; 
    StuardReportCustomCell *cell = [_ReportTableView cellForRowAtIndexPath:indexPath]; 
    NSString *text = cell.MyLabelInCell.text; 

Tout travail bien mais j'ai un avertissement (ce qui est seulement 1 avertissement tout mon application) et je veux supprimer cet avertissement:

incompatible pointer types initializing StuardReportCustomCell with an expression of type 
UITableviewCell 

fichier de StuardReportCustomCell

#import <UIKit/UIKit.h> 

@interface StuardReportCustomCell : UITableViewCell 

@end 

C'est cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

StuardReportCustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if(!Cell){ 
    Cell = [[StuardReportCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

Cell.lbNum.text = [lbNum objectAtIndex:indexPath.row]; 
Cell.lbRouteNum.text = [lbRouteNum objectAtIndex:indexPath.row]; 
Cell.lbTime.text = [lbTime objectAtIndex:indexPath.row]; 
Cell.lbCity.text = [lbCity objectAtIndex:indexPath.row]; 
Cell.lbCameIn.text = [lbCameIn objectAtIndex:indexPath.row]; 
Cell.lbIn.text = [lbIn objectAtIndex:indexPath.row]; 
Cell.lbOut.text = [lbOut objectAtIndex:indexPath.row]; 
Cell.lbCameOut.text = [lbCameOut objectAtIndex:indexPath.row]; 

return Cell; 
} 

Solution:

StuardReportCustomCell *cell = (StuardReportCustomCell *)[_ReportTableView  cellForRowAtIndexPath:indexPath]; 

ou

NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow]; 
NSString *text = [lbTime objectAtIndex:indexPath.row]; 
+2

Il suffit de taper jeté 'StuardReportCustomCell * cellule = (StuardReportCustomCell *) [_ ReportTableView cellForRowAtIndexPath: indexPath];' –

+0

@ MidhunMP Cela réglerait l'avertissement, mais il cacherait le vrai problème. – dasblinkenlight

+0

@dasblinkenlight Où dans la question de l'OP at-il indiqué qu'il utilisait la réutilisation de cellules? – CrimsonChris

Répondre

1

Vous pouvez vous débarrasser de l'avertissement en ajoutant un casting. Cependant, vous ne devriez pas lire le texte de la cellule, car cela mélange les niveaux du Model-View-Controller: vous devriez obtenir la valeur du modèle à la place.

Par conséquent, au lieu d'écrire, par exemple,

NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow]; 
StuardReportCustomCell *cell = (StuardReportCustomCell*)[_ReportTableView cellForRowAtIndexPath:indexPath]; 
NSString *text = cell.lbTime.text; 

vous devriez écrire

NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow]; 
NSString *text = [lbTime objectAtIndex:indexPath.row]; 
+0

Merci de travailler aussi. –

Questions connexes