没有cellidentifier ios的cellForRowIndexPath

时间:2013-09-18 12:33:36

标签: ios uitableview

我的项目中没有xib文件或故事板。在这种情况下,如何设置单元标识符?以下是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"test");
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [self.entries objectAtIndex:indexPath.row];
    return cell;
}

2 个答案:

答案 0 :(得分:5)

尝试用此替换代码(使用ARC):

- (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.textLabel.text = [self.entries objectAtIndex:indexPath.row];

    return cell;
}

++

答案 1 :(得分:0)

如果您只是在表视图中使用UITableViewCell,则可以在viewDidLoad中注册该类,如下所示:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

在cellForRowAtIndexPath中使用相同的标识符:您在此处使用。