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

这使我相信错误出现在我的 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上。

这是我得到的错误。
*** 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
)**

最佳答案

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

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

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

10-10 09:16