【问题标题】:why images not loading from xml file of url and not showing in table view为什么图像没有从 url 的 xml 文件加载并且没有显示在表格视图中
【发布时间】:2012-12-05 05:12:24
【问题描述】:

我正在从 url 检索 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 中将图像显示为缩略图,但我想将图像显示为下面的屏幕截图。

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

请建议我解决这个问题,

【问题讨论】:

  • 在该图片网址中没有图片,所有......

标签: iphone ios ios6


【解决方案1】:
- (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;
}

【讨论】:

  • 你的答案成功了,但一个小变化是[cell setBackgroundView:tweetImageView]; 而不是[cell.contentView addSubview:tweetImageView]; 非常感谢pranav,还有一个问题是在最后一张图片下方有一个空的小空间,请参阅此链接[链接]@987654321 @ [/link] 对于该图像问题,请告诉我如何解决它
  • @sathya 它可能是您在其上添加表格视图的视图(白色),或者您在表格视图中添加了更多行数。将单元格的高度设为 (view'sHeight/3)
  • view'sHeight/3 就像我必须编写一个函数,你的意思是对吗?或者我们有什么选项可以在 stoaryboard 中设置
  • @sathya 有一个代表,我在答案中添加了它。请检查您正在使用的组件的 Apple 参考资料......他们已经记录了所有这些内容。
  • 我遇到了一个问题,比如正在检索 XML 文件中列出的表视图中的所有图像。如果我需要显示特定的,我使用了 objectAtIndex:1 2,3 等等,但这仅显示所有单元格中的最后一个图像。我想做的是从不同单元格中列出的 xml 文件中获取特定图像,其中包含来自 xml 文件的不同图像
【解决方案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;

【讨论】:

  • 它向我显示了这样的错误-[UIImage length]: unrecognized selector sent to instance 0x71c3e00 and app terminates say Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized选择器发送到实例 0x71c3e00'
  • 你能检查[[xmlParser tweets] objectAtIndex:1]是否返回字符串。
  • 您正在尝试访问不是数组或包含 0 个对象的数组的东西。检查[xmlParser tweets] 是什么类使用这个NSLog(@"%@", [[xmlParser tweets] class]);
【解决方案3】:

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 将从数组中获取相应的图像

希望对你有帮助 :)

【讨论】:

  • 是的,我知道了,但是我需要三个不同的图像,我如何分配给单个 UIImageview 变量
  • @sathya load image w.r.t table view row index,检查我的答案
  • @swati 我已根据图像将 objectAtIndex 值分别更改为 0、1、2,但我在所有 3 个单元格中检索到相同的图像如何为 3 个单元格分配单独的图像
  • 你能告诉我你是如何使用 3 三个索引的(请分享与此相关的代码部分)
  • 通过与上面代码中显示的相同方式,我只是将 objectAtIndex 分别更改为 0,1 和 2。还为 UIImage 和 UIImageView 赋予不同的名称以避免名称冲突
猜你喜欢
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多