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

发布于 2024-12-08 07:39:30 字数 8131 浏览 0 评论 0原文

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

请注意,我在控制台上看到来自 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

    

I am newer in iPhone application development. I want to find out why tableview cell is not showing data. I tried a lot of ways. I am giving my code below.

Note that I am seeing data at my console which is coming from XML file but it's not displaying in UITableView cell.

    
    @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

    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听你说爱我 2024-12-15 07:39:30

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

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

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

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

Try to set value of property textLabel, not detailedTextLabel :

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

Also try to create cell using predefined styles:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: MyIdentifier];
等风也等你 2024-12-15 07:39:30

要制作细胞,您应该使用

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

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

To make a cell you should use

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

method. By default the detailTextLabel is not visible in the cell.
Try to use other styles - UITableViewCellStyleValue1, for example.

私藏温柔 2024-12-15 07:39:30

不要给出,而是

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

在此之前,只需检查您的数组(故事)是否有内容?

谢谢..

Instead of giving,

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

try this code:

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

before that ,just check your array(stories)having content?

Thank You..

夏末染殇 2024-12-15 07:39:30
cell.detailTextLabel.text = [stories objectAtIndex:storyIndex];

用下面的语句替换上面的语句我认为我会工作

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

replace above statement with belo wstatement iy will work i think

 cell.detailTextLabel.text = [stories objectAtIndex:indexPath.row];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文