0

I have a TTTableView (_commentsItem) embedded in another TTTableView like so:

   self.dataSource = [DetailItemDataSource dataSourceWithObjects:
                       @"", 
                       self.imageItem,
                       @"", 
                       findItem, 
                       @"",
                       _descriptionItem,
                       @"",
                       self.shareItem,
                       self.editItem,
                       @"",
                       _commentsItem,
                       @"",
                       _addCommentButtonItem,
                       nil];

When _commentsItem has rows, everything works great. I can even add rows to the table in _commentsItem, call [.. refresh], and the enclosing table will adjust itself accordingly.

The problem occurs when _commentsItem is empty. Normally, you'd expect to get a TTErrorView overlaying the whole screen, but I get nothing (actually, I don't even want it to overlay the whole screen, I'd just like to get the "emptyTitle" to show up.

I think the problem is in TTTableView's "ShowEmpty" which is creating another view to contain the TTErrorView, which the enclosing table knows nothing about:

- (void)showEmpty:(BOOL)show {
  if (show) {
    NSString* title = [_dataSource titleForEmpty];
    NSString* subtitle = [_dataSource subtitleForEmpty];
    UIImage* image = [_dataSource imageForEmpty];
    if (title.length || subtitle.length || image) {
    TTErrorView* errorView = [[[TTErrorView alloc] initWithTitle:title
                                                      subtitle:subtitle
                                                         image:image] autorelease];
    errorView.backgroundColor = _tableView.backgroundColor;
    self.emptyView = errorView;

  } else {
    self.emptyView = nil;
  }
  _tableView.dataSource = nil;
  [_tableView reloadData];

  } else {
    self.emptyView = nil;
  }

}

I suspect that showEmpty isn't really intended to work correctly when it's TTTableView is embedded in another TTTableView, but then the question becomes how to do this. The enclosing table expects _commentsItem to be a TTTableViewItem, so I can't just swap _commentsItem's view for, say a UILabel.

So the question is: When you have a TTTableView embedded in another TTTableView, what is the best way to display a "Table Empty" state for the embedded table?

Thanks!.

Michael Wilson
  • 800
  • 8
  • 15

1 Answers1

0

Here's what I ended up doing to resolve this:

  1. When creating the enclosing TTTableView, I decided if I wanted to embed a table view (TTTableView) when comments were present, or a simple empty message [TTTableTextItem itemWithText:@"No Comments Yet!"];

  2. Assigned the TTTableView or TTTableTextItem item to TTTableItem * (superclass of TTTableView and TTTableTextItem) called _commentsItem

  3. Built the enclosing table:

    self.dataSource = [DetailItemDataSource dataSourceWithObjects: @"", self.imageItem, @"", findItem, @"", _descriptionItem, @"", self.shareItem, self.editItem, @"", _commentsItem, @"", _addCommentButtonItem, nil];

  4. When I actually GOT comments, I then swapped the TTTableTextItem for a new TTTableView:

    _commentsPresentItem = 
    [[DetailCommentsItem alloc] initWithFilter:self.lfm.object_id 
                                      detailViewController:self];
    _commentsItem  = _commentsPresentItem;
    
    // This is SUPER important. Without it the TTTableView in the
    // DetailCommentsItem won't appear properly.
    [[_commentsItem commentsViewController] viewWillAppear:TRUE];
    
    // Update OUR data source to signal we're swapped out row. Remember,
    // the source is a NSArray of SECTIONS each containing a NSArray of rows
    // for the section.
    unsigned int section  = [[self commentsItemIndexPath] section];
    unsigned int row      = [[self commentsItemIndexPath] row];
    [[[[self dataSource] items] objectAtIndex:section] removeObjectAtIndex:row];
    [[[[self dataSource] items] objectAtIndex:section] insertObject:_commentsPresentItem 
                                                  atIndex:row];
    
    //Notify the table view
    NSArray *x  = [[NSArray alloc] initWithObjects:_commentsItemIndexPath, nil];
    [self.tableView beginUpdates];
    [self.tableView reloadRowsAtIndexPaths:x withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
    
  5. This bit is called whether or not we created a new table - we need it when we a new comment is added to force the enclosed table to update it's view properly. It's included for completeness.

    [[_commentsPresentItem commentsViewController] invalidateModel];
    [[_commentsPresentItem commentsViewController] refresh];
    
Michael Wilson
  • 800
  • 8
  • 15