搜索好友页面
搜索好友页面用于检索当前用户设备上的所有好友。进入该页面,在输入框输入关键字,SDK 就会从数据库中拉取好友信息。IMKit 提供基于 UIKit UITableView
的搜索页面类 RCSearchFriendsViewController
。
搜索好友页面
搜索好友页面一般由导航栏,搜索栏和好友列表三部分部分组成。
初始化
调用 RCSearchFriendsViewController
类的初始化方法构建搜索好友页面。注意,您需要创建一个 RCSearchFriendsViewModel
对象, 作为RCSearchFriendsViewController
的业务逻辑处理模块。
RCSearchFriendsViewModel *viewModel = [[RCSearchFriendsViewModel alloc] init];
RCSearchFriendsViewController *vc = [[RCSearchFriendsViewController alloc] initWithViewModel:viewModel];
[self.navigationController pushViewController:vc animated:YES];
参数 | 类型 | 说明 |
---|---|---|
viewModel | RCSearchFriendsViewModel | RCSearchFriendsViewController 的业务逻辑处理模块。 处理页面UI的配置以及好友信息的拉取。 |
定制化
IMKit 搜索好友界面的样式可供自定义修改。
标题栏
IMKit 的 RCSearchFriendsViewController
使用了系统的导航栏,可用于显示页面的标题,用户通过继承RCSearchFriendsViewController
,在 viewDidLoad
方法中通过 title
属性设置标题。
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"新标题"
}
搜索框
RCSearchFriendsViewController
搜索默认是搜索好友,通过设置 RCSearchFriendsViewModel
的 delegate
属性, 实现相关代理方法,自定义搜索功能。
RCSearchFriendsViewModel *viewModel = [[RCSearchFriendsViewModel alloc] init];
viewModel.delegate = self;
RCSearchFriendsViewController * = [[RCSearchFriendsViewController alloc] initWithViewModel:viewModel];
[self.navigationController pushViewController:contactVC animated:YES];
...
/// 配置自定义的搜索功能
- (RCSearchBarViewModel *_Nullable)willConfigureSearchBarViewModelForSearchFriendsViewModel:(RCSearchFriendsViewModel *_Nonnull)viewModel {
//返回自定义的搜索 ViewModel
}
...
自定义 Cell
1. 自定义 RCCustomCell
@interface RCCustomCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@end
@implementation
// Cell 绘制
@end
2.自定义 RCCustomCellViewModel
提示
RCFriendListCellViewModel 可以Cell的创建, 自定义 CellViewModel 需要继承该类
typedef void(^RCPermanentCellViewModelBlock)(UIViewController *);
@interface RCCustomCellViewModel : RCFriendListCellViewModel
/// 初始化
- (instancetype)initWithTitle:(NSString *)title
portrait:(UIImage *)portrait
touchBlock:(RCPermanentCellViewModelBlock)touchBlock;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UIImage *portrait;
@property (nonatomic, copy) RCPermanentCellViewModelBlock touchBlock;
@end
@implementation RCCustomCellViewModel
- (instancetype)initWithTitle:(NSString *)title
portrait:(UIImage *)portrait
touchBlock:(RCPermanentCellViewModelBlock)touchBlock{
self = [super init];
if (self) {
self.title = title;
self.portrait = portrait;
self.touchBlock = touchBlock;
}
return self;
}
// 注册并自定义cell
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"RCCustomCellIdentifier";
// 注册 cell
[tableView registerClass:RCCustomCell.class forCellReuseIdentifier:cellIdentifier];
// 返回 cell
RCCustomCellViewModel *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
//修改UI
cell.titleLabel.text = self.title;
cell.contentLabel.text = self.detail;
return cell;
}
@end
3. 添加代理
初始化时设置 RCSearchFriendsViewModel
代理:
RCSearchFriendsViewModel *viewModel = [[RCSearchFriendsViewModel alloc] init];
viewModel.delegate = self;
4. 修改数据源
实现代理方法:
// 好友数据源
- (NSArray *_Nullable)searchFriendsViewModel:(RCSearchFriendsViewModel *_Nonnull)viewModel
willLoadItemsInDataSource:(NSArray *_Nullable)dataSource; {
// 对数据源增删改处理
NSMutableArray *list = dataSource.mutableCopy;
RCCustomCellViewModel *customCellVM = [[RCCustomCellViewModel alloc] initWithTitle:@"title" portrait:[UIImage imageNamed:@"newFriend"] touchBlock:^(UIViewController * vc) {
// 点击事件
}];
[list addObject:@[customCellVM]];
return list;
}
5.点击 cell
/// 用户点击Cell事件
/// - Parameters:
/// - viewModel: viewModel
/// - viewController: viewController
/// - tableView: tableView
/// - indexPath: indexPath
/// - viewModel: CellViewModel
/// - Returns: App是否处理[YES : SDK不再处理, NO: SDK处理]
///
/// - Since:
- (BOOL)searchFriendsViewModel:(RCSearchFriendsViewModel *_Nonnull)viewModel
viewController:(UIViewController*_Nonnull)viewController
tableView:(UITableView *_Nonnull)tableView
didSelectRow:(NSIndexPath *_Nonnull)indexPath
cellViewModel:(RCBaseCellViewModel *_Nonnull)cellViewModel {
}