EXC_BAD_ACCESS Сбой в UITableView, когда количество строк равно 0 - PullRequest
4 голосов
/ 29 декабря 2010

У меня постоянно происходит сбой с моим 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, сбой не появляется.

Ответы [ 2 ]

3 голосов
/ 30 декабря 2010

Оказывается, я забыл вернуть ячейку таблицы в предыдущей функции инициализации. Все исправлено.

0 голосов
/ 29 декабря 2010

ну это потому что

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

не может вернуть 0 Это потому, что UITableview не может нарисовать пустую таблицу без ячейки

так

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

должен сделать свое дело.

...