0

私は現在、ニュース アプリに取り組んでおり、ニュース記事の画像の表の行に画像を追加したいと考えています。

他のすべてを解析できましたが、画像の URL を解析できないだけです。

これは、フィードから HTML をストライピングするために使用したコードです。

.M ファイル

- (NSString *)stringByStrippingHTML:(NSString *)inputString 
{
NSMutableString *outString;

if (inputString)
{
    outString = [[NSMutableString alloc] initWithString:inputString];

    if ([inputString length] > 0)
    {
        NSRange r;

        while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        {
            [outString deleteCharactersInRange:r];
        }      
    }
}

return outString; 

}

.H ファイル

- (NSString *)stringByStrippingHTML:(NSString *)inputString;

私は一般的にxcodeとコードにまったく慣れていないので、このことに慣れていません。

画像のURLを解析するだけではわかりません。

これは tablerow の外観のコードです。

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.numberOfLines=3;
cell.detailTextLabel.numberOfLines=4;


RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];

NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];

NSString * articleDescrptionString = [[[NSString alloc] init] autorelease];
articleDescrptionString = entry.articleDescription;

cell.textLabel.text = entry.articleTitle; 

NSString *plainString = [self stringByStrippingHTML:entry.articleDescription ];

cell.imageView.image = [UIImage imageNamed: entry.articleImageUrl];

//NSString *rangedString = [plainString substringToIndex:249];  //0 to 249 makes it 250 characters

//cell.detailTextLabel.text = articleDescrptionString;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, plainString];

//UIImage *theImage = [UIImage imageWithContentsOfFile:entry.articleImageUrl];
//cell.imageView.image = theImage;

return cell;
}

私がコメントアウトしたコードは、現在使用されていないため、現在使用されていません。

4

1 に答える 1

0

最初にイメージを NSData オブジェクトにロードする必要があります...

NSURL *imgURL = [NSURL URLWithString:entry.articleImageUrl];
NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
cell.imageView.image = [UIImage imageWithData:imgData];

ただし、非同期で実行する必要があります。画像を遅延ロードせずに実行すると、スクロール中にTableViewがストックされるためです...

于 2013-03-15T10:50:22.457 に答える