【发布时间】:2015-02-26 09:50:54
【问题描述】:
在我的应用程序中,我正在使用UISearchBar,用户将通过搜索用户名从NSMutableArray 进行搜索。这是我从NSMutableArray搜索用户的方法
- (void)searchTableList
{
NSString *searchString = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in contentList)
{
NSArray *array = [dictionary objectForKey:@"username"];
[searchArray addObject:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchString options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[filteredContentList addObject:sTemp];
}
}
这是我如何配置 UITableView 以显示搜索结果
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SearchListTableViewCell";
NSMutableDictionary *dict=[contentList objectAtIndex:indexPath.row];
SearchListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SearchListTableViewCell" owner:nil options:nil];
cell = (SearchListTableViewCell*)[topLevelObjects objectAtIndex:0];
cell.backgroundColor=[UIColor colorWithRed:239.0/255 green:239.0/255 blue:239.0/255 alpha:1];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
// Configure the cell...
if (isSearching)
{
cell.friendName.text = [filteredContentList objectAtIndex:indexPath.row];
NSMutableArray *searchImageArray=[[NSMutableArray alloc]init];
for (NSDictionary *dictionary in contentList)
{
NSArray *imageArray=[dictionary objectForKey:@"thumbnail_path_150_150"];
NSLog(@"image arrayt is %@",imageArray);
[searchImageArray addObject:imageArray];
}
for (NSString *imageS in searchImageArray) {
NSRange title=[imageS rangeOfString:cell.friendName.text options:NSCaseInsensitiveSearch];
if (title.length>0) {
[filteredImage addObject:imageS];
}
}
NSString *url=[NSString stringWithFormat:@"%@/get_image?avatar=%@",BaseURL,[filteredImage objectAtIndex:indexPath.row]];
url=[url stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:url]]];
[cell.friendProfileImage setImage:myImage];
NSLog(@"filtered Image data %lu",(unsigned long)filteredImage.count);
NSLog(@"filtered content list data %lu",(unsigned long)filteredContentList.count);
}
else
{
cell.friendName.text =[dict objectForKey:@"username"];
NSString *url=[NSString stringWithFormat:@"%@/get_image?avatar=%@",BaseURL,[dict objectForKey:@"thumbnail_path_150_150"]];
url=[url stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:url]]];
[cell.friendProfileImage setImage:myImage];
}
return cell;
}
现在我想从NSMutableDictionary 中获取搜索到的用户名的其余数据。这是我的字典。
(
{
jid = "hey@196.0.0.1";
"thumbnail_path_150_150" = "E\\path\\to\\getImage\\files\\heythumbnail";
username = hey;
},
{
jid = "tweety@196.0.0.1";
"thumbnail_path_150_150" = "E:\\path\\to\\getImage\\files\\tweetythumbnail";
username = tweety;
}
)
在这里,我想获取搜索用户的图像。如果用户搜索嘿,它必须在 tableViewCell 上显示"thumbnail_path_150_150"
="E\\path\\to\\getImage\\files\\heythumbnail"; 这个图像。我尝试了设置图像的代码但是当两个用户具有相同的名称时它会替换图像。就像如果一个用户是 Ray 而另一个是 Blueray ,那么它会改变两者的形象。我知道我写的太多了。但在这一点上我真的很困惑。请任何人帮助解决这个问题。
【问题讨论】:
-
你有一个字典数组并且想要搜索匹配的数组项.. 只是澄清我在上面看到的内容
-
@Daij-Djan,没错。
标签: ios objective-c uitableview uisearchbar nsmutabledictionary