how to see if a string contains a substring (objec

2019-07-18 23:31发布

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;

    }

4条回答
我想做一个坏孩纸
2楼-- · 2019-07-19 00:04

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.' 
}
查看更多
萌系小妹纸
3楼-- · 2019-07-19 00:09

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!!!");
  }
查看更多
Anthone
4楼-- · 2019-07-19 00:18

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.

查看更多
beautiful°
5楼-- · 2019-07-19 00:28

"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.)

查看更多
登录 后发表回答