簡體   English   中英

numberOfRowsInSection:未在 reloadData 上調用

[英]numberOfRowsInSection: not called on reloadData

我有一個使用數組列出數據的UITableView 這很好用。 我還有一個UISearchBar用於在該tableview中進行搜索。 當數據在 tableviews 數組中匹配時,這些行被添加到另一個可變數組,而cellForRowAtIndexPath:顯示來自該可變數組的數據。

但是當我調用 reloadData 時, numberOfRowsInSection:永遠不會被調用。 因此, tableview在滾動時崩潰,因為它試圖從可變數組中獲取數據,但對於原始數組中的行(包含更多項)

我已經調試了一段時間了,但老天爺還是找不到原因。 Datasource 和 Delegate 連接在tableview中,因為它可以顯示原始數組中的數據。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
         NSLog(@"isSearching: %d", isSearching);
         // Return the number of rows in the section.
         if(!isSearching)
            return [originalArray count];

         NSLog(@"isSearching - Return searchCopyListOfItems");
         return [searchCopyListOfItems count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         static NSString *CellIdentifier = @"Cell";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

         if(cell == nil) {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
         }

         if(!searching)
             cell.textLabel.text = [originalArray objectAtIndex:indexPath.row];
         else
             cell.textLabel.text = [searchCopyListOfItems objectAtIndex:indexPath.row];

         return cell;
    }

如果numberOfSections返回0 ,則不會調用numberOfRowsInSection

確保不要從任何表視圖的委托方法中調用reloadData 這將導致不可預知的行為。 另外,確保您之前沒有調用beginUpdates ,並且您只從主線程調用reloadData方法。

你如何重新加載你的表? [table relaodData]; 正確的? 你的桌子連接到你筆尖上的 IBOutlet 了嗎? 在重新加載時, table是否已初始化?

您也可以發布崩潰信息。

我今天在 XCode 6.1 上遇到了同樣的問題

在我的代碼中,我設置了dataSource源和delegate

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

...奇怪的是,我的代碼在第一次加載時填充表...但它隨后忽略了對reloadData的任何調用。

在 Storyboard 中,當我右鍵單擊 Storyboard 中的 Table View 時,它的“outlet”單選按鈕是一個空心圓圈。

我從這個空圓圈中拖了一條線到父UIView然后reloadData就可以正常工作了。

在此處輸入圖像描述

不知何故,我丟失了 UITableView 控件和用於填充它的 .h/.m 文件之間的IBOutlet鏈接。

還要確保您在重新加載數據的過程中沒有移動表,因為它可能會調用cellForRowAtIndexPath:而您仍在更新數據。

對我來說,問題是截斷的節數返回 0,即使我不打算使用節,也必須是 1,因為它在對象層次結構中比行更高: https ://stackoverflow.com /a/26632808/1449799

對我來說,問題是我試圖增加一個值numberOfSectionsInTableView返回它

return someValue++

顯然這是不可能的,在我的例子中,它沒有將someValue從 0 遞增到 1,因此返回 0 個部分,因此沒有理由調用numberOfRowsInSection:

我的解決方案是我從另一個 nib 復制粘貼它,忘記重新連接 IB 中的dataSourcedelegate插座。 這以前發生在我身上,但我想我會把它貼在這里,以防這個解決方案幫助一些遇到這個問題的人。

嘗試

[self.tableView reloadData];

暫無
暫無

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

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