2013-05-21 2 views
0

В моем проекте xcode у меня есть view с tableview. У меня также есть «специальная» строка, которая содержит объекты для заполнения массива, который заполняет таблицу. Пример:cellForRowAtIndexPath не работает

NSString *myValues=[[NSString alloc]init]; 
myValues = @"aaa$bbb$ccc?111$222$333?"; 

, как вы можете видеть символы $ и? отделить объекты. Когда я вызываю представление, я заполняю массив строкой, но tableview не работает. Вот некоторые код:

file.h:

#import <UIKit/UIKit.h> 

@interface EquipaggioVC : UIViewController <UITableViewDelegate, UITableViewDataSource>{ 
    UITableView *tableViewEquipaggio; 
    UITableViewCell *nibLoadedCell; 

    NSMutableArray *arrayEquipaggio; 

    NSString *stringaDati; 
} 
@property (nonatomic, retain) IBOutlet UITableView *tableViewEquipaggio; 
@property (nonatomic, retain) IBOutlet UITableViewCell *nibLoadedCell; 
@property (nonatomic, retain) NSMutableArray *arrayEquipaggio; 
@property (nonatomic, retain) NSString *stringaDati; 

@end 

File.M:

#import "EquipaggioVC.h" 
#import "AppDelegate.h" 
#import "ClassEquipaggio.h" 
@interface EquipaggioVC() 

@end 

@implementation EquipaggioVC 
@synthesize tableViewEquipaggio; 
@synthesize nibLoadedCell; 
@synthesize arrayEquipaggio; 
@synthesize stringaDati; 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

-(NSString *)uuid 
{ 
    //to create an ID 
    CFUUIDRef uuidRef = CFUUIDCreate(NULL); 
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef); 
    CFRelease(uuidRef); 
    return [(NSString *)uuidStringRef autorelease]; 
} 
-(void)loadTestData:(NSString*)stringaDatiArray{ 

//populate the array by the string. 
    if(stringaDatiArray!=nil){ 
     NSArray *elenco=[stringaDatiArray componentsSeparatedByString:@"?"]; 

     for (int i=0; i<([elenco count]-1) ; i++) { 

      NSString *dettagliEquipaggio=[[NSString alloc]init]; 

      dettagliEquipaggio=[elenco objectAtIndex:i]; 
      NSArray *dettagli=[dettagliEquipaggio componentsSeparatedByString:@"$"]; 
      ClassEquipaggio *aEquipaggio=[[ClassEquipaggio alloc]init]; 
      aEquipaggio.idMembro=[dettagli objectAtIndex:0]; 
      aEquipaggio.sessoMembro=[dettagli objectAtIndex:1]; 
      aEquipaggio.dataNascitaMembro=[dettagli objectAtIndex:2]; 

      [arrayEquipaggio addObject:aEquipaggio]; 
      [aEquipaggio release]; 

     } 
    } 

} 


- (UITableViewCell *)tableView:(UITableView *)tableEquipaggioView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 
    static NSString *sLoadNibNamed; 
    UITableViewCell *cell; 


    ClassEquipaggio *aEquipaggio=[arrayEquipaggio objectAtIndex:indexPath.row]; 
    cell = [tableEquipaggioView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    [email protected]"EquipaggioCell"; 


    if (cell == nil) { 

     [[NSBundle mainBundle] loadNibNamed:sLoadNibNamed owner:self options:NULL]; 
     cell = [self typeNibLoadCell:aEquipaggio.idMembro]; 
    } 

    // Configure the cell 
    UILabel *tipoSessoLabel = (UILabel*) [cell viewWithTag:1]; 
    tipoSessoLabel.text = aEquipaggio.sessoMembro; 

    UILabel *dataNascitaLabel=(UILabel*)[cell viewWithTag:2]; 
    dataNascitaLabel.text=aEquipaggio.dataNascitaMembro; 

    return cell; 
} 




-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [arrayEquipaggio count]; 
} 
-(UITableViewCell *)typeNibLoadCell:(NSString *)named{ 
    return nibLoadedCell; 
} 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return NO; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    arrayEquipaggio=[[NSMutableArray alloc]init]; 
    stringaDati=[[NSString alloc]init]; 
    tableViewEquipaggio=[[UITableView alloc]init]; 

} 


-(void)viewDidAppear:(BOOL)animated{ 
    AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; 

    UIBarButtonItem * btnIndietroa = [[[UIBarButtonItem alloc] initWithTitle:@"Indietro" 
                     style:UIBarButtonItemStyleBordered 
                     target:self 
                     action:@selector(editNavButtonPressed)]autorelease]; 

    [self.navigationItem setLeftBarButtonItem:btnIndietroa]; 
    stringaDati = [[NSUserDefaults standardUserDefaults] objectForKey:@"stringaDati"]; 

    if(stringaDati!=nil){ 

     [arrayEquipaggio removeAllObjects]; 
     [self loadTestData:stringaDati]; 
    } 

} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(void)dealloc{ 
    [super dealloc]; 
} 
@end 

На бобах файле TableView имеет связь с delegate и datasource, как IBOutlet связано с Это.

Спасибо за вашу поддержку.

EDIT: [arrayEquipaggio count] возвращение 0.

+0

Пожалуйста, проверьте значение '[arrayEquipaggio count]' Я имею в виду количество строк. –

+0

Два вопроса: вы пробовали определить строку следующим образом: «myValues ​​= @» aaa $ bbb $ ccc? 111 $ 222 $ 333? "; >>. Я думаю, вы пропустили @ ", определяя его. И второе: какое числоOfRowsInSection возвращается? –

+0

Что вы имеете в виду, не работает ..? –

ответ

0

Судя по вашему вопросу, ваша проблема в том, что метод cellForRowAtIndexPath не вызывается и, как вы сказали, в [arrayEquipaggio count] возвращение 0.

Поскольку вы используете этот массив по методу numberOfRowsInSection с возвращаемым значением 0, это нормально, что ваша таблица не вызывает метод cellForRowAtIndexPath. Поскольку вы говорите своей таблице, что строк нет.

Если вы хотите, чтобы ваша таблица была заполнена, проверьте, чтобы ваш массив возвращал значение больше 0.

В двух словах У него нет данных для заполнения таблицы, поэтому она не работает.

Убедитесь, что в вашем viewDidAppear:(BOOL)animated что вы тянете что-нибудь до stringaDati.

NSLog(@"info: %@",stringaDati); 
Смежные вопросы