【问题标题】:EXC_BAD_ACCESS Crash in UITableView when number of rows is 0行数为 0 时 UITableView 中的 EXC_BAD_ACCESS 崩溃
【发布时间】:2010-12-30 09:30:32
【问题描述】:

当我将表中的行数设置为零时,我的 UITableView 始终出现崩溃。它因 EXC_BAD_ACCESS 错误而崩溃。崩溃是 UITableView 内部的,所以我无法直接看到出了什么问题,尽管这对我来说应该是一个愚蠢的错误。

堆栈跟踪如下:

#0  0x0194ca60 in objc_msgSend ()
#1  0x00656837 in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] ()
#2  0x0064c77f in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] ()
#3  0x00661450 in -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] ()
#4  0x00659538 in -[UITableView layoutSubviews] ()
#5  0x00d39451 in -[CALayer layoutSublayers] ()
#6  0x00d3917c in CALayerLayoutIfNeeded ()
#7  0x00d3237c in CA::Context::commit_transaction ()
#8  0x00d320d0 in CA::Transaction::commit ()
#9  0x00d627d5 in CA::Transaction::observer_callback ()
#10 0x013a3fbb in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#11 0x013390e7 in __CFRunLoopDoObservers ()
#12 0x01301bd7 in __CFRunLoopRun ()
#13 0x01301240 in CFRunLoopRunSpecific ()
#14 0x01301161 in CFRunLoopRunInMode ()
#15 0x01d42268 in GSEventRunModal ()
#16 0x01d4232d in GSEventRun ()
#17 0x005f142e in UIApplicationMain ()
#18 0x000518ec in main (argc=1, argv=0xbfffef84) at /Users/megahub/xcode/QuamSec/main.m:15

而我的代码如下:

- (id)initWithFrame:(CGRect)frame {

        self = [super initWithFrame:frame];
        if (self) {

            m_oPositionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStylePlain];
            m_oPositionTableView.delegate = self;
            m_oPositionTableView.dataSource = self;
            m_oPositionTableView.separatorStyle =  UITableViewCellSeparatorStyleNone;

            [self addSubview:m_oPositionTableView];

            m_oAppDelegate = (AyersGTSAppDelegate *)[[UIApplication sharedApplication] delegate];

        }
        return self;
    }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (m_oPositionItems == nil)
        return 0;
    else
        return [m_oPositionItems count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"QuamPortfolioPositionCell";

    QuamPortfolioPositionCell *cell = (QuamPortfolioPositionCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QuamPortfolioPositionCell" owner:self options:nil] lastObject];
    }

    // Configure the cell...
    SPPositionItem *oPositionItem = [m_oPositionItems objectAtIndex:indexPath.row];
    cell.oSymbol.text = oPositionItem.sProdCode;
    cell.oMktPrice.text = oPositionItem.sMktPrice;
    cell.oNet.text = oPositionItem.sNet;
    cell.oAmount.text = oPositionItem.sMktVal;

    return cell;
}

只有当表中的行数为 0 时才会发生崩溃。如果我将返回的行数硬编码为 1,则不会出现崩溃。

【问题讨论】:

  • 开启NSZombieEnabled,然后你也许可以找出访问了哪个被释放的对象。
  • 原来我忘记在之前的初始化函数中返回一个表格单元格。现在都修好了。
  • 你应该把它写成你的答案,然后自己接受。这样,问题将被标记为已回答,其他搜索的人可能更容易找到您的解决方案,并有一个 duh 时刻并自己修复它。回答你自己的问题并接受这个答案是完全可以接受的(你没有得到任何代表;)

标签: iphone objective-c uitableview exc-bad-access


【解决方案1】:

原来我忘记在之前的初始化函数中返回一个表格单元格。现在都修好了。

【讨论】:

  • 在编译应用程序时不应该出现警告吗?
【解决方案2】:

那是因为

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

不能返回 0 那是因为 UITableview 无法绘制没有任何单元格的空表

所以

    if (m_oPositionItems == nil)
    return 1;
else
    return [m_oPositionItems count];

应该可以解决问题。

【讨论】:

  • 完全不正确。您绝对可以在一个部分中有 0 行,在一个表中有 0 个元素。她/他还有其他一些记忆问题,鉴于我们拥有的上下文数量,我们无法真正推断出这些问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 2013-03-24
  • 2010-11-18
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多