UITableView单元格文本未显示iPhone应用程序开发中的xml数据

问题描述 投票:1回答:4

我是iPhone应用程序开发的新手。我想找出为什么tableview单元格没有显示数据。我尝试了很多方法。我在下面给出我的代码。

请注意,我在我的控制台上看到的数据来自XML文件,但它没有显示在UITableView单元格中。

    
    @synthesize newsTable;
    @synthesize  activityIndicator; 
    @synthesize rssParser; 
    @synthesize  stories;   
    @synthesize  item;
    @synthesize  currentElement; 
    @synthesize  currentTitle, currentDate, currentSummary, currentLink;


    // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    /*
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization.
        }
        return self;
    }
    */

    #pragma mark -
    #pragma mark Parsing

    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    }


    - (void)viewDidAppear:(BOOL)animated 
    { 
        [super viewDidAppear:animated]; 
        if ([stories count] == 0) { 
            NSString * path = @"http://icms7.bitmascot.com:8080/webcommander2.0S2/rest/services/catalogue/getAllDummyCategoryProduct"; 
            [self parseXMLFileAtURL:path]; 
        } 
        cellSize = CGSizeMake([newsTable bounds].size.width, 60); 
    }


    - (void)parseXMLFileAtURL:(NSString *)URL 
    { 
        stories = [[NSMutableArray alloc] init]; 
        //you must then convert the path to a proper NSURL or it won't work 
        NSURL *xmlURL = [NSURL URLWithString:URL]; 
        // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error 
        // this may be necessary only for the toolchain 
        rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; 
        // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks. 
        [rssParser setDelegate:self]; 
        // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser. 
        [rssParser setShouldProcessNamespaces:NO]; 
        [rssParser setShouldReportNamespacePrefixes:NO]; 
        [rssParser setShouldResolveExternalEntities:NO]; 
        [rssParser parse];
    }

    - (void)parserDidStartDocument:(NSXMLParser *)parser 
        { 
            NSLog(@"found file and started parsing"); 
        } 
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 
        { 
            NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i )", [parseError code]]; 
            NSLog(@"error parsing XML: %@", errorString); 
            UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
            [errorAlert show]; 
        } 
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
        NSLog(@"found this element: %@", elementName); 
        currentElement = [elementName copy]; 
        if ([elementName isEqualToString:@"ProductData"]) 
        { 
            // clear out our story item caches... 
            item = [[NSMutableDictionary alloc] init]; 
            currentTitle = [[NSMutableString alloc] init]; 
            currentDate = [[NSMutableString alloc] init]; 
            currentSummary = [[NSMutableString alloc] init]; 
            currentLink = [[NSMutableString alloc] init]; 
        }
    } 
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 
        //NSLog(@"ended element: %@", elementName); 
        if ([elementName isEqualToString:@"ProductData"]) 
            { // save values to an item, then store that item into the array... 
                [item setObject:currentTitle forKey:@"id"]; 
                [item setObject:currentLink forKey:@"productNumber"]; 
                [item setObject:currentSummary forKey:@"name"]; 
                [item setObject:currentDate forKey:@"dateCreated"]; 
                [stories addObject:[item copy]];
                NSLog(@"adding story: %@", currentTitle); 
            } 
    } 
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
        NSLog(@"found characters: %@", string); 
        // save the characters for the current item... 
        if ([currentElement isEqualToString:@"ProductData"]) 
            { 
                [currentTitle appendString:string]; 
            } 
        else if ([currentElement isEqualToString:@"id"]) 
            { 
                [currentLink appendString:string]; 
            } 
        else if ([currentElement isEqualToString:@"ProductNumber"]) 
        { 
            [currentSummary appendString:string]; 
        } 
        else if ([currentElement isEqualToString:@"dateCreatrd"]) 
        { 
            [currentDate appendString:string]; 
        } 
    } 
    - (void)parserDidEndDocument:(NSXMLParser *)parser { 
        [activityIndicator stopAnimating]; 
        [activityIndicator removeFromSuperview]; 
        NSLog(@"all done!"); 
        NSLog(@"stories array has %d items", [stories count]);
        NSLog(@"data in stories: %@",[stories description]);
        [newsTable reloadData]; 
    }


    #pragma mark tableView


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

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

    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

        static NSString *MyIdentifier = @"MyIdentifier"; 
        UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:MyIdentifier]; 
        if (cell == nil) 
        { 

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: MyIdentifier];

        } 

        // Set up the cell 
        int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
        //[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
        //[cell setLabelText:[[stories objectAtIndex:storyIndex] objectForKey: @"ProductData"]];
        //[cell setText:[stories objectAtIndex:storyIndex]];
        cell.textLabel.text = [stories objectAtIndex:storyIndex];
        NSLog(@"%@ ",cell.textLabel.text);

        //cell.detailTextLabel.text = [stories objectAtIndex:indexPath.row];

        return cell; 
    }


    /*
    - (void)setLabelText:(NSString *)_text{
        UILabel *cellText;
        cellText.text= _text;
        [cellText sizeToFit];
    }
    */
    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations.
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    */

    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc. that aren't in use.
    }

    - (void)viewDidUnload {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }


    - (void)dealloc { 
            [currentElement release]; 
            [rssParser release]; 
            [stories release];
            [item release]; 
            [currentTitle release];
            [currentDate release]; 
            [currentSummary release]; 
            [currentLink release]; 
            [super dealloc]; 
        }


    @end

    
iphone uitableview nsxmlparser
4个回答
2
投票

尝试设置属性textLabel的值,而不是detailedTextLabel

cell.textLabel.text = [[stories objectAtIndex:storyIndex] valueForKey:@"productNumber"];

还尝试使用预定义样式创建cell

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: MyIdentifier];

1
投票

要制作一个你应该使用的单元格

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

方法。默认情况下,detailTextLabel在单元格中不可见。尝试使用其他样式 - 例如UITableViewCellStyleValue1。


1
投票

而不是给予,

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
//[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
//[cell setLabelText:[[stories objectAtIndex:storyIndex] objectForKey: @"ProductData"]];
//[cell setText:[stories objectAtIndex:storyIndex]];
cell.detailTextLabel.text = [stories objectAtIndex:storyIndex];

试试这段代码:

cell.detailTextLabel.text = [stories objectAtIndex:indexPath.row];

在此之前,只检查你的阵列(故事)有内容?

谢谢..


1
投票
cell.detailTextLabel.text = [stories objectAtIndex:storyIndex];

用以下陈述取代上述陈述,我认为这将有效

 cell.detailTextLabel.text = [stories objectAtIndex:indexPath.row];
© www.soinside.com 2019 - 2024. All rights reserved.