开发者

Display NSMutableArray in Table View Cell

开发者 https://www.devze.com 2022-12-16 01:12 出处:网络
How could I display arrayData in table view cell? I want to set the table view cell\'s label text to the \'ename\' from the data parsed.

How could I display arrayData in table view cell? I want to set the table view cell's label text to the 'ename' from the data parsed. Please help me out.

Thanks in Advance.

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


I believe your problem is that you are not referring to dataArray as self.dataArray all the time which may cause your program not to work properly.

Then assign via

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

Hope this helped

Seb Kade "I'm here to help"


Apple's SeismicXML app does exactly this. Look at the rootViewController's cellForRowAtIndexPath.

Also, make sure your tableview has it's delegate and datasource set correctly in Interface Builder (if you chose to use it).

0

精彩评论

暂无评论...
验证码 换一张
取 消