2012-06-29 9 views
15

Questo è il mio problema: ho questo piccolo UITableView nel mio storyboard: enter image description hereSet UITableView Delegato e DataSource

E questo è il mio codice:

SmallTableViewController.h

#import <UIKit/UIKit.h> 
#import "SmallTable.h" 

@interface SmallViewController : UIViewController 

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

@end 

SmallTableViewController.m

#import "SmallViewController.h" 

@interface SmallViewController() 

@end 

@implementation SmallViewController 
@synthesize myTable = _myTable; 

- (void)viewDidLoad 
{ 
    SmallTable *myTableDelegate = [[SmallTable alloc] init]; 
    [super viewDidLoad]; 
    [self.myTable setDelegate:myTableDelegate]; 
    [self.myTable setDataSource:myTableDelegate]; 

    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 

Ora come puoi vedere, voglio impostare un'istanza chiamata myTableDelegate come delegato e DataSource di myTable.

Questa è la sorgente della classe SmallTable.

SmallTable.h

#import <Foundation/Foundation.h> 

@interface SmallTable : NSObject <UITableViewDelegate , UITableViewDataSource> 

@end 

SmallTable.m

@implementation SmallTable 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 0; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return 5; 
} 

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

    // Configure the cell... 
    cell.textLabel.text = @"Hello there!"; 

    return cell; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Row pressed!!"); 
} 

@end 

ho implementato tutto il metodo UITableViewDelegate e UITableViewDataSource che la necessità app. Perché si blocca prima che appaia la vista ??

Grazie !!

+0

Potresti incollare l'errore? – JonLOo

+0

Puoi aggiungere anche i crash log? – rishi

+0

Controlla la discussione nella discussione - http://stackoverflow.com/questions/254354/uitableview-issue-when-using-separate-delegate-datasource – rishi

risposta

15

rickster ha ragione. Ma suppongo che tu debba usare un qualificatore strong per la tua proprietà dato che alla fine del tuo metodo viewDidLoad l'oggetto sarà comunque deallocato.

@property (strong,nonatomic) SmallTable *delegate; 

// inside viewDidload 

[super viewDidLoad]; 
self.delegate = [[SmallTable alloc] init];  
[self.myTable setDelegate:myTableDelegate]; 
[self.myTable setDataSource:myTableDelegate]; 

Ma c'è qualche motivo per utilizzare un oggetto separato (origine dati e delegato) per la tabella? Perché non imposti SmallViewController come sorgente e delegato per la tua tabella?

Inoltre, non si sta creando la cella nel modo corretto. Queste linee non fanno nulla:

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

// Configure the cell... 
cell.textLabel.text = @"Hello there!"; 

dequeueReusableCellWithIdentifier semplicemente recupera dalla tabella "cache" una cellula che ha già creato e che possono essere riutilizzati (questo per evitare il consumo di memoria), ma non hanno creato alcuna.

Dove stai andando alloc-init?Fate questo invece:

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(!cell) { 
    cell = // alloc-init here 
} 
// Configure the cell... 
cell.textLabel.text = @"Hello there!"; 

dicono inoltre a numberOfSectionsInTableView per restituire 1 invece di 0:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 
4

Presumibilmente stai usando ARC? Il tuo myTableDelegate viene fatto riferimento solo in una variabile locale in viewDidLoad - una volta terminato, il metodo viene deallocato. (Nel modello delegato/origine dati, gli oggetti non possiedono i loro delegati, quindi i riferimenti della vista tabella all'oggetto sono deboli.) Non mi aspetto che da solo causi un arresto anomalo, ma è probabilmente la chiave del tuo problema.

+0

OK, ho appena creato un nuovo delegato @property (weak, nonatomic) SmallTable *; Ora l'applicazione non si blocca, ma ... la vista tabella è vuota! Non riesco a capire perché ... –

1

setDelegate non manterrà il delegato.

E

numberOfSectionsInTableView metodo deve restituire 1 invece di 0;

0

Il delegato di un oggetto UITableView deve adottare il protocollo UITableViewDelegate. I metodi facoltativi del protocollo consentono al delegato di gestire selezioni, configurare intestazioni e piè di pagina, aiutare a eliminare i metodi.

enter image description here

1
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 0; 
} 

Numero di sezioni deve essere impostato almeno un

Problemi correlati