I have a class that uses sudzc. All works fine, with NSLog I can watch the data of my web services, but when I want to use an array and then use that array in other place, the property is null. I want to fill a table view with the data of the web services, but I can't, How could do it?
This is my class
//
// RootViewController.m
// StratargetMovil
//
// Created by Giovanni Cortés on 30/03/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "RootViewController.h"
#import "Page2.h"
#interface RootViewController ()
#end
#implementation RootViewController
#synthesize myData = _myData;
#synthesize empresaID = _empresaID;
#synthesize datos = _datos;
#synthesize idUnidadNegocio = _idUnidadNegocio;
#synthesize idArray = _idArray;
-(NSString *)empresaID
{
_empresaID = #"fce";
return _empresaID;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
// Do any additional setup after loading the view from its nib.
}
return self;
}
- (void)viewDidLoad
{
// I want to fill the table view but dont work
EWSEmpresaWebServiceSvc *service = [[EWSEmpresaWebServiceSvc alloc] init];
[service ConsultarUnidadesOrganizacionalesPorEmpresa:self EmpresaId:self.empresaID];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.datos = nil;
self.myData = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Lo de la tabla
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.myData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.myData objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Muestra la vista 2 cuando se selecciona una fila
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
self.idUnidadNegocio = [self.idArray objectAtIndex:indexPath.row];
Page2 *nextNavigator = [[Page2 alloc] init];
[[self navigationController] pushViewController:nextNavigator animated:YES];
[nextNavigator release];
}
// -------------------------------------------------
// Consultar unidades organizaciones por empresa
- (void) ConsultarUnidadesOrganizacionalesPorEmpresaHandler: (id) value {
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(#"%#", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(#"%#", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray *result = (NSMutableArray*)value;
NSMutableArray *unidadOrganizacional = [[NSMutableArray alloc] init];
self.myData = [[[NSMutableArray array] init] autorelease];
for (int i = 0; i < [result count]; i++)
{
EWSUnidadNegocio *empresa = [[EWSUnidadNegocio alloc] init];
empresa = [result objectAtIndex:i];
[unidadOrganizacional addObject:[empresa Descripcion]];
}
self.myData = unidadOrganizacional;
}
-(void)onload:(id)value
{
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(#"%#", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(#"%#", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray *result = (NSMutableArray*)value;
NSMutableArray *unidadOrganizacional = [[NSMutableArray alloc] init];
self.myData = [[[NSMutableArray array] init] autorelease];
for (int i = 0; i < [result count]; i++)
{
EWSUnidadNegocio *empresa = [[EWSUnidadNegocio alloc] init];
empresa = [result objectAtIndex:i];
NSLog(#"%ld -- %#", [empresa Id], [empresa Descripcion]); // With this I can watch the data in the console
[unidadOrganizacional addObject:[empresa Descripcion]];
}
self.myData = unidadOrganizacional; // self.myData in another place is null
}
}
#end
My data is
#property (nonatomic, retain) NSMutableArray *myData;
Thanks
Firstly, this is doing nothing for you:
self.myData = [[[NSMutableArray array] init] autorelease];
...
self.myData = unidadOrganizacional;
You are assigning myData then immediately reassigning it.
Skip the first initialization and just do this:
self.myData = [unidadOrganizacional mutableCopy];
I suspect you are losing the pointer to the unidadOrganizacional array at some point and that is why you are getting null for myData.
Personally though I would just do this initialize myData in the viewDidLoad call and then add the objects to myData directly:
- (void)viewDidLoad
{
self.myData = [[NSMutableArray alloc] init];
// I want to fill the table view but dont work
EWSEmpresaWebServiceSvc *service = [[EWSEmpresaWebServiceSvc alloc] init];
[service ConsultarUnidadesOrganizacionalesPorEmpresa:self EmpresaId:self.empresaID];
}
and then:
- (void) ConsultarUnidadesOrganizacionalesPorEmpresaHandler: (id) value {
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(#"%#", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(#"%#", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray *result = (NSMutableArray*)value;
for (int i = 0; i < [result count]; i++)
{
EWSUnidadNegocio *empresa = [[EWSUnidadNegocio alloc] init];
empresa = [result objectAtIndex:i];
[self.myData addObject:[empresa Descripcion]];
}
}
-(void)onload:(id)value
{
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(#"%#", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(#"%#", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray *result = (NSMutableArray*)value;
for (int i = 0; i < [result count]; i++)
{
EWSUnidadNegocio *empresa = [[EWSUnidadNegocio alloc] init];
empresa = [result objectAtIndex:i];
NSLog(#"%ld -- %#", [empresa Id], [empresa Descripcion]); // With this I can watch the data in the console
[self.myData addObject:[empresa Descripcion]];
}
}
You may want to clear out myData before the ConsultarUnidadesOrganizacionalesPorEmpresa call, I'm not really sure what the different between that and onload is really. It looks like they are duplicates of each other.
Good luck!
I think myData autoreleased.
You can alloc and init the myData array but not auto-/release it. (remove autorelease at the myData allocation in the
ConsultarUnidadesOrganizacionalesPorEmpresaHandler method)
Don't forget to add a dealloc method for releasing the myData (and other) properties.
Related
Hi My data is getting duplicated every time I use the pull to refresh.. for eg. 5 initial entries in the table view are doubled to 10 with one pull to refresh and adding 5 more in the subsequent pull to refresh. How can I stop the duplication.. I would like to make sure that only new items are downloaded and existing data in the table is not downloaded again.
#implementation RootViewController
#synthesize allEntries = _allEntries;
#synthesize feeds = _feeds;
#synthesize queue = _queue;
#synthesize webViewController = _webViewController;
#pragma mark -
#pragma mark View lifecycle
- (void)addRows {
RSSEntry *entry1 = [[[RSSEntry alloc] initWithBlogTitle:#"1"
articleTitle:#"1"
articleUrl:#"1"
articleDate:[NSDate date]] autorelease];
RSSEntry *entry2 = [[[RSSEntry alloc] initWithBlogTitle:#"2"
articleTitle:#"2"
articleUrl:#"2"
articleDate:[NSDate date]] autorelease];
RSSEntry *entry3 = [[[RSSEntry alloc] initWithBlogTitle:#"3"
articleTitle:#"3"
articleUrl:#"3"
articleDate:[NSDate date]] autorelease];
[_allEntries insertObject:entry1 atIndex:0];
[_allEntries insertObject:entry2 atIndex:0];
[_allEntries insertObject:entry3 atIndex:0];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Songs";
self.allEntries = [NSMutableArray array];
self.queue = [[[NSOperationQueue alloc] init] autorelease];
self.feeds = [NSArray arrayWithObjects:
#"http://hindisongs.bestofindia.co/?feed=rss2",
nil];
self.refreshControl = [UIRefreshControl new];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to refresh"];
[self.refreshControl addTarget:self action:#selector(bindDatas) forControlEvents:UIControlEventValueChanged];
[self bindDatas]; //called at the first time
}
-(void)bindDatas
{
//GET YOUR DATAS HERE…
for (NSString *feed in _feeds) {
NSURL *url = [NSURL URLWithString:feed];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[_queue addOperation:request];
}
//update the tableView
[self.tableView reloadData];
if(self.refreshControl != nil && self.refreshControl.isRefreshing == TRUE)
{
[self.refreshControl endRefreshing];
}
}
- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
NSArray *channels = [rootElement elementsForName:#"channel"];
for (GDataXMLElement *channel in channels) {
NSString *blogTitle = [channel valueForChild:#"title"];
NSArray *items = [channel elementsForName:#"item"];
for (GDataXMLElement *item in items) {
NSString *articleTitle = [item valueForChild:#"title"];
NSString *articleUrl = [item valueForChild:#"link"];
NSString *articleDateString = [item valueForChild:#"pubDate"];
NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle
articleTitle:articleTitle
articleUrl:articleUrl
articleDate:articleDate] autorelease];
[entries addObject:entry];
}
}
}
- (void)parseAtom:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
NSString *blogTitle = [rootElement valueForChild:#"title"];
NSArray *items = [rootElement elementsForName:#"entry"];
for (GDataXMLElement *item in items) {
NSString *articleTitle = [item valueForChild:#"title"];
NSString *articleUrl = nil;
NSArray *links = [item elementsForName:#"link"];
for(GDataXMLElement *link in links) {
NSString *rel = [[link attributeForName:#"rel"] stringValue];
NSString *type = [[link attributeForName:#"type"] stringValue];
if ([rel compare:#"alternate"] == NSOrderedSame &&
[type compare:#"text/html"] == NSOrderedSame) {
articleUrl = [[link attributeForName:#"href"] stringValue];
}
}
NSString *articleDateString = [item valueForChild:#"updated"];
NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC3339];
RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle
articleTitle:articleTitle
articleUrl:articleUrl
articleDate:articleDate] autorelease];
[entries addObject:entry];
}
}
- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
if ([rootElement.name compare:#"rss"] == NSOrderedSame) {
[self parseRss:rootElement entries:entries];
} else if ([rootElement.name compare:#"feed"] == NSOrderedSame) {
[self parseAtom:rootElement entries:entries];
} else {
NSLog(#"Unsupported root element: %#", rootElement.name);
}
}
- (void)requestFinished:(ASIHTTPRequest *)request {
[_queue addOperationWithBlock:^{
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
options:0 error:&error];
if (doc == nil) {
NSLog(#"Failed to parse %#", request.url);
} else {
NSMutableArray *entries = [NSMutableArray array];
[self parseFeed:doc.rootElement entries:entries];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
for (RSSEntry *entry in entries) {
int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
RSSEntry *entry1 = (RSSEntry *) a;
RSSEntry *entry2 = (RSSEntry *) b;
return [entry1.articleDate compare:entry2.articleDate];
}];
[_allEntries insertObject:entry atIndex:insertIdx];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
}
}];
}
}];
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
NSLog(#"Error: %#", error);
}
/*
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_allEntries count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
cell.textLabel.text = entry.articleTitle;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# - %#", articleDateString, entry.blogTitle];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_webViewController == nil) {
self.webViewController = [[[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
self.webViewController = nil;
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[_allEntries release];
_allEntries = nil;
[_queue release];
_queue = nil;
[_feeds release];
_feeds = nil;
[_webViewController release];
_webViewController = nil;
[super dealloc];
}
The issue is that you keep inserting object to _allEntries without ever reseting it. You will want to empty it out at some point, either when the user pulls to refresh or when the new data comes in before you add any new objects to it.
[_allEntries removeAllObjects];
Try putting it at the start of bindDatas.
When I launch my app, tap on a cell in my RootViewController (AufnahmeIstTableViewController), it opens up my DetailViewController (AufnahmeISTDetailTableViewController).When I then tap on cell, it should open a third ViewController (AufnahmeIstDetailDetailViewController). My app works like in this tutorial: http://www.youtube.com/watch?v=99Ssk1-HUq4
But when I tap on a cell and open my third view, the labels on my third view controller don't display any text as declared in the code.
What do I have to change? Here are all my files:
Here is my AufnahmeIstTableViewController.h file:
#import <UIKit/UIKit.h>
#interface AufnahmeIstTableViewController : UITableViewController
#property (strong, nonatomic) NSMutableArray *categoryArray;
#end
Here's my AufnahmeIstTableViewController.m file:
#import "AufnahmeIstTableViewController.h"
#import "AufnahmeISTDetailTableViewController.h"
#interface AufnahmeIstTableViewController ()
#end
#implementation AufnahmeIstTableViewController
#synthesize categoryArray;
-(NSMutableArray *)categoryArray
{
if (!categoryArray)
{
categoryArray = [[NSMutableArray alloc]init];
}
return categoryArray;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.categoryArray addObject:#"Gesetze"];
[self.categoryArray addObject:#"Verordnungen"];
[self.categoryArray addObject:#"Technische Regeln"];
[self.categoryArray addObject:#"Berufsgenossenschaft"];
[self.categoryArray addObject:#"Management"];
[self.categoryArray addObject:#"Personal"];
[self.categoryArray addObject:#"Vertrieb"];
[self.categoryArray addObject:#"Kunden"];
[self.categoryArray addObject:#"Lieferanten"];
[self.categoryArray addObject:#"Arbeitsumgebung"];
[self.categoryArray addObject:#"Produktion"];
[self.categoryArray addObject:#"Produkte"];
[self.categoryArray addObject:#"Messmittel"];
[self.categoryArray addObject:#"Informationssicherheit"];
[self.categoryArray addObject:#"Rechnungswesen"];
[self.categoryArray addObject:#"Dritte"];
[self setTitle:#"Ist-Aufnahme"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.categoryArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = self.categoryArray [indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AufnahmeISTDetailTableViewController *categories = [[AufnahmeISTDetailTableViewController alloc]init];
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Gesetze"])
categories.istAufnahmeInt = 0;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Verordnungen"])
categories.istAufnahmeInt = 1;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Technische Regeln"])
categories.istAufnahmeInt = 2;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Berufsgenossenschaft"])
categories.istAufnahmeInt = 3;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Management"])
categories.istAufnahmeInt = 4;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Personal"])
categories.istAufnahmeInt = 5;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Vertrieb"])
categories.istAufnahmeInt = 6;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Kunden"])
categories.istAufnahmeInt = 7;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Lieferanten"])
categories.istAufnahmeInt = 8;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Arbeitsumgebung"])
categories.istAufnahmeInt = 9;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Produktion"])
categories.istAufnahmeInt = 10;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Produkte"])
categories.istAufnahmeInt = 11;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Messmittel"])
categories.istAufnahmeInt = 12;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Informationssicherheit"])
categories.istAufnahmeInt = 13;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Rechnungswesen"])
categories.istAufnahmeInt = 14;
if ([[self.categoryArray objectAtIndex:indexPath.row] isEqual:#"Dritte"])
categories.istAufnahmeInt = 15;
[categories setTitle:[self.categoryArray objectAtIndex:indexPath.row]];
//[self.navigationController pushViewController:categories animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Navigation
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"DetailView"])
{
AufnahmeISTDetailTableViewController *controller = (AufnahmeISTDetailTableViewController *)segue.destinationViewController;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Gesetze"])
controller.istAufnahmeInt = 0;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Verordnungen"])
controller.istAufnahmeInt = 1;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Technische Regeln"])
controller.istAufnahmeInt = 2;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Berufsgenossenschaft"])
controller.istAufnahmeInt = 3;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Management"])
controller.istAufnahmeInt = 4;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Personal"])
controller.istAufnahmeInt = 5;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Vertrieb"])
controller.istAufnahmeInt = 6;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Kunden"])
controller.istAufnahmeInt = 7;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Lieferanten"])
controller.istAufnahmeInt = 8;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Arbeitsumgebung"])
controller.istAufnahmeInt = 9;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Produktion"])
controller.istAufnahmeInt = 10;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Produkte"])
controller.istAufnahmeInt = 11;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Messmittel"])
controller.istAufnahmeInt = 12;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Informationssicherheit"])
controller.istAufnahmeInt = 13;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Rechnungswesen"])
controller.istAufnahmeInt = 14;
if ([[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row] isEqual:#"Dritte"])
controller.istAufnahmeInt = 15;
[controller setTitle:[self.categoryArray objectAtIndex:[self.tableView indexPathForSelectedRow].row]];
}
}
#end
Here's my AufnahmeISTDetailTableViewController.h file:
#import <UIKit/UIKit.h>
#import "AufnahmeIstTableViewController.h"
#import "AufnahmeIstDetailDetailViewController.h"
#interface AufnahmeISTDetailTableViewController : UITableViewController {
NSMutableArray *gesetzeArray;
NSMutableArray *verordnungenArray;
NSMutableArray *technischeregelnArray;
NSMutableArray *berufsgenossenschaftArray;
NSMutableArray *managementArray;
NSMutableArray *personalArray;
NSMutableArray *vertriebArray;
NSMutableArray *kundenArray;
NSMutableArray *lieferantenArray;
NSMutableArray *arbeitsumgebungArray;
NSMutableArray *produktionArray;
NSMutableArray *produkteArray;
NSMutableArray *messmittelArray;
NSMutableArray *informationssicherheitArray;
NSMutableArray *rechnungswesenArray;
NSMutableArray *dritteArray;
}
#property int istAufnahmeInt;
#property AufnahmeIstTableViewController *categories;
-(void)makeData;
#end
And here's my AufnahmeISTDetailTableViewController.m file:
#import "AufnahmeISTDetailTableViewController.h"
#import "AufnahmeIstTableViewController.h"
#import "AufnahmeIstDetailDetailViewController.h"
#interface AufnahmeISTDetailTableViewController ()
#end
#implementation AufnahmeISTDetailTableViewController
#synthesize istAufnahmeInt;
#synthesize categories;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self makeData];
}
-(void)makeData
{
gesetzeArray = [[NSMutableArray alloc]init];
verordnungenArray = [[NSMutableArray alloc]init];
technischeregelnArray = [[NSMutableArray alloc]init];
berufsgenossenschaftArray= [[NSMutableArray alloc]init];
managementArray= [[NSMutableArray alloc]init];
personalArray= [[NSMutableArray alloc]init];
vertriebArray= [[NSMutableArray alloc]init];
kundenArray= [[NSMutableArray alloc]init];
lieferantenArray= [[NSMutableArray alloc]init];
arbeitsumgebungArray= [[NSMutableArray alloc]init];
produktionArray= [[NSMutableArray alloc]init];
produkteArray= [[NSMutableArray alloc]init];
messmittelArray= [[NSMutableArray alloc]init];
informationssicherheitArray= [[NSMutableArray alloc]init];
rechnungswesenArray= [[NSMutableArray alloc]init];
dritteArray= [[NSMutableArray alloc]init];
//Gesetze
[gesetzeArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Verordnungen
[verordnungenArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Verordnung zur arbeitsmedizinischen Vorsorge – ArbMedVV",#"name",#"Verordnung zur arbeitsmedizinischen Vorsorge – ArbMedVV wurde gedrückt",#"description", nil]];
//Technische Regeln
[technischeregelnArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Technische Regeln",#"name",#"Technische Regeln wurde gedrückt", #"description", nil]];
//Berufsgenossenschaft
[berufsgenossenschaftArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Berufsgenossenschaft",#"name",#"Berufsgenossenschaft wurde gedrückt", #"description", nil]];
//Management
[managementArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Personal
[personalArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Vertrieb
[vertriebArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Kunden
[kundenArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Lieferanten
[lieferantenArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Arbeitsumgebung
[arbeitsumgebungArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Produktion
[produktionArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Produkte
[produkteArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Messmittel
[messmittelArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Informationssicherheit
[informationssicherheitArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Rechnungswesen
[rechnungswesenArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
//Dritte
[dritteArray addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:#"Bundesdatenschutzgesetz",#"name",#"Bundesdatenschutzgesetz wurde gedrückt", #"description", nil]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (self.istAufnahmeInt == 0)
{
return [gesetzeArray count];
}
if (self.istAufnahmeInt == 1)
{
return [verordnungenArray count];
}
if (self.istAufnahmeInt == 2)
{
return [technischeregelnArray count];
}
if (self.istAufnahmeInt == 3)
{
return [berufsgenossenschaftArray count];
}
if (self.istAufnahmeInt == 4)
{
return [managementArray count];
}
if (self.istAufnahmeInt == 5)
{
return [personalArray count];
}
if (self.istAufnahmeInt == 6)
{
return [vertriebArray count];
}
if (self.istAufnahmeInt == 7)
{
return [kundenArray count];
}
if (self.istAufnahmeInt == 8)
{
return [lieferantenArray count];
}
if (self.istAufnahmeInt == 9)
{
return [arbeitsumgebungArray count];
}
if (self.istAufnahmeInt == 10)
{
return [produktionArray count];
}
if (self.istAufnahmeInt == 11)
{
return [produkteArray count];
}
if (self.istAufnahmeInt == 12)
{
return [messmittelArray count];
}
if (self.istAufnahmeInt == 13)
{
return [informationssicherheitArray count];
}
if (self.istAufnahmeInt == 14)
{
return [rechnungswesenArray count];
}
if (self.istAufnahmeInt == 15)
{
return [dritteArray count];
}
else
{
return 1;
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"DetailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
// Configure the cell...
if (self.istAufnahmeInt == 0) cell.textLabel.text = [[gesetzeArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 1) cell.textLabel.text = [[verordnungenArray objectAtIndex:indexPath.row]objectForKey:#"name" ];
if (self.istAufnahmeInt == 2) cell.textLabel.text = [[technischeregelnArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 3) cell.textLabel.text = [[berufsgenossenschaftArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 4) cell.textLabel.text = [[managementArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 5) cell.textLabel.text = [[personalArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 6) cell.textLabel.text = [[vertriebArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 7) cell.textLabel.text = [[kundenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 8) cell.textLabel.text = [[lieferantenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 9) cell.textLabel.text = [[arbeitsumgebungArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 10) cell.textLabel.text = [[produktionArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 11) cell.textLabel.text = [[produkteArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 12) cell.textLabel.text = [[messmittelArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 13) cell.textLabel.text = [[informationssicherheitArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 14) cell.textLabel.text = [[rechnungswesenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
if (self.istAufnahmeInt == 15) cell.textLabel.text = [[dritteArray objectAtIndex:indexPath.row]objectForKey:#"name"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AufnahmeIstDetailDetailViewController *aufnahmeistDetail =[[AufnahmeIstDetailDetailViewController alloc]initWithNibName:#"AufnahmeIstDetailDetailViewController" bundle:nil];
if (istAufnahmeInt == 0)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[gesetzeArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[gesetzeArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 1)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[verordnungenArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[verordnungenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 2)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[technischeregelnArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[technischeregelnArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 3)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[berufsgenossenschaftArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[berufsgenossenschaftArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 4)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[managementArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[managementArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 5)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[personalArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[personalArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 6)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[vertriebArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[vertriebArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 7)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[kundenArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[kundenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 8)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[lieferantenArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[lieferantenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 9)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[arbeitsumgebungArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[arbeitsumgebungArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 10)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[produktionArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[produktionArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 11)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[produkteArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[produkteArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 12)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[messmittelArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[messmittelArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 13)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[informationssicherheitArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[informationssicherheitArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 14)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[rechnungswesenArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[rechnungswesenArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
if (istAufnahmeInt == 15)
{
aufnahmeistDetail.titelString = [[NSString alloc]initWithString:[[dritteArray objectAtIndex:indexPath.row]objectForKey:#"name"]];
aufnahmeistDetail.title = [[dritteArray objectAtIndex:indexPath.row]objectForKey:#"name"];
}
}
#end
Here's my AufnahmeISTDetailTableViewController.h file:
#import <UIKit/UIKit.h>
#import "AufnahmeISTDetailTableViewController.h"
#interface AufnahmeIstDetailDetailViewController : UIViewController
{
IBOutlet UILabel *titleLabel;
IBOutlet UILabel *textLabel;
NSString *titelString;
NSString *textString;
}
#property (nonatomic, retain) NSString *titelString;
#property (nonatomic, retain) NSString *textString;
#end
And finally, here's my AufnahmeISTDetailTableViewController.m file:
#import "AufnahmeIstDetailDetailViewController.h"
#import "AufnahmeISTDetailTableViewController.h"
#interface AufnahmeIstDetailDetailViewController ()
#end
#implementation AufnahmeIstDetailDetailViewController
#synthesize textString;
#synthesize titelString;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
titleLabel.text = titelString;
textLabel.text = textString;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
My suggestion is to just set breakpoints and ensure that your code that sets the labels is actually setting the correct string values. If you are sure about that, then there must be something wrong with your IB files (clearColor text, maybe?).
On another note, you have a horrendous amount of hardcoding in your view controllers. You should decouple your data from your views.
It sounds like it is either an in-house App or one for OBI given the Messmittel and Lieferanten arrays, but this is out of the scope and with that aside.
tilo's suggestion is correct, if you would like to get a hold of the controller through storyboard and segue to it while passing objects to it too.
Here is what I have noticed in your code, right from the very first AufnahmeIstTableViewController.h:
In your didSelectRowAtIndexPath you don't actually need to check the values against a string, because your array does in fact contains those elements namely "Gesetze", "Verordnungen", "Technische Regeln", "Berufsgenossenschaft" etc.
So basically you could just to:
controller.istAufnahmeInt = [[self.tableView indexPathForSelectedRow] row];
Or safer:
if([self.categoriesArray count] >= [[self.tableView indexPathForSelectedRow] row])
{
controller.istAufnahmeInt = [[self.tableView indexPathForSelectedRow] row];
}
Now in your prepareForSegue:
You're basically doing the same thing there. So what you really need to do is just:
controller.istAufnahmeInt = categories.istAufnahmeInt;
So apply the same logic for your second view controller to your third using till's suggestion to pass the strings to it.
Hope this helps.
in your second view controller, you are setting titelString and textString on a viewController that you don't actually show.
You need to implement prepareForSegue:sender: in your second view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
AufnahmeIstDetailDetailViewController *aufnahmeistDetail = [segue destinationViewController];
int selectedRow = [self.tableView indexPathForSelectedRow].row;
aufnahmeistDetail.titleString = ...; // get the appropriate string based on the selected row
aufnahmeistDetail.textString = ...;
}
I have a pull to refresh setup. It's currently calling [self.tableView reloadData]; but It's not reloading my parsed Json data from the blogData method. Is theres something I'm missing?
My controller is like this:
//
// ARTableViewController.m
// WorldCupLive
//
// Created by Adam Rais on 14/06/2014.
// Copyright (c) 2014 Adam Rais. All rights reserved.
//
#import "ARTableViewController.h"
#import "ARModal.h"
#interface ARTableViewController ()
#end
#implementation ARTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.blogPost = [[ARModal alloc] init];
self.blogPost.jsonMutable = [NSMutableArray array];
for (NSDictionary *post in self.blogPost.blogData) {
ARModal *bp = [ARModal blogPostWithHome:[[post objectForKey:#"home"] objectForKey:#"text"]];
bp.away = [[post objectForKey:#"away"] objectForKey:#"text"];
bp.result = [[post objectForKey:#"result"] objectForKey:#"text"];
bp.info = [post objectForKey:#"info"];
bp.homeImage = [[post objectForKey:#"homeimage"] objectForKey:#"src"];
[self.blogPost.jsonMutable addObject:bp];
}
[self randomBackgroundImage];
self.tableView.contentInset = UIEdgeInsetsMake(0.0f, -10.0f, 0.0f, 0.0f);
// Initialize Refresh Control
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
// Configure Refresh Control
[refreshControl addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventValueChanged];
// Configure View Controller
[self setRefreshControl:refreshControl];
}
-(void)randomBackgroundImage {
UIImage *image = self.blogPost.imageUI;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
self.tableView.backgroundView = imageView;
self.tableView.backgroundView.layer.zPosition -= 1;
}
- (void)refresh:(id)sender
{
NSLog(#"Refreshing");
[self.tableView reloadData];
[self randomBackgroundImage];
// End Refreshing
[(UIRefreshControl *)sender endRefreshing];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.blogPost.jsonMutable count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
// Configure the cell...
ARModal *post = [self.blogPost.jsonMutable objectAtIndex:indexPath.row];
NSData *imageData = [NSData dataWithContentsOfURL:post.jsonURL];
UIImage *image = [UIImage imageWithData:imageData];
cell.textLabel.text = [[[[[post.home stringByAppendingString:#" "]stringByAppendingString:#" "] stringByAppendingString:post.bst] stringByAppendingString:#" "] stringByAppendingString:post.away];
cell.detailTextLabel.text = post.info;
cell.imageView.image = image;
cell.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.000];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
And my modal:
//
// ARModal.m
// WorldCupLive
//
// Created by Adam Rais on 14/06/2014.
// Copyright (c) 2014 Adam Rais. All rights reserved.
//
#import "ARModal.h"
#implementation ARModal
-(id)initWithHome:(NSString *)home {
self = [super init];
if (self) {
_home = home;
_away = nil;
_result = nil;
_info = nil;
_homeImage = nil;
}
return self;
}
+(id)blogPostWithHome:(NSString *)home {
return [[self alloc] initWithHome:home];
}
-(NSArray *)blogData {
NSURL *jsonURL = [NSURL URLWithString:#"http://www.kimonolabs.com/api/2nfgfo2s?apikey=1a1f5f323969d5157af8a8be857026c2"];
NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
NSError *jsonError = nil;
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];
NSArray *jsonArray = [[jsonDictionary objectForKey:#"results"] objectForKey:#"collection1"];
if (blogData == nil) {
blogData = jsonArray;
}
return blogData;
}
-(NSURL *)jsonURL {
return [NSURL URLWithString:self.homeImage];
}
-(NSString *)bst {
NSString *sentence = self.result;
NSString *word = #"-";
NSString *wordTwo = #"00.00";
NSString *wordThree = #"01.00";
NSMutableArray *bstArray = [NSMutableArray array];
if ([sentence rangeOfString:word].location != NSNotFound) {
NSLog(#"Found the string");
[bstArray addObject:sentence];
} else if ([sentence rangeOfString:wordTwo].location != NSNotFound) {
NSLog(#"time is 23:00");
[bstArray addObject:#"23:00"];
} else if ([sentence rangeOfString:wordThree].location != NSNotFound) {
NSLog(#"time is 00:00");
[bstArray addObject:#"00:00"];
} else {
float floatOne = [sentence floatValue];
float floatFinal = floatOne - 1.000000;
NSString *str = [NSString stringWithFormat:#"%f", floatFinal];
NSString *bstFinal = [str substringToIndex:[str length] - 4];
[bstArray addObject:bstFinal];
}
return [bstArray objectAtIndex:0];
}
-(UIImage *)imageUI {
NSArray *imageArray = #[[UIImage imageNamed:#"Algeria"],[UIImage imageNamed:#"Argentina"],[UIImage imageNamed:#"Australia"],[UIImage imageNamed:#"Belgium"],[UIImage imageNamed:#"Bosnia-Herzegovina"],[UIImage imageNamed:#"Switzerland"],[UIImage imageNamed:#"Uruguay"],[UIImage imageNamed:#"USA"],[UIImage imageNamed:#"Brazil"],[UIImage imageNamed:#"Cameroon"],[UIImage imageNamed:#"Chile"],[UIImage imageNamed:#"Colombia"],[UIImage imageNamed:#"Costa Rica"],[UIImage imageNamed:#"Côte d'Ivoire"],[UIImage imageNamed:#"Croatia"],[UIImage imageNamed:#"Ecuador"],[UIImage imageNamed:#"England"],[UIImage imageNamed:#"France"],[UIImage imageNamed:#"Germany"],[UIImage imageNamed:#"Ghana"],[UIImage imageNamed:#"Greece"],[UIImage imageNamed:#"Honduras"],[UIImage imageNamed:#"Iran"],[UIImage imageNamed:#"Italy"],[UIImage imageNamed:#"Japan"],[UIImage imageNamed:#"Mexico"],[UIImage imageNamed:#"Netherlands"],[UIImage imageNamed:#"Nigeria"],[UIImage imageNamed:#"Portugal"],[UIImage imageNamed:#"Russia"],[UIImage imageNamed:#"South Korea"],[UIImage imageNamed:#"Spain"]];
int random = arc4random_uniform(imageArray.count);
return [imageArray objectAtIndex:random];
}
#end
When you call the -[UITableView reloadData] method you are telling the tableview to refresh ints content based on the source you already gave him, but he doesnt change the source. In some cases you refresh you datasource and you need to refresh the UITableView so you call the -[UITableView reloadData]. What you need to do is to first refresh you data and the refresh the view, so the view uses your new data.
Use the the same way you got the data from your modal to refresh it.
self.blogPost.jsonMutable = [NSMutableArray array];
for (NSDictionary *post in self.blogPost.blogData) {
ARModal *bp = [ARModal blogPostWithHome:[[post objectForKey:#"home"] objectForKey:#"text"]];
bp.away = [[post objectForKey:#"away"] objectForKey:#"text"];
bp.result = [[post objectForKey:#"result"] objectForKey:#"text"];
bp.info = [post objectForKey:#"info"];
bp.homeImage = [[post objectForKey:#"homeimage"] objectForKey:#"src"];
[self.blogPost.jsonMutable addObject:bp];
}
Then execute the -[UITableView reloadData], but now he is going to refresh using the refreshed data that you got from your modal.
If you need something else, comment.
-[UITableView reloadData] doesn't actually reload your data. It tells the table that you've reloaded the data, and now you need the table view to reload.
So before you call reloadData you should do:
self.blogPost.jsonMutable = [NSMutableArray array];
for (NSDictionary *post in self.blogPost.blogData) {
ARModal *bp = [ARModal blogPostWithHome:[[post objectForKey:#"home"] objectForKey:#"text"]];
bp.away = [[post objectForKey:#"away"] objectForKey:#"text"];
bp.result = [[post objectForKey:#"result"] objectForKey:#"text"];
bp.info = [post objectForKey:#"info"];
bp.homeImage = [[post objectForKey:#"homeimage"] objectForKey:#"src"];
[self.blogPost.jsonMutable addObject:bp];
}
Also as this chunk of code only uses information provided by ARModal, you should probably put it in a method in ARModal
Problem: I have read in values from Windows Azure. Through NSLogs I am able to see that my application does indeed read in from the table on the Azure Server. However Displaying the values has become a problem.
Situation: So far I have an NSMutableArray object in the ViewController.m file. I have accessed the array and been able to assign the values from the results of the read from the table (in windows azure) to the mutableArray. My problem is that I am trying to display it through a tableview however nothing displays, and when I move down the table view, the application crashes.
I believe the main problem is this line:
cell.textLabel.text = [clubs objectAtIndex:indexPath.row];
Here is the ViewController.m code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController{
NSDictionary *courseDetails;
NSArray *justCourseNames;
NSDictionary *webcourseDetails;
NSArray *webjustCourseNames;
NSDictionary *clubNames;
NSArray *location;
NSMutableArray *clubs;
NSInteger amount;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0)
{
return #"Milton Keynes";
}
else{
return #"Stafford";
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0)
{
return clubs.count;
}
else{
return webcourseDetails.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
UIImage *image = [UIImage imageNamed:#"ClubCellImage"];
[cell.imageView setImage:image];
//removing the line of code below seems to fix the crash. this is the line of code to display the details
cell.textLabel.text = [clubs objectAtIndex:indexPath.row];
/*if (indexPath.section == 0)
{
//cell.textLabel.text = justCourseNames[indexPath.row];
//cell.detailTextLabel.text = courseDetails[justCourseNames[indexPath.row]];
}
else
{
cell.textLabel.text = clubs[indexPath.row];
//cell.textLabel.text = webjustCourseNames[indexPath.row];
//cell.detailTextLabel.text = webcourseDetails[webjustCourseNames[indexPath.row]];
}*/
return cell;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.client = [MSClient clientWithApplicationURLString:#"https://clublocatortimogunmakin.azure-mobile.net/"
applicationKey:#"ecxnaXEfNpeOvwYYgcViJJoumZlZng45"];
// Do any additional setup after loading the view.
NSURL *url = [[NSBundle mainBundle] URLForResource:#"courses" withExtension:#"plist"];
MSTable *itemTable = [_client tableWithName:#"Item"];
courseDetails = [NSDictionary dictionaryWithContentsOfURL:url];
justCourseNames = courseDetails.allKeys;
NSURL *weburl = [[NSBundle mainBundle] URLForResource:#"courses_web" withExtension:#"plist"];
webcourseDetails = [NSDictionary dictionaryWithContentsOfURL:weburl];
webjustCourseNames = courseDetails.allKeys;
[itemTable readWithCompletion:^(NSArray *results, NSInteger totalCount, NSError *error) {
clubs = [results mutableCopy];
amount = totalCount;
if (error) {
NSLog(#"Error: %#", error);
} else {
//NSLog(#"Item read, id: %#", [results objectAtIndex:1]);
for (int i = 0; i < results.count; i++)
{
NSLog(#"Item read, id: %#", [results objectAtIndex:i]);
}
}
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You are contradicting yourself when you implement the required tableview datasource methods (i.e. the numberOfRowsInSection and the cellForRowAtIndexPath methods)
You provide the count of cells here:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0)
{
return clubs.count;
}
else{
return webcourseDetails.count;
}
}
So, your cellForRowAtIndexPath method should look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// cell init/dequeuing
if (indexPath.section == 0) {
cell.textLabel.text = clubs[indexPath.row]; //Assuming that is an NSString instance
} else {
cell.textLabel.text = webcourseDetails[indexPath.row]; //Assuming that is an NSString instance
}
return cell;
}
Please, try to do the following (I think you're not re-loading table after all items are read):
[itemTable readWithCompletion:^(NSArray *results, NSInteger totalCount, NSError *error) {
clubs = [results mutableCopy];
amount = totalCount;
if (error) {
NSLog(#"Error: %#", error);
} else {
//NSLog(#"Item read, id: %#", [results objectAtIndex:1]);
for (int i = 0; i < results.count; i++)
{
NSLog(#"Item read, id: %#", [results objectAtIndex:i]);
}
[YOURTABLENAMEHERE reloadData];
}
}];
Replace YOURTABLENAMEHERE with a reference to your table.
I have a backend made in C#, where I am making WCF calls to from iOS. It works pretty good, but I am stuck in a problem
Code:
#import "ListTableViewController.h"
#import "ListServiceSvc.h"
#import "LoginViewController.h"
#interface ListTableViewController (){
NSMutableArray *productsFromWebServer;
NSMutableDictionary *prodForList;
}
#property (nonatomic, retain) NSMutableArray *shoppingList;
#property (nonatomic, retain) NSMutableArray *productList;
#end
#implementation ListTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
Boolean b = [standardUserDefaults boolForKey:#"HasTokenKey"];
if (!b)
{
[self showLoginViewController];
}
[self loadListsFromRemoteServer];
[self.tableView reloadData];
//self.uname.text = [standardUserDefaults objectForKey:#"username"];
}
- (void) showLoginViewController {
LoginViewController* loginController = (LoginViewController*) [ApplicationDelegate.storyBoard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self presentViewController:loginController animated:YES completion:nil];
}
- (void)userDidLeave
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setBool:NO forKey:#"HasTokenKey"];
// Show the Login screen.
[self showLoginViewController];
}
- (IBAction)exitAction
{
[self userDidLeave];
}
- (void)viewDidLoad
{
_shoppingList = [NSMutableArray array];
_productList = [NSMutableArray array];
//[self loadListsFromRemoteServer];
[super viewDidLoad];
}
-(void) loadListsFromRemoteServer
{
NSString *uname = [[NSUserDefaults standardUserDefaults] objectForKey:#"username"];
NemListBinding *binding = [[ListServiceSvc NemListBinding]initWithAddress:#"http://balder/dm76_gr5/WCF.ListService.svc/custom?singleWsdl"];
binding.logXMLInOut=YES;
ListServiceSvc_GetShoppingListsWithUname *parms = [[ListServiceSvc_GetShoppingListsWithUname alloc]init];
parms.uname = uname;
[binding GetShoppingListsWithUnameAsyncUsingParameters:parms delegate:self];
}
-(void) loadItemsInList:(NSNumber*)slistId
{
NemListBinding *binding = [[ListServiceSvc NemListBinding]initWithAddress:#"http://balder/dm76_gr5/WCF.ListService.svc/custom?singleWsdl"];
binding.logXMLInOut=YES;
ListServiceSvc_GetProductsWithListId *parms = [[ListServiceSvc_GetProductsWithListId alloc]init];
parms.listId = slistId;
[binding GetProductsWithListIdAsyncUsingParameters:parms delegate:self];
}
- (void) operation:(NemListBindingOperation *)operation completedWithResponse:(NemListBindingResponse *)response
{
NSArray *responseHeaders = response.headers;
NSArray *responseBodyParts = response.bodyParts;
[NSThread sleepForTimeInterval:1.0];
NSMutableArray *shoppingListFromWebserver = [[NSMutableArray alloc] init];
productsFromWebServer = [[NSMutableArray alloc]init];
prodForList = [[NSMutableDictionary alloc]init];
// step 1 fill in the blanks.
for(id header in responseHeaders) {
// here do what you want with the headers, if there's anything of value in them
}
for (id mine in responseBodyParts)
{
if ([mine isKindOfClass:[ListServiceSvc_GetShoppingListsWithUnameResponse class]])
{
for (id slist in [[mine GetShoppingListsWithUnameResult] ShoppingList])
{
[shoppingListFromWebserver addObject:slist];
[self loadItemsInList:[slist ShoppingListId]];
//NSLog(#"new list :: RESPONSE FROM SERVER :: nList %#", [slist ShoppingListName]);
}
}
if ([mine isKindOfClass:[ListServiceSvc_GetProductsWithListIdResponse class]])
{
for (id products in [[mine GetProductsWithListIdResult] Product])
{
[prodForList setObject:[products ProductName] forKey:[products ShoppingListId]];
NSLog(#"new product :: RESPONSE FROM SERVER :: nList %#", [products ProductName]);
}
}
}
[self performSelectorOnMainThread:#selector(updateNewView:) withObject:shoppingListFromWebserver waitUntilDone:NO];
}
-(void) updateNewView:(NSMutableArray*) result
{
_shoppingList = result;
NSLog( #"new list - number of news :: %u", [_shoppingList count]);
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; // the number of different sections in your table view
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_shoppingList count]; // the number of data in your shopping list
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
id shopList = [_shoppingList objectAtIndex:indexPath.row];
cell.textLabel.text = [shopList ShoppingListName];
NSNumber *sid = [shopList ShoppingListId];
//[self loadItemsInList:sid];
NSNumber *prodcount = [prodForList objectForKey:sid];
NSString* pcTostring = [NSString stringWithFormat:#"%#", prodcount];
cell.detailTextLabel.text = pcTostring;
return cell;
The log actually tells me that I am getting some products for the last List it gets. Problem is that all cell.detailText.text fields are null, until the last reload, then all cells disappear.
I am pretty sure I'm doing it wrong, but I cant get my head around hot to get the products for the selected list, when I need to get the ShoppingListId from the -(void) loadListsFromRemoteServer call in order to do the -(void) loadItemsInList:(NSNumber*)slistId
WCF connection was made with the help of wsdl2obj