在 UITableView 中使用搜索时如何控制多个数组
How to control multiple arrays when using search in UITableView
我有四个数组videoName、ActorName、VideoID、ActorID。我将 videoName 和 ActorName 组合成一个数组 "title",并与 VideoID 和 ActorID 组合成一个数组 "IDs"
简而言之,
标题 = 演员名 + 视频名
IDs = ActorID + VideoID
这是我的代码,
表格视图方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return self.searchResults.count;
} else {
return self.title.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.imageView.frame = CGRectMake(0,0,32,32);
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[img objectAtIndex:indexPath.row]];
cell.textLabel.textColor = [UIColor blackColor];
} else {
cell.textLabel.text = [self.title objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[img objectAtIndex:indexPath.row]];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
搜索方法
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchText];
// NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"contains[c] %@", searchText];
self.searchResults = [self.title filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
要求
现在,首先我需要获取 selected 的行,actor 或 Video,其次是 actorID 或 VideoID。如果没有搜索栏,这很容易,因为在搜索之后,所有行都用新数据再次恢复,而且行是从 "title" 而不是 "IDs" 填充的,所以我如何在用户 select 时获取 ID该行。
为此 Item.h
和 Item.m
创建一个 NSObject 子 class,其中包含名称和 ID 的 3 个属性,例如
在Item.h
typedef enum : NSInteger {
ItemTypeVideo,
ItemTypeActor,
} ItemType;
@interface Item : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *itemId;
@property (nonatomic, strong) NSString *imageName;
@property (nonatomic, assign) ItemType itemType;
@end
通过上面的代码,我们正在创建一个 class 其对象可以保存属性中定义的值。
由于Item
class的一个对象可以保存video/actor的name,id,image name和type,我们可以将这些值1video/actor设置为1目的。
这里我们有多个 vides/actor 元素,所以我们需要一个 Item
对象数组,每个对象都包含一个 video/actor 的详细信息,例如
Item *videoItem = [[Item alloc] init];
videoItem.name = videoName;
videoItem.itemId = videoId;
videoItem.imageName = videoImageName;
videoItem.itemType = ItemTypeVideo;
为 videoName 数组和 actor 数组中的所有项目创建。将这两个添加到一个常见的 dataArray
中,例如
[self.dataArray addObejct:videoItem];
和
Item *actorItem = [[Item alloc] init];
actorItem.name = actorName;
actorItem.itemId = actorId;
actorItem.imageName = actorImageName;
actorItem.itemType = ItemTypeActor;
同样[self.dataArray addObejct:actorItem];
并在 cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//other stuff
Item *currentItem = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
currentItem = [self.searchResults objectAtIndex:indexPath.row];
} else {
currentItem = [self.dataArray objectAtIndex:indexPath.row];
}
cell.textLabel.text = currentItem.name;
cell.imageView.image = [UIImage imageNamed:currentItem.imageName];
//rest stuff
}
终于找到了,
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name beginswith[c] %@", searchText];
// NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"contains[c] %@", searchText];
self.searchResults = [self.dataArray filteredArrayUsingPredicate:predicate];
}
所以无论如何,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Item *currentItem = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
currentItem = [self.searchResults objectAtIndex:indexPath.row];
} else {
currentItem = [self.dataArray objectAtIndex:indexPath.row];
}
//all details will be in currentItem
}
我有四个数组videoName、ActorName、VideoID、ActorID。我将 videoName 和 ActorName 组合成一个数组 "title",并与 VideoID 和 ActorID 组合成一个数组 "IDs" 简而言之, 标题 = 演员名 + 视频名 IDs = ActorID + VideoID
这是我的代码,
表格视图方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return self.searchResults.count;
} else {
return self.title.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.imageView.frame = CGRectMake(0,0,32,32);
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[img objectAtIndex:indexPath.row]];
cell.textLabel.textColor = [UIColor blackColor];
} else {
cell.textLabel.text = [self.title objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[img objectAtIndex:indexPath.row]];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
搜索方法
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchText];
// NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"contains[c] %@", searchText];
self.searchResults = [self.title filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
要求
现在,首先我需要获取 selected 的行,actor 或 Video,其次是 actorID 或 VideoID。如果没有搜索栏,这很容易,因为在搜索之后,所有行都用新数据再次恢复,而且行是从 "title" 而不是 "IDs" 填充的,所以我如何在用户 select 时获取 ID该行。
为此 Item.h
和 Item.m
创建一个 NSObject 子 class,其中包含名称和 ID 的 3 个属性,例如
在Item.h
typedef enum : NSInteger {
ItemTypeVideo,
ItemTypeActor,
} ItemType;
@interface Item : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *itemId;
@property (nonatomic, strong) NSString *imageName;
@property (nonatomic, assign) ItemType itemType;
@end
通过上面的代码,我们正在创建一个 class 其对象可以保存属性中定义的值。
由于Item
class的一个对象可以保存video/actor的name,id,image name和type,我们可以将这些值1video/actor设置为1目的。
这里我们有多个 vides/actor 元素,所以我们需要一个 Item
对象数组,每个对象都包含一个 video/actor 的详细信息,例如
Item *videoItem = [[Item alloc] init];
videoItem.name = videoName;
videoItem.itemId = videoId;
videoItem.imageName = videoImageName;
videoItem.itemType = ItemTypeVideo;
为 videoName 数组和 actor 数组中的所有项目创建。将这两个添加到一个常见的 dataArray
中,例如
[self.dataArray addObejct:videoItem];
和
Item *actorItem = [[Item alloc] init];
actorItem.name = actorName;
actorItem.itemId = actorId;
actorItem.imageName = actorImageName;
actorItem.itemType = ItemTypeActor;
同样[self.dataArray addObejct:actorItem];
并在 cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//other stuff
Item *currentItem = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
currentItem = [self.searchResults objectAtIndex:indexPath.row];
} else {
currentItem = [self.dataArray objectAtIndex:indexPath.row];
}
cell.textLabel.text = currentItem.name;
cell.imageView.image = [UIImage imageNamed:currentItem.imageName];
//rest stuff
}
终于找到了,
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name beginswith[c] %@", searchText];
// NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"contains[c] %@", searchText];
self.searchResults = [self.dataArray filteredArrayUsingPredicate:predicate];
}
所以无论如何,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Item *currentItem = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
currentItem = [self.searchResults objectAtIndex:indexPath.row];
} else {
currentItem = [self.dataArray objectAtIndex:indexPath.row];
}
//all details will be in currentItem
}