UITableView显示,但单元格中没有任何名称

时间:2012-07-27 20:39:53

标签: ios ipad uitableview

为什么我在iPad上的UITableView中看不到任何文字?

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];

    if(tableView != nil)
        [tableView reloadData];
}

- (void)viewDidLoad {

    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024) style:UITableViewStylePlain];
    tableView.delegate = self;
    [self.view addSubview:tableView];
}

- (void)doneButtonSelected:(id)sender {
    [self dismissModalViewControllerAnimated:NO];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
        return 1;
    else if(section == 1)
        return 2;

    return 0;
}

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

    UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    int section = indexPath.section, row = indexPath.row;
    if(section == 0) {
        if(row == 0)
            cell.textLabel.text = @"YAY";
    }
    else if(section == 1) {
        if(row == 0)
            cell.textLabel.text = @"BLACH";
        else if(row == 1)
            cell.textLabel.text = @"GAH";
    }

    return cell;
}

- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [theTableView deselectRowAtIndexPath:indexPath animated:YES];

    int section = indexPath.section, row = indexPath.row;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if(section == 0)
        return @"Blah";
    else if(section == 1)
        return @"Hi";

    return @"Hello!";
}


@end

1 个答案:

答案 0 :(得分:2)

tableView.dataSource = self; ?