2012-04-03 10 views
13

Sono nuovo di sviluppo di applicazioni iOS e Objective C in sé, quindi ho una domanda molto semplice probabilmente.Creazione di un tableView di programmazione con Objective-C iOS

Attualmente ho il seguente metodo che viene chiamato da un pulsante della barra degli strumenti clic. Il metodo è progettato per creare una vista tabella nella variabile di cornice fr.

- (IBAction)addGolfer:(id)sender { 
    CGRect fr = CGRectMake(101, 45, 100, 416); 

    UITableView *tabrleView = [[UITableView alloc] 
     initWithFrame:fr 
     style:UITableViewStylePlain]; 

    tabrleView.autoresizingMask = 
     UIViewAutoresizingFlexibleHeight | 
     UIViewAutoresizingFlexibleWidth; 
    tabrleView.delegate = self; 
    tabrleView.dataSource = self; 
    [tabrleView reloadData]; 

    self.view = tableView; 
} 

Il risultato di chiamare questo metodo non è quello che mi aspetto. Invece di creare la vista tabella nel frame "fr", la vista tabella riempie l'intero schermo.

Anche in questo caso io sono totalmente nuovo e sarebbe un apprezzare tutte le risposte e le eventuali suggerimenti. Grazie!

risposta

18

Quando si imposta dataSource e delegate proprietà per il vostro UITableView, significa, dovete scrivere almeno questo metodi per dataSource:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 

Se non si desidera fare questo, sarà crash.Sintesi si otterrà questo (questo codice potrebbe contenere errori di sintassi o logica - ho scritto in notepad):

@interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> { 
    UITableView *firstTableView; 
    UITableView *secondTableView; 
} 

@end 

//

@implementation YourViewController 

#pragma mark - Objects Processing 

- (void)addGolfer:(UIBarButtonItem *)sender { 
    if (secondTableView) { 
     [secondTableView removeFromSuperView]; 
     secondTableView = nil; 
    } 

    secondTableView = [[UITableView alloc] initWithFrame:CGRectMake(101, 45, 100, 416)]; 
    secondTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    secondTableView.delegate = self; 
    tabrleView.dataSource = self; 

    [self.view addSubview:secondTableView]; 
} 

#pragma mark - TableView DataSource Implementation 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == firstTableView) { // your tableView you had before 
     return 20; // or other number, that you want 
    } 
    else if (tableView == secondTableView) { 
     return 15; // or other number, that you want 
    } 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

    cell.backgroundView = [[UIView alloc] init]; 
    [cell.backgroundView setBackgroundColor:[UIColor clearColor]]; 
    [[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

    if (tableView == firstTableView) { // your tableView you had before 
     // ... 
    } 
    else if (tableView == secondTableView) { 
     cell.titleLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row + 1]; 
    } 

    return cell; 
} 

@end 
4

Invece di impostare la vista del UIViewController, aggiungere il tableView come una visualizzazione secondaria.

Invece di:

self.view = tableView; 

fare questo:

[self.view addSubview:tableView]; 

Ciò rispettare correttamente la cornice che hai impostato.

+0

ho fatto, ma ora non succede nulla. --- Scusa, ora ricevo un errore. –

+0

Potrebbe essere che tu mispasti 'tabrleView'. Dovrebbe essere 'tableView' ovunque. – DHamrick

+0

Sì, l'ho fatto perché avevo un altro TableView –

16

Fase 1: Aggiungi delegato UITableViewDataSource,UITableViewDelegate

@interface viewController: UIViewController<UITableViewDataSource,UITableViewDelegate> 
{ 
    UITableView *tableView; 
} 

Fase 2:

-(void)viewDidLoad 
{ 
    tableView=[[UITableView alloc]init]; 
    tableView.frame = CGRectMake(10,30,320,400); 
    tableView.dataSource=self; 
    tableView.delegate=self; 
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
    [tableView reloadData]; 
    [self.view addSubview:tableView]; 
} 

Fase 3: Proprietà in Tableview (righe & colonna)

// - Per nessuna di righe nella tabella

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 10; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

// - Tabella altezza intestazione se necessario

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 50; 
} 

// - Assegnare dati alle celle

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

    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text=[your_array objectAtIndex:indexPath.row]; ***(or)*** cell.textLabel.text = @"Hello"; 
    return cell; 
} 

// - Funzionamento quando le cellule tattili

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Your custom operation 
} 
Problemi correlati