簡體   English   中英

自動擴展UITableView - NSRangeException

[英]Automatically expand UITableView - NSRangeException

我有一個與我的uitableview綁定的SQLite數據庫。 大約有2000行,所以我首先檢索25行。 一旦我滾動到列表的末尾,我希望它自動檢索25行。

這是我現在使用的方法。 忽略如果我加載所有2000行會發生什么,我稍后會擔心。 tableView:numberOfRowsInSection:我返回的比當前加載的行多一個。 然后在tableView:cellForRowAtIndexPath:如果加載最后一行,我會加載更多行。

通常我可以下幾頁,但我最終得到一個例外: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (10) beyond bounds (10)' 指數總是8-10。 我不知道它是什么數組,但我的rowsindices數組都有超過10個項目。

tableView:cellForRowAtIndexPath:

if(indexPath.row >= [rows count]) {
    [cell.textLabel setText:@"Loading..."];
    if(!updating && indexPath.row == [rows count]) {
        updating = YES;
        [[self tableView] beginUpdates];
        NSMutableArray *indices = [NSMutableArray array];
        // stmt = ...
        while(sqlite3_step(stmt) == SQLITE_ROW) {
            [rows addObject:@"..."];
            [indices addObject:[NSIndexPath indexPathForRow:[rows count]-1 inSection:0]];
        }
        [[self tableView] insertRowsAtIndexPaths:indices withRowAnimation:UITableViewRowAnimationNone];
        [[self tableView] endUpdates];
        updating = NO;
    }
    return cell;
}

堆棧跟蹤:

#0  0x01ce4004 in ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___ ()
#1  0x9779df49 in objc_exception_throw ()
#2  0x01cc5c3b in +[NSException raise:format:arguments:] ()
#3  0x01cc5b9a in +[NSException raise:format:] ()
#4  0x00072cc9 in _NSArrayRaiseBoundException ()
#5  0x00010227 in -[NSCFArray objectAtIndex:] ()
#6  0x0047fe5b in -[_UITableViewUpdateSupport(Private) _setupAnimationsForExistingVisibleCells] ()
#7  0x0047f9e0 in -[_UITableViewUpdateSupport initWithTableView:updateItems:oldRowData:newRowData:oldRowRange:newRowRange:context:] ()
#8  0x002eca2b in -[UITableView(_UITableViewPrivate) _updateWithItems:withOldRowData:oldRowRange:newRowRange:context:] ()
#9  0x002ec757 in -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] ()
#10 0x002def56 in -[UITableView endUpdates] ()    

我有點不清楚為什么要插入行。 在我看來,你使這個變得比它需要的復雜得多。 UITableView只會詢問您屏幕上(或很快將會)的表格單元格的值。 更重要的是,通過不斷向表視圖添加行,滾動條不會反映用戶在數據集中的實際位置。

返回tableView:numberOfRowsInSection:的總行數同樣有意義tableView:numberOfRowsInSection:您可以使用SQLite COUNT聚合函數執行此操作而不實際獲取所有數據)並根據需要緩存其他數據(使用類似於當你遇到“緩存未命中”時,你有什么)。

但是,如果我不得不猜測為什么這段代碼會導致您出現問題,那么當表視圖等待您返回單元格時,您將修改表視圖的內容。

我會建議看起來更像這樣的東西:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] >= [cache count]) {
        // Load additional items into the cache
    }

    // Do the regular cell setup stuff here; you can assume the data you need is available
    return cell;
}

亞歷克斯,我接受你的答案,雖然我仍然對導致實際異常的原因感興趣。

你在返回單元格時更新tableview的注釋促使我實現- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath在需要時加載更多行,這樣更穩定雖然我必須在一個單獨的線程中這樣做才能獲得任何性能。

您的解決方案也有效,並且肯定比我當前的方法更簡單。 我將不得不決定是否希望滾動條顯示實際位置或更有形的位置。 至少對我而言,感覺應用程序正在加載許多對象,因此當滾動條太小時,它會變慢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM