numberOfRowsInSection:未在reloadData上调用

时间:2012-07-13 10:46:17

标签: iphone objective-c uitableview

我有一个使用数组列出数据的UITableView。这很好用。 我还有一个用于在该tableview中搜索的UISearchBar。当tableviews数组中的数据匹配时,这些行被添加到另一个可变数组中,而cellForRowAtIndexPath:则显示来自该可变数组的数据。

但是当我调用reloadData时,从不调用numberOfRowsInSection :.因此,滚动时tableview崩溃,因为它试图从可变数组中获取数据,但是对于原始数组中的行(具有更多项)

我已经调试了很长一段时间了,但不能为上帝的爱找到原因。数据源和代理连接在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;
}

9 个答案:

答案 0 :(得分:10)

请确保不要在任何表格视图的委托方法中调用reloadData。这将导致不可预测的行为。此外,请确保您之前没有致电beginUpdates,并且只从主线程中调用reloadData方法。

答案 1 :(得分:10)

如果numberOfSections返回0,则不会调用numberOfRowsInSection

答案 2 :(得分:5)

你怎么重装你的桌子? 做[table relaodData];对吧? 您的桌子是否已连接到笔尖上的IBOutlet? 在重新加载时,table被初始化了吗?

你也可以发布崩溃信息。

答案 3 :(得分:1)

我今天遇到了与XCode 6.1相同的问题

在我的代码中,我设置了dataSourcedelegate

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

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

...奇怪的是,我的代码会在第一次加载时填充表格...但它会忽略对reloadData的任何调用。

在故事板中,当我右键点击故事板中的表格视图时,它是"出口"单选按钮是一个空白圈。

我将这一空白圆圈中的一条线拖到父UIView上,然后 reloadData确实可以正常工作。

enter image description here

不知何故,我丢失了我的UITableView控件和用于填充它的.h / .m文件之间的IBOutlet链接。

答案 4 :(得分:1)

还要确保在重新加载数据的过程中没有移动表格,因为它可能会在您仍在更新数据时调用cellForRowAtIndexPath:

答案 5 :(得分:0)

对我来说问题是截断的节数返回0,即使我不打算使用节,也必须是1,因为它在对象层次结构中比行更高:{{3} }

答案 6 :(得分:0)

对我来说,问题是我试图增加一个值并且numberOfSectionsInTableView

返回它

即。 return someValue++

显然这是不可能的,在我的情况下,它没有将someValue从0递增到1,因此返回0个部分,因此numberOfRowsInSection:没有理由叫!

答案 7 :(得分:0)

对我来说,解决方案是我将其复制粘贴到另一个笔尖,却忘记了重新布线IB中的dataSourcedelegate插座。这以前发生在我身上,但我想我将其发布在这里,以防这种解决方案对遇到此问题的人有所帮助。

答案 8 :(得分:-2)

尝试

[self.tableView reloadData];
相关问题