为什么图像没有从url的xml文件加载而没有在表视图中显示

时间:2012-12-05 05:12:25

标签: iphone ios ios6

我正在从网址中检索XML文件,我通过解析它获得了图像。我的XML文件包含这样的内容,

<Products>
<products id="1">
<img>http://images.com/images/image1.jpg</img>
</products>
<products id="2">
<img>http://images.com/images/image2.jpg</img>
</products>
<products id="3">
<img>http://images.com/images/image3.jpg</img>
</products>
</Products>

Tableviewcontroller.m有这样的代码,

- (void)viewDidLoad{
 [super viewDidLoad];
 xmlParser = [[XMLParser alloc] loadXMLByURL:@"http://images.com/Products.xml"];
 [self.tableView reloadData];}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [xmlParser.tweets count];}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Products";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];}
UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:1];
cell.imageView.image = currentTweet;
[cell.contentView addSubview:self.productImage];
return cell;}

这里xmlParser是类XMLParser的对象,tweets是数组。产品图像是UIImageview的参考。 现在我的问题是,当我运行应用程序时,它将tableview中的图像显示为缩略图,但我想显示图像,如下图所示。

Desired output

在故事板中引用UIImage视图时也很困惑,当我在.h文件中将productImage定义为UIImageView并尝试连接故事板时,它向我显示错误,如非法配置:连接“productImage”不能将原型对象作为其目标。所以请告诉我如何在故事板中连接它。

请建议我解决这个问题,

3 个答案:

答案 0 :(得分:3)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Products";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];}

UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UIImageView *tweetImageView = [UIImageView alloc] 
                           initWithFrame:CGRectMake(0,0,
                           cell.frame.size.width , cell.frame.size.height)];
tweetImageView.image = currentTweet;
[cell.contentView addSubview:tweetImageView];

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return self.view.frame.size.height/3;
}

答案 1 :(得分:2)

您正在尝试将ID类型分配给UIImage

尝试使用此代码为您的单元格加载图像

NSURL *imageURL = [NSURL URLWithString:[[xmlParser tweets] objectAtIndex:1]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *currentTweet = [UIImage imageWithData:imageData];
cell.imageView.image = currentTweet;

答案 2 :(得分:1)

1)问题:在表格视图中获取缩略图图像,但您想在背景中显示该图像(完整单元格宽度)我是对的吗?

解决方案

以下面提到的方式,您的图片可以在背景中传播

 UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:1];
 UIImageView *tweetImageView = [UIImageView alloc] 
                               initWithFrame:CGRectMake(0,0,
                               cell.frame.size.width , cell.frame.size.height)];
 tweetImageView.image = currentTweet;
 [cell.contentView addSubview:tweetImageView];

另一个选择是您可以设置单元格的背景图像,如:

[cell setBackgroundView:tweetImageView]

更新:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cell == nil) 
     {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                      reuseIdentifier:CellIdentifier];
     }
     UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
     UIImageView *tweetImageView = [UIImageView alloc] 
                                   initWithFrame:CGRectMake(0,0,
                                   cell.frame.size.width , cell.frame.size.height)];
     tweetImageView.image = currentTweet;
     [cell.contentView addSubview:tweetImageView];
     return cell;
}

不要创建单独的图像视图而是使用一个图像视图。

并且每行(indexPath.row)将不同的图像传递给imageview。

对于每一行创建一个新单元格,其新的imageview将从数组

中获取相应的图像

希望它有所帮助 :)