ios - 由于未捕获的异常,子类化多个 UITableViewCells 终止应用程序

标签 ios objective-c uitableview

我有一个 UITableView,我正在用我创建的多个子类 UITableViewCells 填充它。所有子类 UITableViewCells 都可以正常工作,但是当 UITableView 中有足够的单元格来填充整个 View 时,当我滚动时它会崩溃。

这让我相信错误出在我的 tableView:tableViewcellForRowAtIndexPath 中,但我不确定。

这就是我为 tableView:tableViewcellForRowAtIndexPath 编写的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GetSeriesMacsDictionary *currentMac = [[GetSeriesMacsDictionary alloc] init];
    currentMac = [recMacArray objectAtIndex:indexPath.row];

    if (([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:5]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:7]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:13]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:14]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:17]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:19]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:32]])) {
        static NSString *CellIdentifier = @"Cell";

        cellSingle = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cellSingle == nil) {
            cellSingle = [[MacDataSingleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cellSingle.nameLabel.text = MacStringForCell;
        return cellSingle;
    }
    else if (([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:1]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:2]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:20]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:21]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:28]])) {

        static NSString *CellIdentifier = @"Cell";

        cellSihma = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cellSihma == nil) {
            cellSihma = [[MacDataSihmaTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cellSihma.nameLabel.text = MacStringForCell;
        cellSihma.SihmaLabelLabel.text = cardDictionary.SihmaCardNumber;
        return cellSihma;
    }
    else if (([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:3]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:4]])) {

        static NSString *CellIdentifier = @"Cell";

        cellHP = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cellHP == nil) {
            cellHP = [[MacDataHPTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cellHP.nameLabel.text = MacStringForCell;
        cellHP.HPLabelLabel.text = cardDictionary.HPCardNumber;
        return cellHP;
    }
    else if ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:11]]) {

        static NSString *CellIdentifier = @"Cell";

        cellHPB = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cellHPB == nil) {
            cellHPB = [[MacDataHPBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cellHPB.nameLabel.text = MacStringForCell;
        cellHPB.HPBLabelLabel.text = cardDictionary.hPCCardNumber;
        return cellHPB;
    }
    else if ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:12]]) {

        static NSString *CellIdentifier = @"Cell";

        cellHPP = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cellHPP == nil) {
            cellHPP = [[MacDataHPPTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cellHPP.nameLabel.text = MacStringForCell;
        cellHPP.HPPLabelLabel.text = cardDictionary.HPPCardNumber;
        return cellHPP;
    }
    else if (([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:22]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:23]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:29]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:30]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:31]])) {

        static NSString *CellIdentifier = @"Cell";

        cellInco = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cellInco == nil) {
            cellInco = [[MacDataIncoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cellInco.nameLabel.text = MacStringForCell;
        cellInco.IncoLabelLabel.text = cardDictionary.iCCardNumber;
        return cellInco;

    }
    else if (([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:16]]) || ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:27]])) {

        static NSString *CellIdentifier = @"Cell";

        cellSin = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cellSin == nil) {
            cellSin = [[MacDataSinTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cellSin.nameLabel.text = MacStringForCell;
        cellSin.SinLabelLabel.text = [NSString stringWithFormat:@"%d", seriesDictionary.Sin];
        cellSin.SihmaLabelLabel.text = cardDictionary.SihmaCardNumber;
        return cellSin;
    }
    else if ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:8]]) {
        static NSString *CellIdentifier = @"Cell";

        cellAqe = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cellAqe == nil) {
            cellAqe = [[MacDataAqeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cellAqe.nameLabel.text = MacStringForCell;
        cellAqe.qKitLabelLabel.text = currentMac.MacMiscDescription1;
        cellAqe.pcLabelLabel.text = currentMac.MacMiscDescription2;
        return cellAqe;
    }
    else if ([currentMac.Mac isEqualToNumber:[NSNumber numberWithInt:9]]) {
        static NSString *CellIdentifier = @"Cell";

        cellCue = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cellCue == nil) {
            cellCue = [[MacDataCueTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cellCue.nameLabel.text = MacStringForCell;
        cellCue.CueCamSetLabelLabel.text = currentMac.MacMiscDescription1;
        cellCue.CueCarriageLabelLabel.text = currentMac.MacMiscDescription2;
        cellCue.CueCutLabelLabel.text = currentMac.MacMiscDescription3;
        return cellCue;

    }
    else {
        static NSString *CellIdentifier = @"Cell";

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

        NSString *MacStringForCell = [self convertMacName:currentMac];

        cell.textLabel.text = MacStringForCell;
        return cell;

    }
}

发生的事情是,当 cardDictionary.(myvar) 被传递给任何具有 (name)LabelLabel.text< 的东西时,当滚动 tableview 时第一个新单元格进入 tablebiew/strong> 我的应用程序崩溃了。

这是我遇到的错误。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MacDataHPTableViewCell HPLabelLabel]: unrecognized selector sent to instance 0x16d53990'
*** First throw call stack:
(0x27b3ec1f 0x35316c8b 0x27b44039 0x27b41f57 0x27a73df8 0x1819fd 0x2b2c38c7 0x2b2c398b 0x2b2b90e1 0x2b0d12ff 0x2affb4d7 0x2aa23a0d 0x2aa1f3e5 0x2aa1f26d 0x2aa1ec51 0x2aa1ea55 0x2aa1892d 0x27b053b5 0x27b02a73 0x27b02e7b 0x27a51211 0x27a51023 0x2ee0a0a9 0x2b05d1d1 0x6e01d 0x35896aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是堆栈跟踪。

**<_NSCallStackArray 0x16fde6f0>(
0   ???                                 0x04734a38 0x0 + 74664504,
1   CodeBrealer                         0x0006dfa9 main + 0,
2   UIKit                               0x2b2c38c7 <redacted> + 474,
3   UIKit                               0x2b2c398b <redacted> + 54,
4   UIKit                               0x2b2b90e1 <redacted> + 2268,
5   UIKit                               0x2b0d12ff <redacted> + 186,
6   UIKit                               0x2affb4d7 <redacted> + 514,
7   QuartzCore                          0x2aa23a0d <redacted> + 136,
8   QuartzCore                          0x2aa1f3e5 <redacted> + 360,
9   QuartzCore                          0x2aa1f26d <redacted> + 16,
10  QuartzCore                          0x2aa1ec51 <redacted> + 224,
11  QuartzCore                          0x2aa1ea55 <redacted> + 324,
12  QuartzCore                          0x2aa1892d <redacted> + 56,
13  CoreFoundation                      0x27b053b5 <redacted> + 20,
14  CoreFoundation                      0x27b02a73 <redacted> + 278,
15  CoreFoundation                      0x27b02e7b <redacted> + 914,
16  CoreFoundation                      0x27a51211 CFRunLoopRunSpecific + 476,
17  CoreFoundation                      0x27a51023 CFRunLoopRunInMode + 106,
18  GraphicsServices                    0x2ee0a0a9 GSEventRunModal + 136,
19  UIKit                               0x2b05d1d1 UIApplicationMain + 1440,
20  CodeBrealer                         0x0006e01d main + 116,
21  libdyld.dylib                       0x35896aaf <redacted> + 2
)**

最佳答案

问题是您对不同的单元格类型使用相同的单元格重用标识符“单元格”。这意味着当您出列一个单元格以供重用时,您可能无法获得所需的单元格类型 - 在崩溃的情况下,您在需要 MacDataHPBTableViewCell 时出列了 MacDataHPTableViewCell - 当您尝试访问对象不具有的属性时,导致无法识别的选择器崩溃。

您需要为每种单元格类型使用唯一的单元格重用字符串。

关于ios - 由于未捕获的异常,子类化多个 UITableViewCells 终止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27027269/

相关文章:

ios - 如何创建 CoreData 状态的快照?

objective-c - 发布两个 NSNotifications 时的执行顺序

ios - 如何创建带边框的圆形图像(UIGraphics)?

objective-c - 如何使用 Objective-C 列出垃圾箱的内容(在 Mac 中)

ios - 调整 View 的高度以包含 TableView 的行数

ios - _shouldShowMenuForCell 中意外的零索引路径

ios - NavigationBar 标题为黑色

ios - 从 TableView 中选择时在多个详细 View 之间切换

ios - 对两个项目使用水平滚动时右对齐 UICollectionView(对于 RTL)

ios - 在 tableViewCell 中添加 tableView