x86cam
x86cam

Reputation: 425

iOS - Enclosure URL being detected, but not shown for different items

I am currently trying to get images to be displayed in my Table View. But, it looks like when it gets the last image, it sets all of the images to that.

The image is from my first blog post. enter image description here

Here's my code for the RSS detection:

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return feeds.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
    NSString *textFiltered = [[feeds objectAtIndex:indexPath.row] objectForKey: @"description"];
    cell.detailTextLabel.text = [textFiltered stringByConvertingHTMLToPlainText];
    //cell.detailTextLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"pubDate"];
    //NSString *imageURLString = [[feeds objectAtIndex:indexPath.row] objectForKey: @"enclosure"];

                                NSData *imageData = [NSData dataWithContentsOfURL:imageURL];

                                cell.imageView.image = [UIImage imageWithData:imageData];


    return cell;
}

#pragma mark - parsing of RssFeed Values

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    element = elementName;

    if ([element isEqualToString:@"item"]) {
        item = [[NSMutableDictionary alloc]init];
        title = [[NSMutableString alloc]init];
        link = [[NSMutableString alloc] init];
        desc = [[NSMutableString alloc] init];
        image = [[NSMutableString alloc] init];
        pubDate = [[NSMutableString alloc] init];
        feedEnclosure = [[NSMutableString alloc] init];
        encodedContent = [[NSMutableString alloc] init];
        //imageURL = [[NSURL alloc] init];
    }if ([element isEqualToString:@"enclosure"]){
        NSString *imageStr = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
        imageURL = [NSURL URLWithString:imageStr];
    }

}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"item"]) {

        [item setObject:title forKey:@"title"];
        [item setObject:link forKey:@"link"];
        [item setObject:desc forKey:@"description"];
        [item setObject:pubDate forKey:@"pubDate"];
        [item setObject:feedEnclosure forKey:@"enclosure"];
        [item setObject:encodedContent forKey:@"content:encoded"];
        [item setObject:imageURL forKey:@"url"];
        [feeds addObject:[item copy]];


    }
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([element isEqualToString:@"title"]) {
        [title appendString:string];
    }else if ([element isEqualToString:@"link"]){
        [link appendString:string];
    }else if ([element isEqualToString:@"description"]){
        [desc appendString:string];
    }else if ([element isEqualToString:@"pubDate"]){
        [pubDate appendString:string];
    }else if ([element isEqualToString:@"content:encoded"]){
        [encodedContent appendString:string];
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.tableView reloadData];

}

Do you people have an idea on how I could get this working?

Upvotes: 0

Views: 409

Answers (1)

rmaddy
rmaddy

Reputation: 318854

You are using the ivar imageURL to set the image of each row in the table. This of course means every row will have the same image. You need to store the individual image URLs for each row in the row-specific data.

You seem to be setting the url key in your row-specific data. Use that instead of imageURL when setting up each cell.

Also note that it is a very bad idea to be loading the NSData object on the main thread for each cell. This will make your UI very choppy and unresponsive if the Internet connection is slow at all. There are many solutions to loading the images in the background.

Update:

Try this:

NSURL *url = feeds[indexPath.row][@"url"];
NSData *imageData = [NSData dataWithContentsIfURL:url];

Upvotes: 1

Related Questions