如何在UITableViewCell上添加rss图像

时间:2013-12-24 19:09:20

标签: ios iphone uitableview uiimage rss-reader

我使用以下代码加载RSS源。我可以知道如何将RSS中的小图像添加到UITableViewCell的左侧?非常感谢

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [stories count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier];
    }

    // Set up the cell
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    [cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
    cell.textLabel.font = [UIFont fontWithName:@"Heiti TC Light" size:16.0];
    cell.textColor=[UIColor blackColor];//according to you.
     cell.textLabel.numberOfLines = 3;
    cell.backgroundColor = [UIColor clearColor];
    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"white.jpg"]];
    self.tableView.backgroundColor = [UIColor clearColor];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

    NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];

    // clean up the link - get rid of spaces, returns, and tabs...
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"  " withString:@""];

    NSLog(@"link: %@", storyLink);
    // open in Safari
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if ([stories count] == 0) {
        NSString * path = @"http://www.medworm.com/rss/medicalfeeds/source/Nature.xml";
        [self parseXMLFileAtURL:path];
    }

    cellSize = CGSizeMake([newsTable bounds].size.width, 60);
}

1 个答案:

答案 0 :(得分:1)

我希望这会有所帮助。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                 reuseIdentifier:MyIdentifier];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(12, 5, 158/2, 99/2) ];
    //SDWebImage Extension
    [imgView setImageWithURL:[NSURL URLWithString:@"http://www.yoururl.com/yourimage.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    [self.contentView addSubview:imgView];
}
return cell;
}