【问题标题】:How to set up a cell of fetchedResultsController driven tableView when tableView is empty?tableView 为空时如何设置一个 fetchedResultsController 驱动的 tableView 单元格?
【发布时间】:2011-10-20 08:41:16
【问题描述】:

您好,我有一个 tableView,其中 fetchResultsController 是数据源。 当添加一个对象时,它将它添加到 tableView,并且当对象被删除时,tableView 也会适当地适应。一切正常。 但是 tableView 为空时如何设置单元格? 假设单元格应该说“按下刷新按钮”。一旦有要显示的对象,这个单元格当然应该消失。

我的 tableView 设置如下:

 // Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
        return [[self.fetchedResultsController sections] count];
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];
 }


 // Customize the appearance of table view cells.
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
 }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo name];
    }

【问题讨论】:

    标签: iphone objective-c tableview nsfetchedresultscontroller


    【解决方案1】:

    您能否在返回数据源大小之前检查您的数据源在 numberOfRowsInSection 中是否为空,以及是否向sectionInfo 添加一个表示“刷新”单元格的元素?但是,一旦获得有效数据,您就必须删除该单元格/

    当你的数据源为空时向你的 tableView 添加一个“刷新”子视图可能会更干净,但当它有 > 1 个元素时将其删除。

    【讨论】:

      【解决方案2】:

      您可以尝试以下方法。

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
       {
              id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
      
              if ([sectionInfo numberOfObjects] == 0) {
                    return 1;
              }
              return [sectionInfo numberOfObjects];
       }
      
       // Customize the appearance of table view cells.
       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
       {
          static NSString *CellIdentifier = @"Cell";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
              cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
          }
      
          // Configure the cell.
      
          id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; // get the list of items. Do it yourself
      
      if ([sectionInfo numberOfObjects] == 0) {
          [cell.textLabel setText:@"Press the refresh button"];
      }
      else {
          [self configureCell:cell atIndexPath:indexPath];
      }
          return cell;
       }
      

      NSIndexPath *indexPath =  [[NSIndexPath alloc] indexPathForRow:0 inSection:0];  
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
      [cell.textLabel setText:@"Some Text"];  //Configure it with the first object you get
      


      [listOfObjects removeAllObjects] // Remove all objects - set list count to 0.
      [tableView reloadData];
      

      【讨论】:

      • 我已经尝试过完美的工作,但它永远不会消失!当其他对象进来时,它们不会显示出来。
      • 获取新数据时是否重新加载TableView?如果这也不起作用,那么当您获得新数据时,答案中的代码 sn-ps。
      • 不,我不重新加载表格视图!数据在 NSFetchedResultsController 委托中通过插入、删除、....进行更新。
      猜你喜欢
      • 2020-08-17
      • 2019-11-03
      • 1970-01-01
      • 2016-10-19
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      相关资源
      最近更新 更多