1

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のリファレンスです。今私の問題は、アプリを実行すると、テーブルビューに画像がサムネイル画像として表示されることですが、下のスクリーンショットのように画像を表示したいです。

望ましい出力

また、ストーリーボードでUIImageビューを参照する際に混乱しています.hファイルでproductImageをUIImageViewとして定義し、ストーリーボードに接続しようとすると、Illegal Configurationのようなエラーが表示されます:接続 "productImage"はプロトタイプオブジェクトを宛先として持つことができません. ストーリーボードでも接続する方法を教えてください。

これを解決するために私に親切に提案してください、

4

3 に答える 3

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;
}
于 2012-12-05T05:40:35.623 に答える
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;
于 2012-12-05T05:27:25.397 に答える
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];

もう1つのオプションは、セルの背景画像を次のように設定できることです。

[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;
}

個別の画像ビューを作成せず、代わりに1つの画像ビューを使用してください。

そして、すべての行(indexPath.row)について、異なる画像をimageviewに渡します。

行ごとに新しいセルが作成され、その新しいimageviewは対応する画像を配列からフェッチします

それが役に立てば幸い :)

于 2012-12-05T05:43:43.907 に答える