Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6003165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:04:59+00:00 2026-05-23T01:04:59+00:00

I have a tableview with a segmentedControl in the header, i want the segment

  • 0

I have a tableview with a segmentedControl in the header, i want the segment control to change the data depending on which segment is clicked, in this case Upcoming/Weekly.

Both data sources come from an XML file on my server, which is the part im having trouble with, as im still new to using NSXMLParser

ive messed around with some code, but to no avail. I know that my segmentAction is being picked up because ive used logs to do so. attached is code and an image of my table, cheers

When i click a segmented control, the tableview doesnt update with the new XML source

🙂

enter image description here

RootViewController.h

#import <UIKit/UIKit.h>

@class XMLAppDelegate, BookDetailViewController;

@interface RootViewController : UITableViewController 
<UITableViewDelegate> 
{

    XMLAppDelegate *appDelegate;
    BookDetailViewController *bdvController;
    UITableViewCell *eventUpCVCell;

    UILabel *month;
    UILabel *title;
    UILabel *date;
    UISegmentedControl *segmentedControl;

}
@property (nonatomic, retain) IBOutlet UITableViewCell *eventUpCVCell;
@property (nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl;

@property (nonatomic, retain) IBOutlet UILabel *month;
@property (nonatomic, retain) IBOutlet UILabel *title;
@property (nonatomic, retain) IBOutlet UILabel *date;
@end

RootViewController.m

#import "RootViewController.h"
#import "XMLAppDelegate.h"
#import "Book.h"
#import "BookDetailViewController.h"

#define kLabel1Tag 1
#define kLabel2Tag 2
#define kLabel3Tag 3

@implementation RootViewController
@synthesize eventUpCVCell, segmentedControl;
@synthesize month, title, date;
-(void)viewDidLoad
{

    appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];

    self.title = @"Upcoming Events";

    [super viewDidLoad];

    NSURL *urlUpcoming = [[NSURL alloc] initWithString:@"http://www.randomosity.com.au/EventsUpcoming.xml"];
    NSURL *urlWeekly = [[NSURL alloc] initWithString:@"http://www.randomosity.com.au/EventsWeekly.xml"];
}


-(IBAction)segmentedAction:(id)sender
{
XMLAppDelegate *mainDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];

    segmentedControl = (UISegmentedControl *)sender;

    if (segmentedControl.selectedSegmentIndex == 0)
    {

        mainDelegate.url = [[NSURL alloc] initWithString:@"http://www.randomosity.com.au/EventsUpcoming.xml"];
        [self.tableView reloadData];
    }
    else if (segmentedControl.selectedSegmentIndex == 1)
    {
        NSLog(@"Hello 2");

        mainDelegate.url = [[NSURL alloc] initWithString:@"http://www.randomosity.com.au/EventsWeekly.xml"];
        [self.tableView reloadData];
    }


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
{
    return 66;  

}

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


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

etc etc...

XMLAppDelegate.h

#import <UIKit/UIKit.h>

@interface XMLAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;
    NSString *url;
    NSMutableArray *eventDataUp;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain) NSMutableArray *eventDataUp;

@end

XMLAppDelegate.m

#import "XMLAppDelegate.h"
#import "RootViewController.h"
#import "XMLParser.h"

@implementation XMLAppDelegate

@synthesize window;
@synthesize navigationController, eventDataUp;
@synthesize url;


- (void)applicationDidFinishLaunching:(UIApplication *)application {

    //RootViewController *rvController = [[RootViewController alloc] init];



    NSURL *urlUpcoming = [[NSURL alloc] initWithString:@"http://www.randomosity.com.au/EventsUpcoming.xml"];
   // NSURL *urlWeekly = [[NSURL alloc] initWithString:@"http://www.randomosity.com.au/EventsWeekly.xml"];
    NSURL *url = urlUpcoming;
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

    //Initialize the delegate.
    XMLParser *parser = [[XMLParser alloc] initXMLParser];

    //Set delegate
    [xmlParser setDelegate:parser];

    //Start parsing the XML file.
    BOOL success = [xmlParser parse];

    if(success)
        NSLog(@"No Errors");
    else
        NSLog(@"Error Error Error!!!");

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Save data if appropriate
}


