ios - 如何正确使用具有多个标识符的 "dequeueReusableCellWithIdentifier:"?

标签 ios core-data uitableview

我有一个核心数据支持的 TableView ,它显示了一个可编辑字段列表,对于不同的字段类型,我有不同的 UTableViewCells 和不同的单元格标识符。当我在模拟器中滚动得太快或试图“弹”过最后一个单元格时,我会崩溃,提示 UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath。如果我删除 dequeueReusableCellWithIdentifier: 步骤,整个问题就会消失。这意味着我的 TableView 效率较低。我在我的 TableView 中最多使用 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;

最佳答案

看起来问题在于:

cell = editableCell

如果 editableCell 为 nil,您的应用将崩溃。我假设您打算通过 loadNibNamed: 设置 editableCell。如果您使单元格出队,则不会设置它。

关于ios - 如何正确使用具有多个标识符的 "dequeueReusableCellWithIdentifier:"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8042007/

相关文章:

ios - CoreData 并发问题

ios - tableView 中的索引超出范围

ios - 自定义 UITableViewCell 在触摸时突出显示

ios - Swift核心数据NSFetchRequest谓词可获取日期时间

ios - 将 map 图钉保存到数组,Swift

ios - 多个 segues 到一个 UIViewController

iphone - 从 NSDictionary 按排序顺序显示 JSON 字符串

ios - 如何在 iOS 中的 TableView 中实现下拉菜单

iphone - 加载到 UIScrollview 中的图像尺寸错误

ios - 我可以创建一个按钮来启动 iOS 中的语音听写模式吗?