I'm trying to implement the searchBar following steps in this Video
But I got the following exception when I press the searchBar
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object <Authors: 0x10960c1e0>.'
AuthorsTableViewController.h
#interface AuthorsTableViewController : UITableViewController <UISearchBarDelegate> {
NSMutableArray *authorsArray;
NSMutableArray *resultArray;
QuotesNavController *quotes;
}
#property (nonatomic, strong) IBOutlet UISearchBar *SearchBar;
#property (nonatomic, retain) NSMutableArray *authorsArray;
#property (nonatomic, retain) NSMutableArray *resultArray;
-(sqlite3 *) openDataBase;
-(IBAction)backToQuotes:(id)sender;
#end
AuthorsTableViewController.m
#import "AuthorsTableViewController.h"
#interface AuthorsTableViewController ()
#end
#implementation AuthorsTableViewController
#synthesize authorsArray, resultArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self retrieveDataFromSqlite];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(IBAction)backToQuotes:(id)sender{
[self performSegueWithIdentifier:#"backToQuotes" sender:sender];
}
-(sqlite3 *) openDataBase{
sqlite3 *database;
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:#"SuccessQuotes" ofType:#"sqlite"];
if (sqlite3_open([sqLiteDb UTF8String], &database) != SQLITE_OK) {
NSLog(#"Failed to open database!");
}else{
NSLog(#"database opened!");
}
return database;
}
-(void) retrieveDataFromSqlite{
authorsArray = [[NSMutableArray alloc] init];
sqlite3 *myDatabase = [self openDataBase];
const char *sqlSelect = "SELECT *, COUNT(qu_author) AS count FROM authors JOIN quotes ON _auid = qu_author GROUP BY au_name ORDER BY au_name ASC";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(myDatabase, sqlSelect, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int author_id = sqlite3_column_int(compiledStatement, 0);
NSString *au_name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 1)];
NSString *au_picture = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 2)];
int quotes_id = sqlite3_column_int(compiledStatement, 3);
NSString *quotes_content = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 4)];
int quotes_author = sqlite3_column_int(compiledStatement, 5);
NSString *quotes_favorite = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 6)];
int count = sqlite3_column_int(compiledStatement, 7);
Authors *authors = [[Authors alloc] initWithUniqueId:author_id au_name:au_name au_picture:au_picture quotes_id:quotes_id quotes_content:quotes_content quotes_author:quotes_author quotes_author:quotes_author quotes_favorite:quotes_favorite count:count];
[authorsArray addObject:authors];
} // while end
}// prepare end
sqlite3_finalize(compiledStatement);
sqlite3_close(myDatabase);
}
- (void) searchThroughtData {
self.resultArray = nil;
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF like[c] %#",[NSString stringWithFormat:#"%#*",self.SearchBar.text]];
self.resultArray = [[self.authorsArray filteredArrayUsingPredicate:resultPredicate] mutableCopy];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return [authorsArray count];
} else {
[self searchThroughtData];
return [resultArray count];
}
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self searchThroughtData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"authorCell";
AuthorsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[AuthorsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
Authors *temp_items;
if (tableView == self.tableView) {
temp_items = [self.authorsArray objectAtIndex:indexPath.row];
}else{
temp_items = [self.resultArray objectAtIndex:indexPath.row];
}
[cell.authorName setText:[NSString stringWithFormat:#"%#",temp_items.au_name]];
[cell.count setTitle:[NSString stringWithFormat:#"%d",temp_items.count] forState:UIControlStateNormal];
UIImage *theImage = [UIImage imageNamed:temp_items.au_picture];
cell.authorPic.image = theImage;
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithRed:93/255.0
green:45/255.0
blue:22/255.0
alpha:0.5];
cell.selectedBackgroundView = customColorView;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Authors *temp_items = [self.authorsArray objectAtIndex:indexPath.row];
quotes = [self.storyboard instantiateViewControllerWithIdentifier:#"QuotesNavController"];
quotes.quType = 3;
quotes.authorId = temp_items.author_id;
[self presentModalViewController:quotes animated:YES];
}
#end
Related
I want to display images in a tableView from SQLite. Here is my retrieve image code from SQLite code. How do I load the images in the tableview?
-(void) retrieveData{
sqlite3 * database;
NSMutableArray *prodnamemutable = [[NSMutableArray alloc] init];
NSMutableArray *prodpricemutable = [[NSMutableArray alloc] init];
NSMutableArray *prodimagemutable = [[NSMutableArray alloc] init];
NSString *databasename=#"contacts.sqlite"; // Your database Name.
NSArray * documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);
NSString * DocDir=[documentpath objectAtIndex:0];
NSString * databasepath=[DocDir stringByAppendingPathComponent:databasename];
if(sqlite3_open([databasepath UTF8String], &database) == SQLITE_OK)
{
const char *sqlStatement = "SELECT * FROM contacts"; // Your Tablename
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
[prodnamemutable removeAllObjects];
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
[prodnamemutable addObject:[NSString stringWithFormat:#"%s",(char *) sqlite3_column_text(compiledStatement, 1)]];
[prodpricemutable addObject:[NSString stringWithFormat:#"%s",(char *) sqlite3_column_text(compiledStatement, 3)]];
int length = sqlite3_column_bytes(compiledStatement, 0);
NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(compiledStatement, 0) length:length];
[prodimagemutable addObject:imageData];
NSLog(#"Length from db : %lu", (unsigned long)[imageData length]);
imageArray = [NSArray arrayWithArray:prodimagemutable];
NSLog(#"image found.%#",imageArray);
}
}
nameArray = [[NSArray alloc]initWithArray:prodnamemutable];
priceArray = [[NSArray alloc]initWithArray:prodpricemutable];
NSLog(#"nameArray:%#",nameArray);
[[self navigationController] tabBarItem].badgeValue = [NSString stringWithFormat:#"%lu",(unsigned long)[nameArray count]];
sqlite3_finalize(compiledStatement);
[self.tableView reloadData];
}
sqlite3_close(database);
}
UIImage *image = [UIImage imageWithData:data];
This will convert your image NSData to UIImage now this image can be shown on any UIImageView.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; //count of section
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [prodimagemutable count]; //count number of row from image array.
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
cell.imageView.image = [UIImage imageWithData:[prodimagemutable objectAtIndex: indexpath.row]] ;
return cell;
}
//Below use for set height of cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;// or the height you want.
}
it will be better if you separated the code into multiple classes like the following
1. DBManager
#interface DBManager : NSObject
- (NSArray *)getImages;
#end
#implementation DBManager
- (NSArray *)getImages {
// put your code to retrieve the images from the database
// use the following to convert from NSData to UIImage
UIImage *image = [UIImage imageWithData:imageData];
}
#end
2. ImagesTableViewController
#interface ImagesTableViewController : UITableViewController
#property(nonatomic,strong) NSArray* images;
#end
#implementation ImagesTableViewController
- (void)viewDidLoad {
self.images = [dbManager getImages];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.images.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
}
cell.imageView.image = self.images[indexpath.row];
return cell;
}
#end
I am trying to display data in different sections using PFQueryTableViewController. The code suggested in https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections seemed to work, but I couldn't get the data to load on self.objects. Here is my code:
#import "AKAdminViewStudentsViewController.h"
#interface AKAdminViewStudentsViewController ()
#property (strong, nonatomic) NSMutableDictionary *sections;
#property (strong, nonatomic) NSMutableDictionary *sectionToClassMap;
#end
#implementation AKAdminViewStudentsViewController
#synthesize sections = _sections;
#synthesize sectionToClassMap = _sectionToClassMap;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.parseClassName = #"Students";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.tableView reloadData];
NSLog(#"sections found: %#", self.sections); // returns null
NSLog(#"sectionToClassMap: %#", self.sectionToClassMap); // returns null
NSLog(#"objects in table: %#", self.objects); // this found to be empty
}
...
#pragma mark - Tableview methods
- (PFQuery *)queryForTable
{
PFQuery *queryStudents = [PFQuery queryWithClassName:#"Students"];
[queryStudents whereKey:#"student_admin" equalTo:[PFUser currentUser]];
if ([self.objects count]==0) {
queryStudents.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
[queryStudents orderByAscending:#"class_name"];
return queryStudents;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.sections.allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *className = [self classForSection:section];
NSArray *rowIndicesInSection = [self.sections objectForKey:className];
return rowIndicesInSection.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *className = [self classForSection:section];
return className;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *CellIdentifier = #"Students";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure cell
cell.textLabel.text = [NSString stringWithFormat:#"%#", object[#"full_name"]];
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateStyle:NSDateFormatterShortStyle];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [formatDate stringFromDate:object[#"createdAt"]]];
return cell;
}
#pragma mark - Helper methods
- (void)objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];
// Clear data before loading
[self.sections removeAllObjects];
[self.sectionToClassMap removeAllObjects];
NSInteger section = 0;
NSInteger rowIndex = 0;
for (PFObject *object in self.objects) {
NSString *className = [object objectForKey:#"class_name"];
NSMutableArray *objectsInSection = [self.sections objectForKey:className];
if (!objectsInSection) {
objectsInSection = [NSMutableArray array];
// Create new section for new class found
[self.sectionToClassMap setObject:className forKey:[NSNumber numberWithInt:section++]];
}
[objectsInSection addObject:[NSNumber numberWithInt:rowIndex++]];
[self.sections setObject:objectsInSection forKey:className];
}
}
- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath
{
NSString *className = [self classForSection:indexPath.section];
NSArray *rowIndicesInSection = [self.sections objectForKey:className];
NSNumber *rowIndex = [rowIndicesInSection objectAtIndex:indexPath.row];
return [self.objects objectAtIndex:[rowIndex intValue]];
}
- (NSString *)classForSection:(NSInteger)section
{
return [self.sectionToClassMap objectForKey:[NSNumber numberWithInt:section]];
}
#end
When I look at the NSLog data, self.objects is empty. I know this is the reason why data isn't displayed, and would like some advice to sort this out please! Thanks.
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 UITableview that I have populated with data from a database, I know what to send data to another view (the detailViewController), however I can't get it to work, right now I am sending data via the - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { method. I have two classes the UITableview class and the detail class they are below. I also have an NSObject class that holds the objects for the database. Please check it out, here's my code:
UITableView Class code.:
Header File
#import <UIKit/UIKit.h>
#import <sqlite3.h>
#interface ExerciseViewController : UITableViewController {
NSMutableArray *theauthors;
sqlite3 * db;
}
#property(nonatomic,retain) NSMutableArray *theauthors;
-(NSMutableArray *) authorList;
#end
Implementation File
#import "ExerciseViewController.h"
#import "sqlColumns.h"
#import <sqlite3.h>
#import "UIColor+FlatUI.h"
#import "ExerciseDetailViewController.h"
#interface ExerciseViewController ()
#end
#implementation ExerciseViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Abdominal";
[self authorList];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.theauthors count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"exerciseCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
sqlColumns *author = [self.theauthors objectAtIndex:indexPath.row];
UILabel *exerciseName = (UILabel *)[cell viewWithTag:101];
exerciseName.text = author.Name;
UILabel *equipment = (UILabel *)[cell viewWithTag:102];
equipment.text = author.Equipment;
NSString *string = author.Equipment;
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
if ([trimmedString isEqualToString:#"null"]) {
equipment.text = #"No Equipment";
}
UILabel *difficulty = (UILabel *)[cell viewWithTag:103];
difficulty.text = author.Difficulty;
if ([difficulty.text isEqualToString:#"Easy"]) {
difficulty.textColor = [UIColor emerlandColor];
}
if ([difficulty.text isEqualToString:#"Intermediate"]) {
difficulty.textColor = [UIColor belizeHoleColor];
}
if ([difficulty.text isEqualToString:#"Hard"]) {
difficulty.textColor = [UIColor alizarinColor];
}
if ([difficulty.text isEqualToString:#"Very Hard"]) {
difficulty.textColor = [UIColor alizarinColor];
}
UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:100];
cellImageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",author.File]];
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor cloudsColor];
bgColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bgColorView];
return cell;
}
-(NSMutableArray *) authorList{
_theauthors = [[NSMutableArray alloc] initWithCapacity:10];
#try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:#"StayhealthyExercises.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(#"Cannot locate database file '%#'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(#"An error has occured: %s", sqlite3_errmsg(db));
}
const char *query = "SELECT * FROM strengthexercises WHERE primarymuscle LIKE '%abdominal%'";
const char *sql = query;
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(#"Problem with prepare statement: %s", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
sqlColumns * author = [[sqlColumns alloc] init];
author.Name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
author.Muscle = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
author.Description = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
author.File= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 4)];
author.Sets= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 5)];
author.Reps= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 6)];
author.Equipment= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 7)];
author.PrimaryMuscle= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 8)];
author.SecondaryMuscle= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 9)];
author.Difficulty= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 10)];
[_theauthors addObject:author];
}
}
sqlite3_finalize(sqlStatement);
}
#catch (NSException *exception) {
NSLog(#"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
#finally {
sqlite3_close(db);
return _theauthors;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
sqlColumns *author = [self.theauthors objectAtIndex:indexPath.row];
ExerciseDetailViewController *destViewController = segue.destinationViewController;
destViewController.exerciseImage.image = [UIImage imageNamed:author.File];
destViewController.descriptionLabel.text = author.Description;
}
}
#end
Now the DetailViewController
Header file:
#import <UIKit/UIKit.h>
#import "sqlColumns.h"
#interface ExerciseDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *exerciseImage;
#property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
#property (nonatomic, strong) sqlColumns *author;
#end
Implementation File
#import "ExerciseDetailViewController.h"
#import "ExerciseViewController.h"
#interface ExerciseDetailViewController ()
#end
#implementation ExerciseDetailViewController
#synthesize exerciseImage,descriptionLabel,author;
- (void)viewDidLoad
{
[super viewDidLoad];
descriptionLabel.text = author.Description;
NSLog(#"%#",author.Description);
}
#end
Must be a small error, any help would be greatly appreciated!
I think the problem is that your outlets are nil. You can only set the text and the image after the viewDidLoad get called.
Try to save your info in other properties and after viewDidLoad get called assign this info to your label and image view.
In your prepare for segue:
destViewController.image = [UIImage imageNamed:author.File];
destViewController.text = author.Description;
In your header add those properties:
#property (strong, nonatomic) UIImage *image;
#property (strong, nonatomic) NSString *text;
Then in your viewDidLoad:
exerciseImage.image = self.image;
descriptionLabel.text = self.text;
I have a problem with a searchBar, i get an error unrecognized selector.
2013-02-11 14:48:27.211 Scores (FREE)[13946:c07] test ijsid
CONTAINS[cd] "A" 2013-02-11 14:48:27.213 Scores (FREE)[13946:c07] test
(
Axel,
"Double Axel",
"Triple Axel",
"Quad Axel" ) 2013-02-11 14:48:27.213 Scores (FREE)[13946:c07] -[SingleElements isEqualToString:]: unrecognized selector sent to instance 0x755a5f0 2013-02-11 14:48:27.214 Scores (FREE)[13946:c07]
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SingleElements
isEqualToString:]: unrecognized selector sent to instance 0x755a5f0'
* First throw call stack: (0x209f012 0x11ace7e 0x212a4bd 0x208ebbc 0x208e94e 0x249e9f 0x24a064 0x2ae4 0x1ab8fb 0x1ab9cf 0x1941bb 0x192872
0x19d701 0x1a5d5d 0x1ade04 0x3d55f5 0x3d809e 0x327cc5 0x11c0705
0xf4213 0x1b5c7e 0x1b5310 0x1c213c 0x1cc5a6 0xc6d4f9 0x20f90c5
0x2053efa 0xba1bb2 0x38a39de 0x29f11da 0x2e2adfc 0x2e2dbf8 0x387e612
0x387e74a 0x387eec0 0x387ecb8 0x387e204 0x2c6c22b 0x2c6c193 0x3850e96
0x387d4cc 0x2e28136 0x2e273c6 0x2e5a980 0x34b67fd 0x2e51576 0x2e526da
0x2e5072e 0x34b4eaa 0x2e6aaf1 0x2e5a72a 0x2e2e6ae 0x2a2d62b 0x11c06b0
0x3899810 0x2a6c1a4 0x2a6e2ff 0x2b20b4 0x274aef 0x275e58 0x2749fe
0x27ed29 0x101ddb 0x1ff7f5 0x1ff7f5 0x1ff7f5 0x1ff7f5 0x1ff7f5
0x1ff7f5 0x1ff7f5 0x1ff7f5 0x1ff7f5 0x101e35 0x101806 0x101beb 0xf3698
0x1ffadf9 0x1ffaad0 0x2014bf5 0x2014962 0x2045bb6 0x2044f44 0x2044e1b
0x1ff97e3 0x1ff9668 0xf0ffc 0x1f8d 0x1eb5) libc++abi.dylib: terminate
called throwing an exception
(lldb) po 0x755a5f0 $0 = 123053552 Axel
.h
#interface SingleElementsVC : UITableViewController{
NSMutableArray *thesingleelementslist;
sqlite3 * db;
UISearchDisplayController *searchDisplayController;
UISearchDisplayController *searchBar;
NSMutableArray *searchResults;
}
#property(nonatomic,retain) NSMutableArray *thesingleelementslist;
#property(nonatomic, strong)SingleElementsDetailView *details;
-(NSMutableArray *) singleElementsList;
#property (nonatomic, retain) IBOutlet UISearchDisplayController *searchDisplayController;
#property (nonatomic, retain) IBOutlet UISearchDisplayController *searchBar;
#property (nonatomic, copy) NSMutableArray *searchResults;
#end
.m
#interface SingleElementsVC ()
#end
#implementation SingleElementsVC
#synthesize thesingleelementslist;
#synthesize searchDisplayController;
#synthesize searchBar;
#synthesize searchResults;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self singleElementsList];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (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];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *sectionHeader = nil;
sectionHeader = #"Scale Of Values For Single";
return sectionHeader;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSInteger rows = 0;
if ([tableView
isEqual:self.searchDisplayController.searchResultsTableView]){
rows = [self.searchResults count];
}
else{
rows = [self.thesingleelementslist count];
}
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SingleElementsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
}
else{
SingleElements *singleElements = [self.thesingleelementslist objectAtIndex:indexPath.row];
cell.textLabel.text = singleElements.ijsid;
NSString *singleElementsDescriptionAndBase = [NSString stringWithFormat:#"%# (%#)",singleElements.description, singleElements.base];
cell.detailTextLabel.text = singleElementsDescriptionAndBase;
}
return cell;
}
-(NSMutableArray *) singleElementsList{
thesingleelementslist = [[NSMutableArray alloc] initWithCapacity:10];
#try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:#"ElementsDb.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(#"Cannot locate database file '%#'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(#"An error has occured: %s", sqlite3_errmsg(db));
}
const char *sql = "SELECT * FROM single";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(#"Problem with prepare statement: %s", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
SingleElements * singleElements = [[SingleElements alloc] init];
singleElements.group = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
singleElements.ijsid = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
singleElements.description = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,3)];
singleElements.plus3 = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(sqlStatement,4)];
singleElements.plus2 = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(sqlStatement,5)];
singleElements.plus1 = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(sqlStatement,6)];
singleElements.base = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(sqlStatement,7)];
singleElements.baseur = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(sqlStatement,8)];
singleElements.minus1 = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(sqlStatement,9)];
singleElements.minus2 = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(sqlStatement,10)];
singleElements.minus3 = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(sqlStatement,11)];
singleElements.goeplus3 = [singleElements.plus3 decimalNumberByAdding:singleElements.base];
singleElements.goeplus2 = [singleElements.plus2 decimalNumberByAdding:singleElements.base];
singleElements.goeplus1 = [singleElements.plus1 decimalNumberByAdding:singleElements.base];
singleElements.goeminus1 = [singleElements.minus1 decimalNumberByAdding:singleElements.base];
singleElements.goeminus2 = [singleElements.minus2 decimalNumberByAdding:singleElements.base];
singleElements.goeminus3 = [singleElements.minus3 decimalNumberByAdding:singleElements.base];
[thesingleelementslist addObject:singleElements];
}
}
}
#catch (NSException *exception) {
NSLog(#"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
#finally {
sqlite3_close(db);
return thesingleelementslist;
}
}
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"ijsid contains[cd] %#",
searchText];
self.searchResults = [NSMutableArray arrayWithArray:[self.thesingleelementslist filteredArrayUsingPredicate:resultPredicate]];
NSLog(#"test %#", searchResults);
}
#pragma mark - UISearchDisplayController delegate methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//if ([[segue identifier] isEqualToString:#"showDetail"]) {
SingleElementsDetailView *detailViewController = [segue destinationViewController];
SingleElements *singleElements = [[SingleElements alloc]init];
detailViewController.singleElements = [self.singleElementsList objectAtIndex:[self.tableView indexPathForSelectedRow].row];
singleElements = [self.singleElementsList objectAtIndex:[self.tableView indexPathForSelectedRow].row];
NSLog(#"test %#", singleElements.ijsid);
NSLog(#"test %#", singleElements.description);
NSLog(#"test %#", singleElements.goeplus3);
NSLog(#"test %#", singleElements.goeplus2);
NSLog(#"test %#", singleElements.goeplus1);
NSLog(#"test %#", singleElements.base);
NSLog(#"test %#", singleElements.goeminus1);
NSLog(#"test %#", singleElements.goeminus2);
NSLog(#"test %#", singleElements.goeminus3);
//}
}
#end
Class SingleElements.h
#interface SingleElements : NSObject{
NSString *group;
NSString *ijsid;
NSString *description;
NSDecimalNumber *plus3;
NSDecimalNumber *plus2;
NSDecimalNumber *plus1;
NSDecimalNumber *base;
NSDecimalNumber *baseur;
NSDecimalNumber *minus1;
NSDecimalNumber *minus2;
NSDecimalNumber *minus3;
NSDecimalNumber *goeplus3;
NSDecimalNumber *goeplus2;
NSDecimalNumber *goeplus1;
NSDecimalNumber *goeminus1;
NSDecimalNumber *goeminus2;
NSDecimalNumber *goeminus3;
}
#property(nonatomic,copy) NSString *group;
#property(nonatomic,copy) NSString *ijsid;
#property(nonatomic,copy) NSString *description;
#property(nonatomic,copy) NSDecimalNumber *plus3;
#property(nonatomic,copy) NSDecimalNumber *plus2;
#property(nonatomic,copy) NSDecimalNumber *plus1;
#property(nonatomic,copy) NSDecimalNumber *base;
#property(nonatomic,copy) NSDecimalNumber *baseur;
#property(nonatomic,copy) NSDecimalNumber *minus1;
#property(nonatomic,copy) NSDecimalNumber *minus2;
#property(nonatomic,copy) NSDecimalNumber *minus3;
#property(nonatomic,copy) NSDecimalNumber *goeplus3;
#property(nonatomic,copy) NSDecimalNumber *goeplus2;
#property(nonatomic,copy) NSDecimalNumber *goeplus1;
#property(nonatomic,copy) NSDecimalNumber *goeminus1;
#property(nonatomic,copy) NSDecimalNumber *goeminus2;
#property(nonatomic,copy) NSDecimalNumber *goeminus3;
#end
Any help are welcome, thanks.
Is SingleElements subclass of NSString ??
My guess would be isEqualToString is not available for SingleElements.
Can you post code for SingleElements class?
EDIT:
From your updated question I can see that it's a subclass of NSObject which hasn't any idea with what isEqualToString has to do with.
For getting to the line of crash (which you will not have any clue, If I guessed it right), Try to do as following:
Go to the breakpoints navigator in the left pane.
Click the plus button at the bottom of the screen.
Choose Add Exception Breakpoint
In the bubble that pops up choose Objective-C in the Exception field.
Execute the program again
Now, Try to have that exception again. XCode will bring you to that line before crashing.
Then, Post that line here. May be then, We can go for the solution.