I have an iPhone app were I parse JSON to a Table View and display the data in a custom cell. I have three different JSON files containing the same structure but with different data. I also have three different UIButtons displaying as a tab bar on the same page, like this:
I have two different images for the buttons - one when it's selected and one when it's unselected.
My code for parsing the JSON file looks like this:
#import "DEMOSecondViewController.h"
#import "DEMONavigationController.h"
#import "PostsObject.h"
#import "AFNetworking.h"
@interface DEMOSecondViewController ()
@end
@implementation DEMOSecondViewController
@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView, movies = _movies;
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"iCelebri.com";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu"
style:UIBarButtonItemStylePlain
target:(DEMONavigationController *)self.navigationController
action:@selector(showMenu)];
self.tableView.separatorColor = [UIColor clearColor];
// Setting Up Activity Indicator View
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.activityIndicatorView.hidesWhenStopped = YES;
self.activityIndicatorView.center = self.view.center;
[self.view addSubview:self.activityIndicatorView];
[self.activityIndicatorView startAnimating];
self.tableView.separatorColor = [UIColor clearColor];
// Initializing Data Source
self.movies = [[NSArray alloc] init];
NSURL *url = [[NSURL alloc] initWithString:@"http://my-site.com/jsonfile.php?name=Name"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.movies = JSON;
[self.activityIndicatorView stopAnimating];
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}
// Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.movies && self.movies.count) {
return self.movies.count;
} else {
return 0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 124;
}
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"PostsObject";
PostsObject *cell = (PostsObject *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PostsObject" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundView = imageView;
}
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
cell.title.text = [movie objectForKey:@"message"];
cell.published.text = [movie objectForKey:@"published"];
return cell;
}
@end
So when I click for example the Twitter button, I want the NSURL *url = [[NSURL alloc] initWithString:@"http://my-site.com/jsonfile.php?name=Name"];
to change the link to another. It's like a UISegmentControl but with UIButtons instead.
How is this possible?
Thanks.
First connect to a property the 3 UIButtons on the tabBar I called them for the example:
btnFaceBook
btnTwitter
btnTwitter2
Set for the 3 UIButtons the URL for that tab in -viewDidLoad like this:
[btnFaceBook setTitle:@"http://URLFacebook.com" forState:UIControlStateDisabled];
[btnTwitter setTitle:@"http://URLTwitter.com" forState:UIControlStateDisabled];
[btnTwitter2 setTitle:@"http://URLTwitter2.com" forState:UIControlStateDisabled];
Set for the 3 UIButtons the images for Unpressed and pressed, You can do that from your nib file or like that programatically:
[btnFaceBook setBackgroundImage:[UIImage imageNamed:@"imageFacebookUnpressedUnSelected.png"] forState:UIControlStateNormal];
[btnFaceBook setBackgroundImage:[UIImage imageNamed:@"imageFacebookPressedSelected.png"] forState:UIControlStateSelected];
[btnTwitter setBackgroundImage:[UIImage imageNamed:@"imageTwitterUnpressedUnSelected.png"] forState:UIControlStateNormal];
[btnTwitter setBackgroundImage:[UIImage imageNamed:@"imageTwitterPressedSelected.png"] forState:UIControlStateSelected];
[btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"imageTwitter2UnpressedUnSelected.png"] forState:UIControlStateNormal];
[btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"imageTwitter2PressedSelected.png"] forState:UIControlStateSelected];
Then connect all 3 UIButton's TouchUpInside action to this method:
- (IBAction)btnFromTabBarClicked:(UIButton *)sender
{
//Unselect all 3 buttons
btnFaceBook.selected = btnTwitter.selected = btnTwitter2.selected = NO;
//Select the button that was clicked
sender.selected = YES;
//Set the string of an NSMutableString property called strURLToLoad with the URL
//The URL is pre stored in the text of the UIButton in the Disabled text.
[strURLToLoad setString:[sender titleForState:UIControlStateDisabled]];
//Load the URL
[self loadJSONFromCurrentURL];
}
You can simply create a property for your NSURL so you can access it from all your class methods. Modify your code like this:
@interface DEMOSecondViewController ()
@property (strong) NSURL *url;
@end
Then in your viewDidLoad:
url = [[NSURL alloc] initWithString:@"http://my-site.com/jsonfile.php?name=Name"];
Then create an action for your buttons:
// Facebook button example. Do the same for the other 2 buttons
- (IBAction)facebookButton:(id)sender {
url = [NSURL URLWithString:@"yourNewUrl"];
// Here you need to call again your AFNewtorking code (maybe put it in a sperate method instead of the viewDidLoad)
}
I hope I well explained the answer :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.