2

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"];
}

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!

Ok here is the code:

NSRange range = [[[cell textLabel] text] rangeOfString:@"Steve" options:NSCaseInsensitiveSearch];

if (range.location != NSNotFound) 
{
    cell.imageView.image = [UIImage imageNamed:@"steve.png"];


}

I have tried it other peoples way also but SAME RESULT:

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;

    }
6
  • 1
    Please give the text of a few cells that falsely match "Steve". Commented Dec 18, 2011 at 15:12
  • 1
    And are you removing the image from recycled cells that are not "Steve"? It doesn't look like it to me. Commented Dec 18, 2011 at 15:14
  • Text of a few cells that falsely match steve: statement of apple board directors 100 million OSX Lion downloads in a day Commented Dec 18, 2011 at 23:08
  • then when i scroll back up all the recycled cells have the steve jobs image Commented Dec 18, 2011 at 23:09
  • 1
    "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; } at an appropriate place.) Commented Dec 18, 2011 at 23:23

4 Answers 4

12

NSString-rangeOfString returns an NSRange, which can be checked for the "not found" case.

if ([@"Some awesome string." rangeOfString:@"awesome"].location != NSNotFound)
{
  // awesome is in 'Some awesome string.'
}
else 
{
 // awesome is not in 'Some awesome string.' 
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Using the options:NSCaseInsensitiveSearch version accomplishes the OP's (apparent) goal of a case-insensitive search.
for some reason this comparison gets called EVEN WHEN THAT IS NOT TRUE Check my updated question
6

You're comparing the entire string, and "Steve Jobs" won't match either "Steve" or "Jobs". You probably want to use rangeOfString:@"Steve" options:NSCaseInsensitiveSearch, or some such.

3 Comments

but that won't tell me if Steve is located in the string
that returns an NSRange nowhere close to what i want I basically want to FIND OUT IF Steve is inside the title so I can make the image of the imageView of the cell a picture of steve jobs
It does exactly what you want. If you read the documentation, it returns an NSRange of {NSNotFound, 0} if the string is not contained.
2

If you are on iOS8 you can do this now as:

  NSString *stringToSearch = @"Steve";
  if ([stringToSearch containsString:@"Steve"])
  {
      NSLog(@"String contains Steve!!!");
  } 
  else 
  {
      NSLog(@"String does not contain Steve!!!");
  }

Comments

1

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

at an appropriate place.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.