EXC_BAD_ACCESS 当行数为​​ 0 时 UITableView 崩溃

发布于 2024-10-10 00:42:25 字数 3108 浏览 0 评论 0原文

当我将表中的行数设置为零时,我的 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,则不会出现崩溃。

I am having a consistent crash with my UITableView when I set the number of rows in the table to zero. It crashes with an EXC_BAD_ACCESS error. The crash is internal to the UITableView, so I cannot directly see what went wrong, though it should be a stupid mistake on my part.

The stack trace is as follows:

#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

And my code is as follows:

- (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;
}

The crash only happens when the number of rows in the table is 0. If I hardcode the number of rows returned to 1, the crash does not appear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-10-17 00:42:25

结果我忘记在之前的初始化函数中返回一个表格单元格。现在全部修好了。

Turns out that I forgot to return a table cell in a previous initialization function. All fixed now.

南笙 2024-10-17 00:42:25

那是因为

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

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

因此

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

应该可以解决问题。

well that's because

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

cannot return 0
That's because UITableview cannot draw an empty table without any cell

so

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

should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文