如何查看字符串是否包含子字符串(objective-c) [英] how to see if a string contains a substring (objective-c)
问题描述
我想查看一个字符串,这是苹果rss新闻源上的帖子的标题包含一个子字符串,例如。 史蒂夫或工作。
I want to see if a string, which is a title of a post on the apple rss news feed contains a substring, e.g. "Steve" or "Jobs". I organized the posts into a uitableview.
所以这里有一个标题中有史蒂夫或乔布斯的帖子,所以我用这个来检查:
So there WAS a post which had Steve or Jobs in its title so I used this to check:
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类,该条目及其标题不是我的问题,我已经检查过。我的比较是问题。如何比较
But it was never called, entry is an RSSItem class which contains the title - the entry and its title is not my problem, I have checked. My comparism is the problem. How do i compare
UPDATE!
确定这里是代码:
NSRange range = [[[cell textLabel] text] rangeOfString:@"Steve" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound)
{
cell.imageView.image = [UIImage imageNamed:@"steve.png"];
}
但同一结果:
一些单元格的imageView为steve.png,即使他们的标题不包含steve作业。奇怪的???我向下滚动,当我回去时,所有重新分配和初始化的出队单元都有steve作业图片。开始时我打开应用程序的一些细胞,他们的标题没有史蒂夫有图像,然后上述发生。
some cells have their imageView as steve.png even though their title doesnt contain steve jobs. Weird??? I scroll down and when I go back up all the dequeued cells which are reallocated and initialized have the steve jobs picture. At starting when i open the app some cells which don't have steve in their title DO HAVE THE IMAGE, and then the above happens.
我的周围的代码如果需要:
My surrounding code IF NEEDED:
-(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;
}
推荐答案
当我向上滚动所有回收的单元格都有史蒂夫作业图像
"then when i scroll back up all the recycled cells have the steve jobs image"
这是因为您不会重置回收单元格中的图像。 (尝试添加
That's because you're not resetting the image in the recycled cells. (Try adding
else {
cell.imageView.image = nil;
}
。
这篇关于如何查看字符串是否包含子字符串(objective-c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!