如何设置fetchedResultsController驱动表的单元格当tableView为空时查看?

时间:2011-10-20 08:41:16

标签: iphone objective-c tableview nsfetchedresultscontroller

您好我有一个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];
    }

2 个答案:

答案 0 :(得分:0)

您可以在numberOfRowsInSection中检查数据源是否为空,是否在返回数据源大小之前向sectionInfo添加代表'refresh'单元的元素?一旦通过/

获得有效数据,就必须删除该单元格

当您的dataSource为空时,向tableView添加“刷新”子视图可能会更清晰,并且当它具有&gt;时将其删除。但是有一个元素。

答案 1 :(得分:0)

您可以尝试以下操作。

- (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];