使用自定义单元格而不是默认单元格时出现问题

时间:2013-05-15 19:24:34

标签: ios

我在这里有一个简单的项目https://github.com/spikepoolie/tracker.git,其中一个用户名和密码“你可以输入任何值”的视图没有验证,点击LOGON按钮后,我想带我到tableview,它但它没有使用我创建的自定义单元格。

有人可以看看项目,让我知道我错过了什么

由于 罗德里戈

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CellCustomLoadCell";
    CellCustomLoadCell *cell = (CellCustomLoadCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellCustomLoadCell" owner:self options:nil];
        cell = (CellCustomLoadCell *)[nib objectAtIndex:0];
    }

    cell.loadNumber.text=@"rodrigo";
    return cell;
}

1 个答案:

答案 0 :(得分:0)

你在表视图控制器中唯一能做的就是:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"CellCustomLoadCell" bundle:nil] forCellReuseIdentifier:@"CellCustomLoadCell"];
}


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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellCustomLoadCell";
    CellCustomLoadCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.loadNumber.text=@"rodrigo";
    return cell;
}

在cellForRowAtIndexPath方法之外注册nib是使用基于nib的单元格的新方法。

ViewController类中也存在一个问题,即如何在屏幕上显示表视图控制器。您不应该只将其视图添加为视图的子视图。您可以使用推送或模式演示移动到下一个控制器,或者只是更改窗口的根视图控制器,这样(此代码在您的ViewController类中,我已注释掉您添加的行子视图):

- (IBAction)login:(id)sender {
    RodrigoTableViewController *test = [[RodrigoTableViewController alloc] initWithNibName:@"RodrigoTableViewController" bundle:nil];
    //[self.view addSubview:test.view];

    self.view.window.rootViewController = test;
}