2017-07-26 44 views
1

加载图像在我的RSS提要应用程序,我通过输入didStartElement方法解析用的NSXMLParser图片:SDWebImage不会从URL中的UITableViewCell

If ([element isEqualToString: @ "item"]) { 
         
        Item = [[NSMutableDictionary alloc] init]; 
        Title = [[NSMutableString alloc] init]; 
        Link = [[NSMutableString alloc] init]; 
        PubDate = [[NSMutableString alloc] init]; 
        ImageUrl = [[NSMutableString alloc] init]; 
    } 
     
    If ([element isEqualToString: @ "enclosure"]) { 
        NSString * name = [attributeDict objectForKey: @ "url"]; 
        NSLog (@ "The URL is:% @", name); 
         
    } 

而在didEndElement方法:

If ([elementName isEqualToString: @ "item"]) { 
        [Item setObject: title forKey: @ "title"]; 
        [Item setObject: link forKey: @ "link"]; 
        [Item setObject: pubDate forKey: @ "pubDate"]; 
        [Item setObject: imageUrl forKey: @ "url"]; 
        [Feeds addObject: [item copy]]; 
    } 

好的。这工作,我拥有他们所有的链接。

现在我想将这些URL加载到我的UITableView中的单元格中。所以我使用了'SDWebImage'窗格。

cellForRowAtIndexPath方法中我有:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];  
    cell.textLabel.numberOfLines = 0; 
    [cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping]; 
    cell.detailTextLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"pubDate"]; 
    NSString *str = [[feeds objectAtIndex:indexPath.row] objectForKey: @"pubDate"]; 
    cell.detailTextLabel.text = str; 
    NSString * string = [[feeds objectAtIndex:indexPath.row] objectForKey: @"url"]; 
    NSLog(@"THE URL IS: %@", string); 

    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"placeholder.gif"]]; 

    return cell; 

    } 

没有发生。这是我目前的情况。

enter image description here

如何解决?

+0

你能打印'字符串的值和'[NSURL URLWithString:string]'值吗?该网址可能不是 – nathan

+0

@nathan yep。我解决了这个问题,但现在我又遇到了另一个问题:可以看到图像,但是当我点击一张图像时,它会改变它的大小并变得更长。为什么?看看http://imgur.com/YhSUiBL – Taprolano

+0

不,不,我有简单的图片.jpg来自url – Taprolano

回答

1

sd_setImageWithURL:(null的NSURL *)网址

API允许为空的值,所以你需要检查,如果两个string[NSURL URLWithString: string]是非零使用它们之前。

+0

是的。我解决了这个问题,谢谢.. !!但现在我有另一个问题:可以看到图像,但是当我点击图像时,它会改变它的尺寸并变得更长。为什么?查看imgur.com/YhSUiBL – Taprolano

+0

发布'didSelectRowAt'方法的内容,并验证UIImage是否有任何附加的IBAction/UIGestureRecognizer – nathan

+0

好,新问题在线..https://stackoverflow.com/questions/45338779/the -width-的图像功能于UITableViewCell中,变化到的点击 – Taprolano