即使在委托设置之后,iPhone编程cellForRowAtIndexPath也不会被调用

时间:2009-12-01 05:04:00

标签: iphone objective-c iphone-sdk-3.0

我正在尝试创建并绘制由数组填充的UITableView。我已经创建了一个UIViewController并将UIViewController设置为我的tableview的委托。但是,在创建tableview时似乎没有调用我的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;
}

另外,如果我使用数据源分配菜单项会更好吗?似乎只能使用数据源分配节标题。谢谢!

1 个答案:

答案 0 :(得分:3)

tableView:cellForRowAtIndexPath:是UITableView DataSource 协议的一部分 - 而不是UITableViewDelegate。您需要将控制器作为UITableView的数据源:

prefTable.dataSource = self;