2

I am trying to implement adding recipients to TTMessageController based on the example from TTCatalog. Everything works fine until I enter search controller - I can search and get results, but nothing happens when I select items. I tried to set delegates, but none works.

- (void)loadView {
    [super loadView];

    TTTableViewController* searchController = [[TTTableViewController alloc] init];
    searchController.dataSource = [[FriendsDataSource alloc] initWithApi:self.appDelegate.api];
    searchController.variableHeightRows=YES;
    self.searchViewController = searchController;
    self.tableView.tableHeaderView = _searchController.searchBar;
}

In TTCatalog, items in search have an URL directing to google - that's not very helpful ;)

Michal Dymel
  • 4,312
  • 3
  • 23
  • 32

1 Answers1

2

OK, I've found the answer :)

One line was missing from the code above. I had a feeling I should set the delegate, but didn't know where:

- (void)loadView {
    [super loadView];

    TTTableViewController* searchController = [[TTTableViewController alloc] init];
    searchController.dataSource = [[FriendsDataSource alloc] initWithApi:self.appDelegate.api];
    self.searchViewController = searchController;
    _searchController.searchResultsTableView.delegate=self;
    self.tableView.tableHeaderView = _searchController.searchBar;
}

Then it as enough to add this to allow variable heights:

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    id<TTTableViewDataSource> dataSource = (id<TTTableViewDataSource>)tableView.dataSource;

    id object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath];
    Class cls = [dataSource tableView:tableView cellClassForObject:object];
    return [cls tableView:tableView rowHeightForObject:object];
}

And this to handle selection:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TTTableImageItemCell *cell = (TTTableImageItemCell *) [tableView cellForRowAtIndexPath:indexPath];
    TTTableImageItem *object = [cell object];
    [_delegate MessageAddRecipient:self didSelectObject:object];
}
Michal Dymel
  • 4,312
  • 3
  • 23
  • 32
  • I see what you did there. Nice way doing it by setting the `UITableViewDelegate`. However, I was expecting Three20 to be little more intelligent and do this for you somehow. Anyway, you solved my problem. Thanks a lot! – dimme Jan 04 '12 at 17:06