Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to see if a string contains a substring (objective-c)
    primarykey
    data
    text
    <p>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.</p> <p>So there WAS a post which had Steve or Jobs in its title so I used this to check:</p> <pre><code> if ([[entry title] localizedCaseInsensitiveCompare:@"Steve"] == NSOrderedSame || [[entry title] localizedCaseInsensitiveCompare: @"Jobs"] == NSOrderedSame) { NSLog(@"Comparism of Steve Jobs"); cell.imageView.image = [UIImage imageNamed:@"steve.png"]; } </code></pre> <p>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</p> <p>UPDATE!</p> <p>Ok here is the code:</p> <pre><code>NSRange range = [[[cell textLabel] text] rangeOfString:@"Steve" options:NSCaseInsensitiveSearch]; if (range.location != NSNotFound) { cell.imageView.image = [UIImage imageNamed:@"steve.png"]; } </code></pre> <p>I have tried it other peoples way also but SAME RESULT:</p> <p>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.</p> <p>My surrounding code IF NEEDED:</p> <pre><code> -(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 &gt; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload