Skip to main content

Search my groups

The search my groups page retrieves group channels that the current user has joined. When entering keywords in the search field, the Chat UI SDK fetches matching group information from the database. Chat UI provides NCSearchGroupsViewController, a search page class based on UITableView.

The search my groups page typically consists of three components:

  • Navigation bar
  • Search bar
  • Group list

Initialize

Create a search groups page by initializing the NCSearchGroupsViewController class. Note: You must create an NCSearchGroupsViewModel object to serve as the business logic handler for NCSearchGroupsViewController.

Parameters

ParameterTypeDescription
viewModelNCSearchGroupsViewModelBusiness logic handler for NCSearchGroupsViewController. Manages UI configuration and group information retrieval.

Example

Objective C
NCSearchGroupsViewModel *viewModel = [[NCSearchGroupsViewModel alloc] init];
NCSearchGroupsViewController *vc = [[NCSearchGroupsViewController alloc] initWithViewModel:viewModel];
[self.navigationController pushViewController:vc animated:YES];

Customization

You can customize the appearance of the search my groups interface in Chat UI.

Customize the title bar

NCSearchGroupsViewController uses the system navigation bar to display the page title. To customize the title, subclass NCSearchGroupsViewController and set the title property in viewDidLoad.

Objective C
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Custom title";
}

Customize the search field

By default, NCSearchGroupsViewController searches groups by name. To implement custom search behavior, set the delegate property of NCSearchGroupsViewModel and implement the search delegate methods.

Example

Objective C
NCSearchGroupsViewModel *viewModel = [[NCSearchGroupsViewModel alloc] init];
viewModel.delegate = self;
NCSearchGroupsViewController *vc = [[NCSearchGroupsViewController alloc] initWithViewModel:viewModel];

[self.navigationController pushViewController:vc animated:YES];

...

/// Configure custom search functionality
- (NCSearchBarViewModel *_Nullable)willConfigureSearchBarViewModelForSearchGroupsViewModel:(NCSearchGroupsViewModel *_Nonnull)viewModel{
// Return custom search ViewModel
}

...

Customize group cell

1. Create a custom NCCustomCell

Objective C
@interface NCCustomCell : NCFriendListPermanentCell
- (void)showPortrait:(NSString *)url;
@end

@implementation
// Cell rendering
@end

2. Create a custom NCCustomCellViewModel

tip

Custom cell view models must subclass NCGroupInfoCellViewModel.

Objective C
@interface NCCustomCellViewModel: NCGroupInfoCellViewModel
@property (nonatomic, strong) NCGroupInfo *groupInfo;

/// Initialize
/// - Parameters:
/// - groupInfo: Group information
/// - keyword: Highlight keyword
- (instancetype)initWithGroupInfo:(NCGroupInfo *)groupInfo
keyword:(NSString *)keyword;
@end
@end

// Register and customize cell
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"NCCustomCellIdentifier";
// Register cell
[tableView registerClass:NCCustomCell.class forCellReuseIdentifier:cellIdentifier];
// Return cell
NCCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
// Modify UI
...
return cell;
}

@end

3. Modify data source

Implement the data source callback delegate method:

Objective C
// Friend data source
- (NSArray *_Nullable)searchGroupsViewModel:(NCSearchGroupsViewModel *_Nonnull)viewModel
willLoadItemsInDataSource:(NSArray *_Nullable)dataSource {
// Process data source modifications
NSMutableArray *list = dataSource.mutableCopy ?: [NSMutableArray array];

NCGroupInfo *groupInfo = [[NCGroupInfo alloc] init];
groupInfo.groupId = @"groupId";
groupInfo.groupName = @"Group Name";
NCCustomCellViewModel *customCellVM =
[[NCCustomCellViewModel alloc] initWithGroupInfo:groupInfo keyword:@"keyword"];
[list addObject:customCellVM];
return list;
}

4. Customize cell tap events

Objective C
/// Handle cell selection events
/// - Parameters:
/// - viewModel: viewModel
/// - viewController: viewController
/// - tableView: tableView
/// - indexPath: indexPath
/// - viewModel: CellViewModel
/// - Returns: Whether the app handles the event [YES: SDK won't process, NO: SDK processes]
- (BOOL)searchGroupsViewModel:(NCSearchGroupsViewModel *_Nonnull)viewModel
viewController:(UIViewController*_Nonnull)viewController
tableView:(UITableView *_Nonnull)tableView
didSelectRow:(NSIndexPath *_Nonnull)indexPath
cellViewModel:(NCBaseCellViewModel *_Nonnull)cellViewModel {
}