如何正确使用带有多个标识符的“dequeueReusableCellWithIdentifier:”?

时间:2011-11-07 20:12:07

标签: ios core-data uitableview

我有一个Core Data支持的表视图,它显示了可编辑字段的列表,对于不同的字段类型,我有不同的UTableViewCell具有不同的单元标识符。当我在模拟器中滚动得太快或试图“弹跳”过去的最后一个单元格时,我发生了崩溃,说 UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath 。如果我删除dequeueReusableCellWithIdentifier:步骤,整个问题就消失了。这意味着我的表视图效率较低。在我的表视图中,我最多使用20个获取的对象(更多的是8-10行),因此效率低下可能是一个小问题。我只是想知道我是否采取了错误的做法。

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    Field *aField = [self.fetchedResultsController objectAtIndexPath:indexPath];
    static NSString *CellIdentifier = @"Cell";
    static NSString *ChoiceIdentifier = @"ChoiceCell";
    static NSString *SwitchIdentifier = @"SwitchCell";

    UITableViewCell *cell;

    if ([aField.fieldType isEqualToString:@"choice"] || [aField.fieldType isEqualToString:@"date"]  || [aField.fieldType isEqualToString:@"multiChoice"] ) {

        NSLog(@"ChoiceCell");
        cell = [tableView dequeueReusableCellWithIdentifier:ChoiceIdentifier];
        if (cell == nil) {
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                [[NSBundle mainBundle] loadNibNamed:@"ChoiceTableViewCell-iPad" owner:self options:nil];
            } else {
                [[NSBundle mainBundle] loadNibNamed:@"ChoiceTableViewCell" owner:self options:nil];
            }
        }

    } else if ([aField.fieldType isEqualToString:@"boolean"]){

        NSLog(@"SwitchCell");
        cell = [tableView dequeueReusableCellWithIdentifier:SwitchIdentifier];
        if (cell == nil) {

            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                [[NSBundle mainBundle] loadNibNamed:@"SwitchTableViewCell-iPad" owner:self options:nil];
            } else {

                [[NSBundle mainBundle] loadNibNamed:@"SwitchTableViewCell" owner:self options:nil];
            }
        }


    } else {

        NSLog(@"Cell");
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                [[NSBundle mainBundle] loadNibNamed:@"EditableTableViewCell-iPad" owner:self options:nil];
            } else {

                [[NSBundle mainBundle] loadNibNamed:@"EditableTableViewCell" owner:self options:nil];
            }
        }
    } 

    cell = editableCell;
    self.editableCell = nil;


    // Configure the cell...
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

其他详细信息:editableCell的设置与每个与自定义单元格对应的NIB相同。我试着通过说

直接设置这个
dynamicCell = [[[NSBundle mainBundle] loadNibNamed:@"ChoiceTableViewCell-iPad" owner:self options:nil] objectAtIndex:0];

但仍有同样的问题。它应该永远不会返回零。只要我不滚动得太快,就会加载所有NIB。我已经对NIB名称进行了两次和三次检查以确保。

以下是更新的工作代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

Field *aField = [self.fetchedResultsController objectAtIndexPath:indexPath];
static NSString *CellIdentifier = @"Cell";
static NSString *ChoiceIdentifier = @"ChoiceCell";
static NSString *SwitchIdentifier = @"SwitchCell";


if ([aField.fieldType isEqualToString:@"choice"] || [aField.fieldType isEqualToString:@"date"]  || [aField.fieldType isEqualToString:@"multiChoice"] ) {

    NSLog(@"ChoiceCell");
    dynamicCell = [tableView dequeueReusableCellWithIdentifier:ChoiceIdentifier];
    if (dynamicCell == nil) {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {


            [[NSBundle mainBundle] loadNibNamed:@"ChoiceTableViewCell-iPad" owner:self options:nil];
        } else {
            [[NSBundle mainBundle] loadNibNamed:@"ChoiceTableViewCell" owner:self options:nil];
        }
    }

} else if ([aField.fieldType isEqualToString:@"boolean"]){

    NSLog(@"SwitchCell");
    dynamicCell = [tableView dequeueReusableCellWithIdentifier:SwitchIdentifier];
    if (dynamicCell == nil) {

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            [[NSBundle mainBundle] loadNibNamed:@"SwitchTableViewCell-iPad" owner:self options:nil];
        } else {

            [[NSBundle mainBundle] loadNibNamed:@"SwitchTableViewCell" owner:self options:nil];
        }
    }


} else {

    NSLog(@"Cell");
    dynamicCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (dynamicCell == nil) {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            [[NSBundle mainBundle] loadNibNamed:@"EditableTableViewCell-iPad" owner:self options:nil];
        } else {

            [[NSBundle mainBundle] loadNibNamed:@"EditableTableViewCell" owner:self options:nil];
        }
    }
} 

UITableViewCell *cell;
cell = dynamicCell;
self.dynamicCell = nil;


// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;

}

1 个答案:

答案 0 :(得分:4)

看起来问题出在:

cell = editableCell

如果editableCell为零则您的应用会崩溃。我假设您打算editableCell设置loadNibNamed:。如果您将单元格出列,则不会设置。