- (void)dealloc {
    [eventDataUp release];
    [navigationController release];
    [window release];
    [super dealloc];
}

@end

XMLParser

#import <UIKit/UIKit.h>

@class XMLAppDelegate, Book;

@interface XMLParser : NSObject {

    NSMutableString *currentElementValue;

    XMLAppDelegate *appDelegate;
    Book *eventUp; 
}

- (XMLParser *) initXMLParser;

@end


//
//  XMLParser.m
//  XML
//
//  Created by iPhone SDK Articles on 11/23/08.
//  Copyright 2008 www.iPhoneSDKArticles.com.
//

#import "XMLParser.h"
#import "XMLAppDelegate.h"
#import "Book.h"

@implementation XMLParser

- (XMLParser *) initXMLParser {

    [super init];

    appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];

    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"EventsUpcoming"]) {
        //Initialize the array.
        appDelegate.eventDataUp = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Event"]) {

        //Initialize the book.
        eventUp = [[Book alloc] init];

        //Extract the attribute here.
        eventUp.eventUpID = [[attributeDict objectForKey:@"id"] integerValue];

        NSLog(@"Reading id value :%i", eventUp.eventUpID);
    }

    NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

    NSLog(@"Processing Value: %@", currentElementValue);

}

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

    if([elementName isEqualToString:@"EventsUpcoming"])
        return;

    //There is nothing to do if we encounter the Books element here.
    //If we encounter the Book element howevere, we want to add the book object to the array
    // and release the object.
    if([elementName isEqualToString:@"Event"]) {
        [appDelegate.eventDataUp addObject:eventUp];

        [eventUp release];
        eventUp = nil;
    }
    else 
        [eventUp setValue:currentElementValue forKey:elementName];

    [currentElementValue release];
    currentElementValue = nil;
}

- (void) dealloc {

    [eventUp release];
    [currentElementValue release];
    [super dealloc];
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:05:00+00:00Added an answer on May 23, 2026 at 1:05 am

    Quite a lot of code to digg through, but I think the problem lays here:

           if (segmentedControl.selectedSegmentIndex == 0)
            {
    
                mainDelegate.url = [[NSURL alloc] initWithString:@"http://www.randomosity.com.au/EventsUpcoming.xml"];
                [self.tableView reloadData];
            }
            else if (segmentedControl.selectedSegmentIndex == 1)
            {
                NSLog(@"Hello 2");
    
                mainDelegate.url = [[NSURL alloc]initWithString:@"http://www.randomosity.com.au/EventsWeekly.xml"];
                NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:mainDelegate.url];
    
                XMLParser *parser = [[XMLParser alloc] initXMLParser];
                [parser parse];
                [parser release];
        }
    

    You are changing the url, and reloading the data in the tableview. But not parsing the xml again with the new url.

    EDIT
    You can parse the XML just like you did before, but now place it somewhere so it’s called again. Only I would call the [self.tableView reloadData] when you surely know when the parser is finished. In the parserDidEndDocument delegate method for instance, but that depends on how you format your code.

    Check code above to see example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tableview with a navigationBar with a segmentedControl on the top of
I have a segmented control on the top of a tableView that I would
In my application I have a tableview which a couple of cells on click
I have a grouped tableView with 5 sections and i want to add buttons
Not even sure if this is feasible, but here's the use case: I have
I have this code: - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row { if ([[tableColumn identifier]
i have all that data with me in tableview but when i dont know
I have a TableView in which I can scroll to view all the rows.
I have a TableView in which I am adding custom UILabels to the UITableViewCells.
I have a grouped table view with textfields in the tableview. For the keyboard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.