如何查看字符串是否包含子字符串(objective-c)
我想看看一个字符串(苹果rss新闻提要上的帖子标题)是否包含子字符串,例如“Steve”或“Jobs”。我将帖子组织成一个uitableview 有一篇文章的标题是史蒂夫或乔布斯,所以我用这个来检查:如何查看字符串是否包含子字符串(objective-c),objective-c,xcode,string,compare,substring,Objective C,Xcode,String,Compare,Substring,我想看看一个字符串(苹果rss新闻提要上的帖子标题)是否包含子字符串,例如“Steve”或“Jobs”。我将帖子组织成一个uitableview 有一篇文章的标题是史蒂夫或乔布斯,所以我用这个来检查: if ([[entry title] localizedCaseInsensitiveCompare:@"Steve"] == NSOrderedSame || [[entry title] localizedCaseInsensitiveCompare: @"Jobs"] =
if ([[entry title] localizedCaseInsensitiveCompare:@"Steve"] == NSOrderedSame || [[entry title] localizedCaseInsensitiveCompare: @"Jobs"] == NSOrderedSame) {
NSLog(@"Comparism of Steve Jobs");
cell.imageView.image = [UIImage imageNamed:@"steve.png"];
}
但它从未被调用,entry是一个RSSItem类,它包含标题-我已经检查过,条目及其标题不是我的问题。我的比较主义是个问题。我如何比较
更新
好的,代码如下:
NSRange range = [[[cell textLabel] text] rangeOfString:@"Steve" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound)
{
cell.imageView.image = [UIImage imageNamed:@"steve.png"];
}
我也尝试过其他人的方法,但结果相同:
有些单元格的imageView为steve.png,尽管标题中不包含steve jobs。奇怪的我向下滚动,当我返回所有重新分配和初始化的出列单元时,都有steve jobs的图片。在开始时,当我打开应用程序时,一些标题中没有史蒂夫的单元格确实有图像,然后发生了上述情况
我的周围代码(如果需要):
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"UITableViewCell"]
autorelease];
}
tableView.autoresizingMask = 5;
tableView.autoresizesSubviews = YES;
cell.autoresizingMask = 5;
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, 20, 20);
RSSItem *item = [[channel items] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[item title]];
NSMutableString *string = [[NSMutableString alloc] initWithString:[[cell textLabel] text]];
if (string.length > 46) {
cell.textLabel.numberOfLines = 2;
UILineBreakMode lineBreak = UILineBreakModeClip;
cell.textLabel.lineBreakMode = lineBreak;
}
[string release];
tableView.backgroundColor = [UIColor darkGrayColor];
cell.textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size: 12.0];
cell.backgroundColor = [UIColor whiteColor];
NSRange range = [[[cell textLabel] text] rangeOfString:@"Steve" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound)
{
cell.imageView.image = [UIImage imageNamed:@"steve.png"];
}
return cell;
}
您正在比较整个字符串,“Steve Jobs”与“Steve”或“Jobs”都不匹配。您可能希望使用
rangeOfString:@“Steve”选项:NSCaseInsensitiveSearch
,或类似的一些选项。NSString rangeOfString返回一个NSRange,可以检查是否存在“未找到”情况
“当我向上滚动时,所有回收的电池都有史蒂夫·乔布斯的图像” 这是因为你没有重置回收单元中的图像。(尝试添加
else {
cell.imageView.image = nil;
}
在适当的位置。)如果您使用的是iOS8,您现在可以通过以下方式执行此操作:
NSString *stringToSearch = @"Steve";
if ([stringToSearch containsString:@"Steve"])
{
NSLog(@"String contains Steve!!!");
}
else
{
NSLog(@"String does not contain Steve!!!");
}
但这并不能告诉我Steve是否位于返回NSRange的字符串中,该字符串与我想要的字符串相差甚远。我基本上想知道Steve是否在标题中,这样我就可以将单元格的imageView图像制作为Steve Jobs的图像。这正是您想要的。如果您阅读文档,如果字符串不包含,它将返回
{NSNotFound,0}
的NSRange
。+1使用选项:NSCaseInsensitiveSearch
版本完成操作(明显)不区分大小写搜索的目标。由于某些原因,即使不正确,也会调用此比较检查我的更新问题请提供与“Steve”错误匹配的几个单元格的文本。您是否从非“Steve”的回收单元格中删除图像?在我看来不是这样的。几个与史蒂夫不匹配的细胞的文本:苹果董事会声明一天内下载1亿OSX Lion然后当我卷回所有回收细胞时都有史蒂夫·乔布斯的图像“然后当我卷回所有回收细胞时都有史蒂夫·乔布斯的图像”--这是因为您没有重置回收单元中的图像。(请尝试在适当的位置添加else{cell.imageView.image=nil;}
。)
NSString *stringToSearch = @"Steve";
if ([stringToSearch containsString:@"Steve"])
{
NSLog(@"String contains Steve!!!");
}
else
{
NSLog(@"String does not contain Steve!!!");
}