objective-c - 如何查看字符串是否包含子字符串(objective-c)

标签 objective-c xcode string compare substring

我想看看一个字符串,它是苹果 rss 新闻提要上帖子的标题,是否包含一个子字符串,例如“史蒂夫”或“乔布斯”。我将帖子组织成一个 uitableview。

所以有一篇帖子的标题中有史蒂夫或乔布斯,所以我用它来检查:

   if ([[entry title] localizedCaseInsensitiveCompare:@"Steve"] == NSOrderedSame ||        [[entry title] localizedCaseInsensitiveCompare: @"Jobs"] == NSOrderedSame) {

    NSLog(@"Comparism of Steve Jobs");
    cell.imageView.image = [UIImage imageNamed:@"steve.png"];
}

但它从未被调用过,条目是一个包含标题的 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 的单元格确实有图像,然后发生了上述情况。

如果需要,我的周边代码:

  -(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;

    }

最佳答案

NSString-rangeOfString 返回一个 NSRange,可以检查“未找到”的情况。

if ([@"Some awesome string." rangeOfString:@"awesome"].location != NSNotFound)
{
  // awesome is in 'Some awesome string.'
}
else 
{
 // awesome is not in 'Some awesome string.' 
}

关于objective-c - 如何查看字符串是否包含子字符串(objective-c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8549898/

相关文章:

ios - 如何使 SKView 的背景颜色清晰

objective-c - 如何使用Xcode内置的单元测试框架来测试C函数?

r - 使用 grepl 创建基于另一列的列

bash - 在 bash 中,如何计算变量中的行数?

iphone - 调整 UITableView 标题的大小并包含 UITextView (iOS7 + AutoLayout)

ios - iOS7键盘快捷键是如何实现的?

objective-c - Java 的 List 在 Objective-C 中的等价物是什么?

ios - 如何使用 Swift 3 iOS 应用程序读取 plist

c - 字符串终止符 '\0' 与整数常量 0 的值如何相同?

objective-c - UITextView setBorderStyle 在 iOS 5 模拟器中崩溃