xcode 将文本字符串加载到 UISearchController 中 [英] xcode Loading a text string into a UISearchController
问题描述
我使用这个例子来搜索 UICollectionView:https://github.com/ihomam/CollectionViewWithSearchBar/blob/master/collevtionViewWithSearchBar/CollectionViewController.m
I was using this example for searching through a UICollectionView: https://github.com/ihomam/CollectionViewWithSearchBar/blob/master/collevtionViewWithSearchBar/CollectionViewController.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
if (self.searchBarActive) {
cell.laName.text = self.dataSourceForSearchResult[indexPath.row];
} else {
cell.laName.text = self.dataSource[indexPath.row];
}
return cell;
}
...只是我的应用程序设置有点不同:
...only my app is setup a little differently:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PlaceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
Place *p = [_entries objectAtIndex:indexPath.item];
if (self.searchBarActive) {
cell.placeName.text = self.dataSourceForSearchResult[indexPath.item];
} else {
cell.placeName.text = p.PName;
cell.placeImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:p.PImage]]];
}
return cell;
}
因此,如果我只是按照它的方式运行它,那么 self.dataSourceForSearchResult[indexpath.item] 将返回所有内容,并且我收到一个错误,因为它没有返回字符串...
So if I just run it the way it is then self.dataSourceForSearchResult[indexpath.item] will return EVERYTHING and I get an error because it's not returning a string...
'不能将 in/contains 运算符与集合
但我想要它做的是搜索 p.PName 结果.类似的东西:
but what I want it to do is search through the p.PName results. Something like:
self.dataSourceForSearchResult[p.PName indexpath.item]
推荐答案
查看 github 代码后,我认为您的问题的核心在于其他地方.
Having looked through the github code I think that the core of your problem lies somewhere else.
问题出在这里:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self contains[c] %@", searchText];
self.dataSourceForSearchResult = [self.dataSource filteredArrayUsingPredicate:resultPredicate];
}
这个方法的作用是过滤来自 self.datasource
的匹配项(我假设你重命名为 self.entries
,我希望你也在这里做了)基于谓词.谓词有点难以理解,特别是如果您以前没有使用它们的经验.让我们分解一下:
What this method does is it filters matching items from self.datasource
(which I assume you renamed to self.entries
, and I hope you also did it here) based on the predicate. The predicate is a little tricky to understand, especially if you have no previous experience with them. Let's break it down :
self
- 在此上下文中,它表示将根据条件进行检查的对象.这个对象将是数组的一个元素.
self
- in this context it means the object that will be checked against the condition. This object will be an element of the array.
contains[c]
- 这是条件.self
将被检查是否contains
.[c]
表示检查不区分大小写.
contains[c]
- this is the condition. The self
will be checked if it contains
something. The [c]
means that the check will be case insensitive.
%@
- 这是东西".它将被替换为 searchText
.
%@
- this is the 'something'. It will be replaced with searchText
.
这在示例中有效,因为数组包含 NSString
对象,并且 NSPredicate
知道如何检查它们是否包含
某些东西(子字符串).由于您的数组由 Place
对象组成,NSPredicate
不知道如何检查它们是否包含
某些东西(子位置?:P).
This worked in the example because the array contained NSString
objects and NSPredicate
knows how to check if they contain
something (a substring). Since your array consists of Place
objects the NSPredicate
doesn't know how to check if they contain
something (a subplace? :P).
为了解决这个问题,用这个self.PName contains[c] %@
替换谓词.这将访问数组的每个对象的 PName
属性并检查它是否包含子字符串.
To fix this replace the predicate with this one self.PName contains[c] %@
. This one will acces the PName
property of each object of array and check whether it contains the substring.
关于第二个问题,self.dataSourceForSearchResult[indexpath.item]
实际上不会返回一个NSString
而是一个Place
对象,虽然不是 EVERYTHING
,但其中之一.它实际上从未在您的应用程序中访问过,因为它早些时候崩溃了.无意冒犯,但我认为您并没有完全理解这段代码中的整个顺序.分解一下:
As to the second problem, self.dataSourceForSearchResult[indexpath.item]
will not in fact return an NSString
but a Place
object, though not EVERYTHING
, but one of the filtered ones. It was actually never reached in your app, as it crashed earlier. No offence, but I think that you don't entirely understand the whole order of things in this code. To break it down :
- 显示视图控制器并且搜索栏为空,因此根据
self.datasource
(或重命名时的self.entries
)中的数据绘制单元格.collectionView:cellForItemAtIndexPath:
正在被调用. - 用户在搜索栏中输入内容.(这会触发
searchBar:textDidChange:
). - 匹配的对象被过滤到
self.dataSourceForSearchResult
数组中. - 集合视图被重新加载.
- 单元格被重绘(
collectionView:cellForItemAtIndexPath:
再次被调用),但由于搜索栏不为空,我们现在只使用来自self.dataSourceForSearchResult
的对象.
- The view controller is shown and the searchbar is empty so the cells are drawn, based on data in
self.datasource
(orself.entries
as you renamed it).collectionView:cellForItemAtIndexPath:
is being called. - User inputs something in the search bar. (this triggers
searchBar:textDidChange:
). - The matching objects are filtered into
self.dataSourceForSearchResult
array. - The collection view gets reloaded.
- The cells are redrawn (
collectionView:cellForItemAtIndexPath:
is being called again), but since the searchbar is not empty, we now use only objects fromself.dataSourceForSearchResult
.
所以为了让它工作你的 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
应该是这样的:
So to make it work your - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
should look like this :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PlaceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
Place *p;
// retrieve item from appropriate array
if (self.searchBarActive) {
p = self.dataSourceForSearchResult[indexPath.item];
} else {
p = self.entries[indexPath.item];
}
// populate the cell - we do this regardless of whether the searchbar is active or not
cell.placeName.text = p.PName;
cell.placeImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:p.PImage]]];
return cell;
}
这篇关于xcode 将文本字符串加载到 UISearchController 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!