0

私が直面している問題は、URL パスを含む _podAllEntries というラベルの付いた NSMutableArray があることです。この配列の内容は、UITableView の 1 つの行に読み込まれます。この配列には合計 4 つの項目があり、項目ごとにテーブルにエントリが必要です。以下は、アプリの仕組みのより詳細な説明です。

この iOS アプリは、NSURL を使用して Web サイト ( http://undignified.podbean.com/feed ) から XML データを使用します。そこから、NSXMLParser を使用して XML を解析し、「enclosure」要素内の「url」という属性を検索します。次に、「url」に一致するすべての属性が配列に追加され、各項目が個別の行にある UITableView に読み込まれる必要があります (前述のように、1 行だけが入力されています)。

私はおそらくこの時点で些細なことを見逃しているので、正しい方向への微調整やフィードバックをいただければ幸いです。

UnDigParser.h

クラスを解析するためのヘッダー ファイル:

#import <Foundation/Foundation.h>

@interface UnDigParser : NSXMLParser <NSXMLParserDelegate> {

}
@property (retain) NSMutableArray *links;
@end

UnDigParser.m

解析クラスの実装:

#import "UnDigParser.h"

@implementation UnDigParser
NSMutableArray *_links;

@synthesize links = _links;

-(void)parserDidStartDocument:(NSXMLParser *)parser{
    _links=[[NSMutableArray alloc] init];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"enclosure"]){

        NSString *link = [attributeDict objectForKey:@"url"];
        if (link){
            [_links addObject:link];
        }
    }
    //NSLog(@"%@",_links);
}

-(BOOL)parse{
    self.delegate = self;
    return [super parse];
}

@end

ViewController.h

@class WebViewController;

@interface getpodViewController : UITableViewController <UITableViewDataSource>
{
    NSMutableArray *_podAllEntries;
    NSMutableArray *_allEntries;
    WebViewController *_webViewController;
}

@property(retain) NSMutableArray *podAllEntries;
@property(retain) NSMutableArray *allEntries;
@property(retain) WebViewController *webViewController;

@end

ViewController.m

@implementation getpodViewController

@synthesize podAllEntries = _podAllEntries;
@synthesize allEntries = _allEntries;
@synthesize webViewController = _webViewController;

-(void)addRows
{
    //dispatch_async(dispatch_get_global_queue(0, 0), ^{

    NSURL *url = [NSURL URLWithString:@"http://undignified.podbean.com/feed"];
    UnDigParser *parser = [[UnDigParser alloc] initWithContentsOfURL:url];      

    [parser parse];
    [_podAllEntries insertObject:parser.links atIndex:0];
    NSLog(@"%@", _podAllEntries);

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]
                          withRowAnimation:UITableViewRowAnimationRight];
}

- (void)viewDidLoad 
{
    [super viewDidLoad];

    self.title = @"Podcasts";
    self.podAllEntries = [[NSMutableArray alloc]init];
    [self addRows];
}

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

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *cellValue = [NSString stringWithFormat:@"%@", _podAllEntries];

    cell.textLabel.text = cellValue;
    return cell;
}
4

3 に答える 3

0

次のようにする必要があります。

 [NSString stringWithFormat:@"%@", [_podAllEntries objectAtIndex:indexPath.row]];

例:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *cellValue = [NSString stringWithFormat:@"%@", [_podAllEntries objectAtIndex:indexPath.row]];

    cell.textLabel.text = cellValue;

    /* to use less code, you can also do it like this: */
    //cell.textLabel.text = [_podAllEntries objectAtIndex:indexPath.row];

    return cell;
}

セルが実際に作成される場所であるaddRowsため、次のステートメントを削除することもできます。cellForRowAtIndexPath:

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
于 2012-04-12T18:18:02.593 に答える
0
  1. cellForRowAtIndexPath では、[NSString stringWithFormat:@"%@", _podAllEntries];必要ではなく[NSString stringWithFormat:@"%@", [_podAllEntries objectAtIndex:indexPath.row]];. これにより、テーブルの各行に _podAllEntries 配列の異なる行が含まれるようになります。

  2. ジョナスは、あなたがやるべきではないというのは正しいinsertRowsAtIndexPaths. 定義したデリゲート メソッドは、すべてが確実に追加されるように処理します。

  3. _podAllEntries にはいくつのアイテムがありますか? それは次のようになります:[_podAllEntries insertObject:parser.links atIndex:0];それ自体が配列である単一のオブジェクトを追加します。したがって、行は 1 つしかありません。欲しいと思います[_podAllEntries insertObjectsFromArray:parser.links]

于 2012-04-12T18:26:31.823 に答える
0

iOS で UITableView を構築する方法を理解していませんでした。UITableViewDataSourceプロトコル全体で UITableView を設定する必要があります。

UITableViewに関する Apple Info を読む必要があります。

insertRowsAtIndexPaths「メイン」行の追加には使用しないでください。

于 2012-04-12T18:08:42.207 に答える