0

我创建了一个从 .xml 文件解析的 RSS 阅读器。numberOfRowsInSectioncellForRowAtIndexPath如下所示:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.parseResults.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //Check if cell is nil. If it is create a new instance of it
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    // Configure titleLabel
    cell.textLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.textLabel.numberOfLines = 2;
    //Configure detailTitleLabel
    cell.detailTextLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"summary"];

    cell.detailTextLabel.numberOfLines = 2;

    //Set accessoryType
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //Set font and style
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

    return cell;
}

我正在尝试创建搜索栏和搜索显示控制器,但不确定如何在 UITableView 中搜索objectForKey “标题”或objectForKey “摘要”。

任何帮助将不胜感激。

4

1 回答 1

0

如果您尝试同时搜索标题和摘要,您可以在 UISearchBarDelegate 方法中尝试此操作吗

- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchText
{
  if ([searchText length] == 0)
  {
    [self.tableView reloadData];
    return;
  }

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.title contains[cd] %@ OR SELF.summary contains[cd] %@", searchText, searchText];
  NSArray *filteredArray = [self.parseResults filteredArrayUsingPredicate:predicate];

  // TODO: Use this filteredArray to reload data

  [self.tableView reloadData];
}

供您参考的示例项目 - https://github.com/deepthit/TableViewSearch.git

于 2013-11-12T02:16:27.073 回答