iOS:向UITableView添加数组 - 显示为一个项目

时间:2012-04-12 18:03:40

标签: ios uitableview

我遇到的问题是我有一个标记为:_podAllEntries的NSMutableArray,其中包含url路径。然后将此数组的内容加载到UITableView中的一行中。此数组中总共有4个项目,每个项目的表格中应该有一个条目。以下是有关应用程序如何运作的更详细的探索:

此iOS应用使用NSURL来使用网站上的XML数据:http://undignified.podbean.com/feed。从那里我使用NSXMLParser解析XML并在“enclosure”元素中搜索名为“url”的属性。然后将匹配“url”的所有属性添加到数组中,然后应将其加载到UITableView中,每个项目位于单独的行上(如前所述,只填充了一行)。

我可能在这一点上遗漏了一些未成熟的东西,并且朝着正确的方向轻推或任何反馈都会非常感激。

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

3 个答案:

答案 0 :(得分:0)

您不了解如何在iOS中构建UITableView。 您应该在UITableViewDataSource协议中填充UITableView。

您应阅读有关UITableView的Apple信息。

请勿使用insertRowsAtIndexPaths添加“主”行。

答案 1 :(得分: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];

答案 2 :(得分:0)

  1. 在cellForRowAtIndexPath中,您应该[NSString stringWithFormat:@"%@", _podAllEntries];而不是[NSString stringWithFormat:@"%@", [_podAllEntries objectAtIndex:indexPath.row]];。这将确保表的每一行都有一个_podAllEntries数组的不同行。

  2. Jonas是对的,你不应该做insertRowsAtIndexPaths。您定义的委托方法负责确保添加所有内容。

  3. 您_podAllEntries有多少项?它看起来像:[_podAllEntries insertObject:parser.links atIndex:0];会添加一个对象,它本身就是一个数组。因此,你只有一行。我想你想要[_podAllEntries insertObjectsFromArray:parser.links]