2009-12-01 23 views
0

我試圖創建並繪製由數組填充的UITableView。我創建了一個UIViewController並將UIViewController設置爲我的tableview的委託。但是,似乎我的cellForRowAtIndexPath方法不會在創建tableview時調用,所以我的表永遠不會被填充。我在這裏錯過了什麼? (所有標籤都在該函數中初始化,因爲我只是想用表格創建一個主菜單,所有的條目都將被修復)。iPhone編程cellForRowAtIndexPath即使在委託集之後也不會調用

// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 

prefTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; 

[prefTable setDelegate:self]; 

//NSIndexSet *tmpIndRange = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0,1)]; 

//[prefTable insertSections:tmpIndRange withRowAnimation:UITableViewRowAnimationNone]; 

//[tmpIndRange release]; 

labelArray = [[NSMutableArray alloc] initWithCapacity:10]; 
[labelArray addObject:NSLocalizedString(@"Account ID",@"C_ACC_ID")]; 
[labelArray addObject:NSLocalizedString(@"Site ID",@"Site_ID")]; 
[labelArray addObject:NSLocalizedString(@"Station ID",@"Station_ID")]; 
[labelArray addObject:NSLocalizedString(@"Encryption Key",@"ENC_KEY")]; 
[labelArray addObject:NSLocalizedString(@"Encryption On",@"ENC_ON")]; 

placeholderArray = [[NSMutableArray alloc] initWithCapacity:10]; 
[labelArray addObject:NSLocalizedString(@"Required",@"Required")]; 
[labelArray addObject:NSLocalizedString(@"Required",@"Required")]; 
[labelArray addObject:NSLocalizedString(@"Required",@"Required")]; 
[labelArray addObject:NSLocalizedString(@"Required",@"Required")]; 
[labelArray addObject:NSLocalizedString(@"Required",@"Required")]; 

self.view = prefTable; 
} 

-(BOOL)textFieldShouldReturn:(UITextField *)textField { 

    NSInteger currenttag = textField.tag; 

    NSLog(@"%d",textField.tag); 

    //accountId = textField.text; 
    //stationId = textField.text; 
    //siteId = textField.text; 

    [textField resignFirstResponder]; 

    return YES; 
} 

- (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] autorelease]; 
    } 

    // Set up the cell... 

    [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    [cell.textLabel setText:[labelArray objectAtIndex:indexPath.row]]; 
    //((TextInputTableCell *)cell).textField.placeholder = [placeholderArray objectAtIndex:indexPath.row]; 

    return cell;  
} 

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 4; 
} 

另外,如果我使用數據源分配菜單項,會更好嗎?看來只能使用數據源分配節標題。謝謝!

回答

3

tableView:cellForRowAtIndexPath:是UITableView的一部分DataSource協議 - 不是UITableViewDelegate。你需要讓你的控制器成爲UITableView的數據源:

prefTable.dataSource = self; 
+0

是的......我認爲就是這樣。 – futureelite7

相關問題