gpt4 book ai didi

ios - UITableView中使用搜索时如何控制多个数组

转载 作者:行者123 更新时间:2023-12-01 18:50:14 27 4
gpt4 key购买 nike

我有四个数组 videoName、ActorName、VideoID、ActorID。我将 videoName 和 ActorName 组合成一个数组“标题”,并与 VideoID 和 ActorID 组合成一个数组“ID”
简而言之,
标题 = Actor 名 + 视频名
ID = ActorID + VideoID

这是我的代码,

TableView 方法

- (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;
}

要求

现在,首先我需要获取选择了哪一行,actor 或 Video,其次是 actorID 或 VideoID。如果没有搜索栏,这很容易,因为在搜索后,所有行都用新数据再次恢复,并且行是从“标题”而不是“ID”填充的,所以当用户选择行时我如何获取 ID。

最佳答案

为这个 Item.h 创建一个 NSObject 子类和 Item.m具有名称和 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

通过上面的代码,我们正在创建一个类,其对象可以保存属性中定义的值。

由于 Item 的对象类可以保存视频/ Actor 的名称、id、图像名称和类型,我们可以将 1 个视频/ Actor 的这些值设置为一个对象。
这里我们有多个vides/actor元素,所以我们需要一个 Item的数组每个对象都包含一个视频/ 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
}

关于ios - UITableView中使用搜索时如何控制多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31579143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com