在表视图单元格中显示NSMutableArray

时间:2010-01-13 08:53:14

标签: iphone cocoa-touch uitableview nsmutablearray

如何在表格视图单元格中显示arrayData? 我想将表格视图单元格的标签文本设置为解析数据中的“ename”。 请帮帮我。

先谢谢。

- (void)viewDidLoad {

    NSString *url = [NSString stringWithFormat:@"http://mmabigshow.com/app/get_result_from_query.php?q=select * from event"];

    NSString *escapedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    [self performSelector:@selector(startParsing:) withObject:escapedUrl afterDelay:1.0];

    NSLog(@"View Did Load End");*/
     [super viewDidLoad];

}
- (void) startParsing: (NSString *) query{

    NSLog(@"Start Of Parsing");
    NSURL *xmlURL = [[NSURL alloc] initWithString:query];
    NSData *myData = [NSData dataWithContentsOfURL:xmlURL];
    NSString *myStr = [[NSString alloc] initWithData:myData encoding:NSWindowsCP1252StringEncoding];
    myStr = [myStr stringByReplacingOccurrencesOfString:@"encoding=\"windows-1252\"" withString:@""];
    NSData* aData = [myStr dataUsingEncoding:NSUTF8StringEncoding];

    rssParser = [[NSXMLParser alloc] initWithData:aData];
    [rssParser setDelegate:self];
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];    
    [rssParser parse];
    NSLog(@"End of Parsing");

}


#pragma mark xml parser
#pragma mark -

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"table"]){
    item = [[NSMutableDictionary alloc] init];
    currentEid = [[NSMutableString alloc] init];
    currentEname = [[NSMutableString alloc] init];
    currentEurl = [[NSMutableString alloc] init];
    dataArray = [[NSMutableArray alloc] init];
    }
    //NSLog(@"didStartElement");
    }

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     

    if ([elementName isEqualToString:@"table"]) {
        // save values to an item, then store that item into the array...
        [item setObject:currentEname forKey:@"ename"];
        //NSLog(@"%@",item);
        [dataArray addObject:[[item copy] autorelease]];
        //NSLog(@"%@",dataArray);
        }
    [currentEid release];
    [currentEname release];
    [currentEurl release];

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([currentElement isEqualToString:@"eid"]) {
        [currentEid appendString:string];
    } else if ([currentElement isEqualToString:@"ename"]) {
        [currentEname appendString:string];
        //NSLog(@"%@",currentEname);
    } else if ([currentElement isEqualToString:@"eurl"]) {
        [currentEurl appendString:string];
    } 
    //  NSLog(@"foundCharacters");
}


#pragma mark - 
#pragma mark Table Data Source Methods 
- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"numberOfRowsInSection");

    return [self.dataArray count]; 

}
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"cellForRowAtIndexPath");
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier "; 

    TableDetailsCell *cell = (TableDetailsCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 
    if (cell == nil) { 
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableDetailsCell" owner:self options:nil]; 
        for (id oneObject in nib) 
            if ([oneObject isKindOfClass:[TableDetailsCell class]]) 
                cell = (TableDetailsCell *)oneObject; 
    } 
    NSUInteger row = [indexPath row]; 

    NSDictionary *rowData = [self.dataArray objectAtIndex:row]; 
    //NSLog(@"%@",rowData);

    cell.text = [rowData objectForKey:@"ename"]; 
    cell.labelName.text= [rowData objectForKey:@"ename"];




    return cell; 
} 

2 个答案:

答案 0 :(得分:2)

我相信你的问题是你并没有将dataArray称为self.dataArray,这可能导致你的程序无法正常工作。

然后通过

分配
cell.textLabel.text = [self.dataArray objectAtIndex:indexRow.row];

希望这有帮助

Seb Kade “我来帮忙”

答案 1 :(得分:0)

Apple的SeismicXML app就是这样做的。查看rootViewController的cellForRowAtIndexPath。

另外,请确保您的tableview在Interface Builder中正确设置了委托和数据源(如果您选择使用它)。