【发布时间】:2012-01-22 21:48:08
【问题描述】:
我想查看一个字符串,它是苹果 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"];
}
但它从未被调用过,entry 是一个包含标题的 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 作业。诡异的???我向下滚动,当我返回时,所有重新分配和初始化的出列单元都有史蒂夫·乔布斯的照片。在开始时,当我打开应用程序时,一些标题中没有 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;
}
【问题讨论】:
-
请给出几个错误匹配“Steve”的单元格的文本。
-
您是否要从不是“Steve”的回收单元格中删除图像?在我看来它不像。
-
与史蒂夫错误匹配的几个单元格的文本:苹果董事会声明一天 1 亿次 OSX Lion 下载
-
然后当我向上滚动时,所有回收的单元格都有史蒂夫·乔布斯的形象
-
“然后当我向上滚动所有回收的单元格时,所有的回收单元都有史蒂夫·乔布斯的图像”——那是因为你没有重置回收单元中的图像。 (尝试在适当的位置添加
else { cell.imageView.image = nil; }。)
标签: objective-c xcode string compare substring