我对这段代码感到非常沮丧,已经过了好几周,我无法找到问题的解决方案。 我的RSS应用程序与一些RSS工作很好,它成功地将图像从文章拉到缩略图到我的表格视图。我知道RSS XML类型的文件在这个结构中看起来很棒,它很有效:
<item>
<title>" title going to be here "</title>
<description>
here is the description plus the image file <p><img src='http://img.mako.co.il/2014/05/20/maytal_7_a.jpg'/></p>
</description>
<link>
here is going to be the link
</link>
我的问题是有些RSS没有描述中的图像,所以我需要将它从自己的文章中删除。 我当前的cellForRowAtIndexPath代码是这样的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NewsItemCell *cell = (NewsItemCell *)[tableView dequeueReusableCellWithIdentifier:STORYBOARD_KEY__ID__NEWS_ITEM_TABLE_CELL];
if (cell == nil)
{
cell = [[NewsItemCell alloc] initWithFrame:CGRectMake(0, 0, MAIN_TABLE_CELL__NEWS_ITEM_CELL_WIDTH, MAIN_TABLE_CELL__NEWS_ITEM_CELL_HEIGHT)];
}
NewsItem *currentNewsItem = [self.newsItemsForSingleSource objectAtIndex:indexPath.row];
NSString* imageURL = currentNewsItem.imageUrl;
if ( [imageURL length] > 0 ) {
[cell.thumbnailImageView setImageWithURL: [NSURL URLWithString: [imageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] ];
}
else {
// placeholder if no image available
// cell.thumbnailImageView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0.9];
NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"placeholder_1.png"];
[cell.thumbnailImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
}
cell.titleLabelView.text = currentNewsItem.title;
cell.newsItem = currentNewsItem;
return cell;
}
在我的其他应用程序中,我使用这种代码模式来抓取文章的第一个图像并且它的效果非常好:
MWFeedItem *item = [listadoArticulos objectAtIndex:indexPath.row];
// my Method to search and fetch the pictures to cell
if (item) {
NSString *htmlContent = item.content;
NSString *imgSrc;
// find match for image
NSRange rangeOfString = NSMakeRange(0, [htmlContent length]);
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(<img.*?src=\")(.*?)(\".*?>)" options:0 error:nil];
if ([htmlContent length] > 0) {
NSTextCheckingResult *match = [regex firstMatchInString:htmlContent options:0 range:rangeOfString];
if (match != NULL ) {
NSString *imgUrl = [htmlContent substringWithRange:[match rangeAtIndex:2]];
NSLog(@"url: %@", imgUrl);
NSLog(@"match %@", match);
if ([[imgUrl lowercaseString] rangeOfString:@"feedburner"].location == NSNotFound) {
imgSrc = imgUrl;
[cell.myCellImage setImageWithURL:[NSURL URLWithString:imgSrc] placeholderImage:[UIImage imageNamed:@"customItem30.png"]];
}}}}
我的问题是,我如何将它们两者结合起来?或者也许有人可以考虑其他解决方案(比我的代码更好)。 任何帮助将不胜感激 。
编辑**:哦,我忘了把CellForRowAtIndex
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NewsItemCell *cell = (NewsItemCell *)[tableView dequeueReusableCellWithIdentifier:STORYBOARD_KEY__ID__NEWS_ITEM_TABLE_CELL];
if (cell == nil)
{
cell = [[NewsItemCell alloc] initWithFrame:CGRectMake(0, 0, MAIN_TABLE_CELL__NEWS_ITEM_CELL_WIDTH, MAIN_TABLE_CELL__NEWS_ITEM_CELL_HEIGHT)];
}
NewsItem *currentNewsItem = [self.newsItemsForSingleSource objectAtIndex:indexPath.row];
NSString* imageURL = currentNewsItem.imageUrl;
if ( [imageURL length] > 0 ) {
[cell.thumbnailImageView setImageWithURL: [NSURL URLWithString: [imageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] ];
}
else {
// placeholder if no image available
// cell.thumbnailImageView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0.9];
NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"placeholder_1.png"];
[cell.thumbnailImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
}
cell.titleLabelView.text = currentNewsItem.title;
cell.newsItem = currentNewsItem;
return cell;
}
答案 0 :(得分:0)
首先,您可以使用正则表达式(另一个应用程序)检查RSS提要是否包含描述标记中的任何图像URL。如果存在图像,则只需创建新闻项对象并添加该文章。
NSMutableArry * newsItemsForSingleSource = [[NSMutableArray alloc]init];
for(int i=0;i<listadoArticulos.count;i++)
{
MWFeedItem *item = [listadoArticulos objectAtIndex:i];
// my Method to search and fetch the pictures to cell
if (item) {
NSString *htmlContent = item.content;
NSString *imgSrc;
// find match for image
NSRange rangeOfString = NSMakeRange(0, [htmlContent length]);
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(<img.*?src=\")(.*?)(\".*?>)" options:0 error:nil];
if ([htmlContent length] > 0) {
NSTextCheckingResult *match = [regex firstMatchInString:htmlContent options:0 range:rangeOfString];
if (match != NULL ) {
NSString *imgUrl = [htmlContent substringWithRange:[match rangeAtIndex:2]];
NSLog(@"url: %@", imgUrl);
NSLog(@"match %@", match);
if ([[imgUrl lowercaseString] rangeOfString:@"feedburner"].location == NSNotFound) {
imgSrc = imgUrl;
NewsItem * currentNewsItem = [[NewsItem alloc]init];
currentNewsItem.imageUrl = imgSrc;
currentNewsItem.title = @"title";
currentNewsItem.Description = @"description";
[self.newsItemsForSingleSource addObject:currentNewsItem];
[currentNewsItem release];
}}}}
}
然后在tableview cellForRowAtIndexPath中填充NewsItem。你有新闻肯定包含图片。