【问题标题】:xcode Loading a text string into a UISearchControllerxcode 将文本字符串加载到 UISearchController
【发布时间】:2015-07-24 15:45:15
【问题描述】:

我正在使用这个示例搜索 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;
}

...只有我的应用程序设置略有不同:

- (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] 将返回所有内容,并且我收到一个错误,因为它没有返回字符串...

'Can't use in/contains operator with collection <Place: 0x17d8ad70> (not a collection)'

但我想要它做的是搜索 p.PName 结果。比如:

self.dataSourceForSearchResult[p.PName indexpath.item]

【问题讨论】:

  • 你试过了吗:shouldChangeTextInRange:(NSRange)range replacementText:(NSString * _Nonnull)text
  • 不确定您的意思...您能否详细说明或给我一个示例链接?

标签: ios xcode6 uicollectionview uisearchcontroller


【解决方案1】:

查看 github 代码后,我认为您的问题的核心在其他地方。

问题出在这里:

- (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,我希望您在这里也这样做了)。谓词有点难以理解,特别是如果您以前没有使用它们的经验。让我们分解一下:

  • self - 在此上下文中,它表示将根据条件检查的对象。该对象将是数组的一个元素。

  • contains[c] - 这是条件。 self 将被检查是否为 contains 某事。 [c] 表示检查不区分大小写。

  • %@ - 这就是“东西”。它将被替换为searchText

这在示例中有效,因为数组包含 NSString 对象并且 NSPredicate 知道如何检查它们是否 contain 某些东西(子字符串)。由于您的数组由Place 对象组成,NSPredicate 不知道如何检查它们是否contain 某些东西(子位置?:P)。

要解决此问题,请将谓词替换为 self.PName contains[c] %@。这将访问数组的每个对象的PName 属性并检查它是否包含子字符串。

您可以在 NSHipsterdocs 上阅读更多关于 NSPredicate 的信息


至于第二个问题,self.dataSourceForSearchResult[indexpath.item] 实际上不会返回一个NSString,而是一个Place 对象,虽然不是EVERYTHING,而是过滤的对象之一。它实际上从未在您的应用程序中到达,因为它之前崩溃了。没有冒犯,但我认为你并不完全理解这段代码中的整个顺序。分解它:

  1. 显示视图控制器并且搜索栏为空,因此根据self.datasource(或您重命名的self.entries)中的数据绘制单元格。正在调用collectionView:cellForItemAtIndexPath:
  2. 用户在搜索栏中输入内容。 (这会触发 searchBar:textDidChange:)。
  3. 匹配的对象被过滤到self.dataSourceForSearchResult数组中。
  4. 集合视图被重新加载。
  5. 重绘单元格(再次调用collectionView:cellForItemAtIndexPath:),但由于搜索栏不为空,我们现在只使用来自self.dataSourceForSearchResult 的对象。

所以要让它发挥作用,你的 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; 应该是这样的:

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

【讨论】:

  • 哇...只是...哇。这真的是超越了。谢谢。
  • 我听从了您的建议,现在效果很好。现在让我感到困惑的一件事是:当我键入时,它会过滤匹配的对象并重新加载集合......但如果我退格它不会回到原始视图。另外,如果我按下取消按钮。这是我第一次运行程序时...但不是现在。奇怪......它应该在我删除时重新加载,当我点击取消按钮时它肯定会刷新。 (只有当我输入新文本时它才会改变)。您知道为什么会发生这种情况吗?
  • 让我们继续聊天,以免混淆原始问题:chat.stackoverflow.com/rooms/84280/…
猜你喜欢
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
相关资源
最近更新 更多