【问题标题】:Problems with UITableViewDelegateUITableViewDelegate 的问题
【发布时间】:2011-10-20 17:16:00
【问题描述】:

我有一个包含多个部分的 UITableView。我有一个 NSMutableArray 作为数据源(floorArray)。当我重新加载 tableview 数据时,我得到[FloorGroup count]: unrecognized selector sent to instance 0x5c18fc0。我的 ViewController 实现了 UITableViewDelegate 和 UITableViewDataSource。我在 FloorGroup 对象上调用 count 时看不到任何地方。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [floorArray count];

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    FloorGroup *group = [floorArray objectAtIndex:section];
    return group.floorName;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    FloorGroup *group = [floorArray objectAtIndex:section];
    NSMutableArray *areasOnFloor = group.areas;
    NSUInteger count = [areasOnFloor count];
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FloorGroup *group = [floorArray objectAtIndex:[indexPath section]];

    static NSString *identity = @"MainCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];

    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 0.0) reuseIdentifier:identity] autorelease];
    }
    cell.text = [group.areas objectAtIndex:indexPath.row];

    return cell;
}

我的班级定义是:

@interface FloorGroup : NSObject {
    NSString *floorName;
    NSMutableArray *areas;
}

@interface FloorArea : NSObject {
    NSString *floor;
    NSString *description;
    NSDecimalNumber *area;
    NSDecimalNumber *price;
}

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

确切的错误是:

2011-08-05 10:54:58.692 com.Lime49.myApp[1987:207] -[FloorGroup count]: unrecognized selector sent to instance 0x5d93c90
2011-08-05 10:54:58.694 com.Lime49.myApp[1987:207] NSInvalidArgumentException
2011-08-05 10:55:02.321 com.Lime49.myApp[1987:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FloorGroup count]: unrecognized selector sent to instance 0x5d93c90'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x0139f5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x014f3313 objc_exception_throw + 44
    2   CoreFoundation                      0x013a10bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x01310966 ___forwarding___ + 966
    4   CoreFoundation                      0x01310522 _CF_forwarding_prep_0 + 50
    5   UIKit                               0x006492b7 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
    6   UIKit                               0x00646d88 -[UITableViewRowData numberOfRows] + 108
    7   UIKit                               0x004fa677 -[UITableView noteNumberOfRowsChanged] + 132
    8   UIKit                               0x00507708 -[UITableView reloadData] + 773
    9   Foundation                          0x0020b94e __NSThreadPerformPerform + 251
    10  CoreFoundation                      0x013808ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    11  CoreFoundation                      0x012de88b __CFRunLoopDoSources0 + 571
    12  CoreFoundation                      0x012ddd86 __CFRunLoopRun + 470
    13  CoreFoundation                      0x012dd840 CFRunLoopRunSpecific + 208
    14  CoreFoundation                      0x012dd761 CFRunLoopRunInMode + 97
    15  GraphicsServices                    0x01b3c1c4 GSEventRunModal + 217
    16  GraphicsServices                    0x01b3c289 GSEventRun + 115
    17  UIKit                               0x0049ac93 UIApplicationMain + 1160
    18  com.Lime49.myApp                0x00002256 main + 84
    19  com.Lime49.myApp                0x000021f9 start + 53
)
terminate called after throwing an instance of 'NSException'

【问题讨论】:

  • 在数组和分配数组中添加对象的位置给出该代码。也在哪个类中。

标签: iphone objective-c ios xcode uitableview


【解决方案1】:

您的错误看起来像您的对象具有类 FloorGroup 而不是 floorArray 的指针。

您的数组已经被释放并且它的内存被 FloorGroup 对象占用。调试你的 floorArray 是否有对象。

【讨论】:

  • 从main.m抛出异常,当我在numberOfRowsInSection中调试时, floorArray 已经加载并包含四个对象,然后我得到了异常。
  • 能否控制到达 titleForHeaderInSection 或在此之前您的应用崩溃?
  • 给你的数组初始化和对象添加的地方(代码)
【解决方案2】:

你在哪里初始化'floorArray'?

尝试在 init 中而不是在 viewDidLoad 中执行(视图尝试加载数组,并在视图尝试加载后初始化......明白了吗?)

【讨论】:

  • 这是有道理的,但是我在viewDidLoad 中打了一个断点,然后是numberOfRowsInSectiontitleForHeaderInSection,所有这些都被调用了。但是,cellForRowAtIndexPath 不是
  • 仍然很奇怪,因为如果 NSMutableArray 未初始化,则计数仅返回 0。因此无论如何都不会显示任何对象。看起来调用是在其他东西上调用的,而不是数组,也许是一个类? (也许你有一个重复的名字?,尝试重命名你用来保存表数据的 NSMutableArray 女巫)。编辑:我没有初始化 floorArray 对象吗?
【解决方案3】:

检查你的 floorArray 可能已经发布了。

【讨论】:

    【解决方案4】:

    一个问题的痛苦。我的 DAL 中有大约 20 行代码来将 XML 元素树解析为 Objective-C 对象。我正在创建 FloorGroups,将它们添加到字典中,然后在最后循环并将值转换为 NSMutableDictionary。我真的不明白为什么编译器甚至让我这样做或为什么它运行,但我只需要跳过最后一个循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      相关资源
      最近更新 更多