XML Data with UIScrollView

Discussion in 'iOS Programming' started by dcastal1, Oct 11, 2010.

  1. dcastal1 macrumors newbie

    Joined:
    Oct 11, 2010
    #1
    I am populating a UIScrollView from data collected via XML. I'd like the user to press a "Next" button on the view to be able to see more data relative to the data in the displayed view. When they click the button, a new view is presented, with the relevant data.
    The problem I'm having is, whenever I try to pass the information from the initial element into the new view, all I get is the LAST element in the XML set. So, I need to be able to save the element set at the point of the button select, and pass that to the new view, however, I can't seem to figure it out.
    Is what I'm trying to do possible?
    The pertinent function is:
    Code:
    
    -(void)layoutScrollView
    {
    	CGRect workingFrame;
    	
    	workingFrame.origin.x = 0;
    	workingFrame.origin.y = 0;
    	workingFrame.size.width = 768;
    	workingFrame.size.height = 1024;
    	
    	NoelsMyRealtorXMLView *myView;
    	
    	for(NoelsMyRealtorXMLElement *element in [self xmlElementObjects])
    	{
    		NSString *trimmedString = [[element title1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];		
    		NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NoelsMyRealtorXMLView" owner:nil options:nil];
    		
    		for(id currentObject in topLevelObjects)
    		{
    			if([currentObject isKindOfClass:[NoelsMyRealtorXMLView class]])
    			{
    				myView = (NoelsMyRealtorXMLView *)currentObject;
    			}
    		}		
    		UIImage *t_Image_View = [[UIImage alloc]init];
    		t_Image_View = [element imageView];
    		tempImageView = t_Image_View;
    		self.tempImageView = element.imageView;
    		[[myView v_imageView]		setImage:[element imageView]];
    		[[myView v_title1]			setText: [element title1]];
    		[[myView v_photodate1]		setText: [element photodate1]];
    		[[myView v_mls]				setText: [element MLS]];
    		[myView						setFrame:workingFrame];
    		
    		
    		UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    		btn.frame = CGRectMake(301, 636, 165, 30);
    		[btn setTitle:@"See More Info" forState:UIControlStateNormal];
    		[btn addTarget:self action:@selector(flipView:) forControlEvents:UIControlEventTouchUpInside];
    		[btn setTag:1];
    		btn.alpha = .5;
    		currentMLS = [[NSMutableString alloc] initWithString:[element MLS]];
    		[myView addSubview:btn];
    		
    		[scrollView addSubview:myView];
    		
    		workingFrame.origin.x = workingFrame.origin.x + 768; 
    	}
    	
    	workingFrame.size.width = workingFrame.origin.x;
    	[scrollView setContentSize:workingFrame.size];
    	
    	workingFrame.origin.x = 0;
    	workingFrame.origin.y = 0;
    	workingFrame.size.width = 768;
    	workingFrame.size.height = 1024;
    	
    	[scrollView setFrame:workingFrame];
    }
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Is there some reason you aren't using a UITableView?
     
  3. dcastal1 thread starter macrumors newbie

    Joined:
    Oct 11, 2010
    #3
    I'm not sure I understand what you mean. Do you mean a tableview instead of a scrollview?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I suggest you take a closer look at the documentation, specifically the superclass.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Yeah, that's what I meant. But maybe I am confused as to the purpose of the scroll view. Care to elaborate? Is it a paging kind of scroll view?
     
  6. dcastal1 thread starter macrumors newbie

    Joined:
    Oct 11, 2010
    #6
    OIC.
    You're suggesting that instead of the view that I'm using, use a tableview. This would suggest I could pass along properties of the cell to the "Next" view? Forgive my newbie-ness...I'm pretty new to Objective-C, xcode and the entire platform.
    I'm writing an app for a realtor. It has bunch of pictures of houses that I want to be able to scroll thru. Upon selecting the house, I want to display more detailed images of the house. So, scroll thru a high level set, then drill to a more detailed set.
     
  7. dcastal1 thread starter macrumors newbie

    Joined:
    Oct 11, 2010
    #7
    Upon further consideration, you're wondering why I don't use a table view instead of a scroll view. No real reason I guess. ScrollView seemed like it was the straightforward way to do this. However, that may not be the case. Although, my understanding is that a table view will only allow vertical scrolling, and I'd prefer horizontal.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Ah, that answers my question. Thanks.

    So, what does your code for flipView: look like?
     
  9. dcastal1 thread starter macrumors newbie

    Joined:
    Oct 11, 2010
    #9
    Here's the current iteration of flipview:
    Code:
    -(IBAction)flipView:(id)sender{
    	WebViewController *webView = [[[WebViewController alloc] init] autorelease];
    	[webView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    
    	if ([sender tag] == 1) {
    		webView.webURL = currentMLS;
    		webView.v_tempImageView = tempImageView;
    	}else {
    		webView.webURL = currentMapURL;
    	}
    	[self presentModalViewController:webView animated:YES];
    }
    
    
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Seems to me that your currentMLS instance variable is being set each time through your loop in layoutScrollView, so when your loop is done, it contains the last one. Instead you should look into setting it inside flipView: based on which button triggered the call.

    Which brings me to: Also seems to me that your tag is not dynamic enough (i.e. you set it to 1 every time through your loop). You probably want to set this more dynamically and then have flipView: set currentMLS based on the tag.
     
  11. dcastal1 thread starter macrumors newbie

    Joined:
    Oct 11, 2010
    #11
    Sweet! Thanks for the tip on the tag. I was initially planning on using that for something else, but changed my mind. However, I changed the design to store the elements from XML into an array, then use the tag to pass the index number of the array to the flipView function. That accessed the array data and passed that to the new view.
    Thanks for your help and for not being hard on a newb.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    I like the way you think! :)

    You're welcome. Thank you for responding to my questions promptly and for having a grasp of the fundamentals, as well as treating those trying to help you with respect. It's appreciated.

    I think you're gonna fit in fine here. :cool:
     

Share This Page