UITableViewController not showing data [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UITableViewController not loading data
I have created a UITableViewController in the following way
#import "KLActionsViewController.h"
#interface KLActionsViewController ()
#end
#implementation KLActionsViewController
#synthesize actionList,delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
}
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray* list = [[NSMutableArray alloc] init];
self.tableView = [[UITableView alloc] init];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.actionList = list;
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 400.0);
[self.actionList addObject:#"Obj1"];
[self.actionList addObject:#"Obj2"];
[self.actionList addObject:#"Obj3"];
[self.actionList addObject:#"Obj4"];
}
- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"in number of section");
return 1;
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString* lab = [self.actionList objectAtIndex:indexPath.row];
// NSLog(lab);
NSLog(#"here in there");
cell.textLabel.text = lab;
return cell;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Total entries : %i ",[self.actionList count]);
return [self.actionList count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.delegate != nil) {
[self.delegate actionSelected:indexPath.row];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.actionList = nil;
self.delegate = nil ;
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
But when I try to create it's instance and show it in a UIPopoverController all I can see is an empty table and no data in it. Also, numberOfRowsInSection and cellForRowAtIndexPath never get called.
Any help ?

You are creating a new table view inside viewDidLoad: but not making it a subview of this controller's view. Since you say you see an empty table, this leads me to believe that you're loading one table from your .xib file, but setting the dataSource only of this new one.
If that's how it's structured, use a connection in the .xib from the table view to the self.tableView property and don't create a new object...just configure the one you have.

Related

cellForRowAtIndexPath not called when compiled with xcode 6.1

In my app I have a popup with a [table view][1] .
When compiled with Xcode 5.1 everything works fine, but the same code compiled with Xcode 6.1 failed to call the [cellForRowAtIndexPath][3] [delegate][4] method.
The other delegate meths are called.
One intersting point is self.tableView.rowHeight; returns -1
I have tried explicitly setting the delegate and data source to self but that makes not difference
The class is called by the following code;
`-(IBAction)selectLanguage:(id)sender
{
ATLMLanguagePopoverTableViewController *pvc = [[ATLMLanguagePopoverTableViewController alloc] initWithNibName:nil bundle:nil];
pvc.target = self;
pvc.action = #selector(popoverDidSelectItem:);
pvc.items = [[[ATLMLibraryManager getManager]libraryDefaults]getAvailableLanguageNames];
_myPopoverController.contentViewController = pvc;
[_myPopoverController setPopoverContentSize:[pvc popoverContentSize]];
[_myPopoverController presentPopoverFromBarButtonItem:(UIBarButtonItem *)sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
`
Hear is the definition of the class
/
/ LanguagePopoverTableViewController.m
// SalesAid
//
// Created by phuang on 1/16/13.
// Copyright (c) 2013 Align Technology. All rights reserved.
//
#import "ATLMLanguagePopoverTableViewController.h"
#import "ATLMLocalizationManager.h"
#import "ATLMUtils.h"
#interface ATLMLanguagePopoverTableViewController ()
#end
#implementation ATLMLanguagePopoverTableViewController
#synthesize items, selectedItem, target, action;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
selectedItem = -1;
target = nil;
action = NULL;
}
return self;
}
-(void) resetLocalization {
[self.tableView reloadData];
}
- (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.
}
- (void)setItems:(NSArray *)newItems {
items = [newItems copy];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
selectedItem=0;
NSString *curLang = (NSString *) [[ATLMLocalizationManager getManager]getCurrentLanguage] ;
for(int i = 0; i < items.count ; i++ ){
if([curLang isEqualToString:(NSString *)[items objectAtIndex:i]]){
selectedItem = i;
break;
}
}
NSIndexPath *i = [NSIndexPath indexPathForRow:selectedItem inSection:0];
[self.tableView selectRowAtIndexPath:i animated:NO scrollPosition:UITableViewScrollPositionNone];
}
- (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;
}
- (CGSize)popoverContentSize {
NSInteger rowHeight = self.tableView.rowHeight;
UITableView *tv = self.tableView;
rowHeight = 50;
return CGSizeMake(100, [items count] * rowHeight);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:14];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [ATLMUtils getAlignBlue];
cell.selectedBackgroundView = myBackView;
[cell setSelectedBackgroundView: myBackView ];
NSString *textLabelKey = [items objectAtIndex:[indexPath indexAtPosition:1]];
cell.textLabel.text = ATLMLocalizedString(textLabelKey, nil);
cell.textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedItem = indexPath.row;
if (target != nil && action != NULL) {
[target performSelector:action withObject:self];
}
}
#end
`
OK to answer my own question;
Basically the code adds the model to the view controller after the init method is called. However is seem the thread model has changed a bit and the view is created before the model is added so the row count in the model is zero
The solution is to pass the model as part of the init method.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil items:(NSArray *)itemArray
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
items = [itemArray copy];
selectedItem = -1;
target = nil;
action = nil;
}
return self;
}

iPhone Array with Dictionary with Array (ListApp)

I'm working on a list app. I have two arrays (Animals and Cars). The code looks like:
...
Edit:
This is the new code of the whole .m with your suggestions:
Now it works that everything is in one list. Now how to group them in the list?
#import "ListeViewController.h"
#interface ListeViewController ()
#end
#implementation ListeViewController{
NSArray *kategorien;
NSArray *autosArray;
NSArray *tiereArray;
NSArray *kategorienArray;
NSArray *combined;
NSDictionary *allgemein;
}
-(instancetype) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self){
self->kategorien = #[#"Autos", #"Tiere"];
self->autosArray = #[#"Opel", #"Mercedes", #"Audi"];
self->tiereArray = #[#"Hamster", #"Ente", #"Schwan", #"Pferd", #"Pinguin"];
self->combined = [autosArray arrayByAddingObjectsFromArray:tiereArray];
self->allgemein = [NSDictionary dictionaryWithObjects: combined forKeys:combined];
}
return self;
}
-(instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self->kategorien = #[#"Autos", #"Tiere"];
self->autosArray = #[#"Opel", #"Mercedes", #"Audi"];
self->tiereArray = #[#"Hamster", #"Ente", #"Schwan", #"Pferd", #"Pinguin"];
self->combined = [autosArray arrayByAddingObjectsFromArray:tiereArray];
self->allgemein = [NSDictionary dictionaryWithObjects: combined forKeys:combined];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark -
#pragma mark Table view data source
-(NSInteger) numberOfSectionsInTableView:(UITableView*) tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{
return [combined 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
//Configure the Cell
cell.textLabel.text = [combined objectAtIndex:indexPath.row];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"In did Select Row At Index Path");
NSLog(#"Row %d" ,indexPath.row);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
(sry or the german tagging of the arrays and the dictionary, also sry for some spelling and grammar)
You can use NSDictionary's dictionaryWithObjects:forKeys to create an NSDictionary based on two arrays.
The way you would do it would be:
self.allgemein = [NSDictionary dictionaryWithObjects:autosArray forKeys:tiereArray];
If you want to combine both arrays and then store them in a Dictionary with the same keys and objects (although why you would do this I'm not sure) then do:
NSArray *combined = [self.autosArray arrayByAddingObjectsFromArray:self.tiereArray];
self.allgemein = [NSDictionary dictionaryWithObjects:combined forKeys:combined];

UITableView as subview is always empty

I have a ViewController which has several subviews (Graphs) added using function
[self.view addSubview:subView]
Now, I want to add a UITableView as subview, but when I do this, my table is always empty. Methods like numberOfSectionsInTableView, numberOfRowsInSection, etc are never called.
My table doesn't have a XIB file. This is the code from my .M file:
#import "TableSubviewViewController.h"
#implementation TableSubviewViewController
#synthesize data;
#synthesize tableFrame;
-(id)initWithStyle:(UITableViewStyle)style{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void)viewDidLoad{
[super viewDidLoad];
self.view.frame = tableFrame;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [[self.data allKeys]count];
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [[self.data allKeys]objectAtIndex:section];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Some text";
return cell;
}
And this is the code in my main ViewController, in which I'm creating TableSubviewViewController:
TableSubviewViewController *tableSubView = [[TableSubviewViewController alloc]initWithStyle:UITableViewStyleGrouped];
tableSubView.tableFrame = tableFrame;
tableSubView.data = data;
[self.view addObject:tableSubView.view];
What Am I missing?
Thank you
TableSubviewViewController tableSubView must be main ViewController's global variable , can't be a local variable in the method viewDidLoad of main ViewController
U need to set datasource and delegate methods do like this
// in TableSubviewViewController.h file put a custom initilizer
#interface TableSubviewViewController : UIViewController
- (id)initTheSubviewTableVIewWithFrame:(CGRect)rect; //your custom initialiser
#end
//TableSubviewViewController.m file
#import "TableSubviewViewController.h"
#interface TableSubviewViewController ()<UITableViewDataSource,UITableViewDelegate>
#end
#implementation TableSubviewViewController
//dont use this method left blank
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialisation
}
return self;
}
//you can make custom initiliser method
- (id)initTheSubviewTableVIewWithFrame:(CGRect)rect
{
self = [super init];
if(self)
{
UITableView *subViewTable = [[UITableView alloc]initWithFrame:rect];//since u are creating the tableview programatically u need to set datasource and delegate programatically
subViewTable.dataSource = self; //add this
subViewTable.delegate = self; //this also
[self.view addSubview:subViewTable];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//datasource and delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
//use as reqired
//-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section{
// return 3;
//}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Some text";
return cell;
}
//in main ViewController.m controler u need to do like this
TableSubviewViewController *subViewTable = [[TableSubviewViewController alloc]initTheSubviewTableVIewWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, 150)];//created the subviewTableView tableview
//set properties that u defined for tableview delegate and datasource
[self.view addSubview:subViewTable.view];
hope this helps u :)
Make sure your .h file use
and set delegate and datasource to self.
TableSubviewViewController *tableSubView = [[TableSubviewViewController alloc]initWithStyle:UITableViewStyleGrouped];
tableSubView.tableFrame = tableFrame;
tableSubView.delegate = self;
tableSubView.dataSource = self;
tableSubView.data = data;
[self.view addObject:tableSubView.view];
After that check whether array self.data have object or not. It might be possible that array is blank.

IndexPath.row on a button?

I have an app in which you can have some details of something and then inside that thing you have a sub-category of things. I have made a button as there was not enough room for a navigation item and I can't seem to be able to call only the items that are assigned to that in my nsmutablearray. I have tried to used initWithIndexPath:indexPath.row though it comes up with this error:
Use of undeclared identifyer "indexPath"; did you mean "NSIndexPath"
This is the code for my tableView inside my tableView:
#import "PRViewController.h"
#import "Patient.h"
#import "LSAppDelegate.h"
#import "LSViewController.h"
#import "LSAppDelegate.h"
#import "Patient.h"
#import "PatientController.h"
#import "AddPatientController.h"
#import "treatmentController.h"
#interface PRViewController ()
#end
#implementation PRViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title = #"Treatments";
LSAppDelegate *delegate = (LSAppDelegate *)[[UIApplication sharedApplication] delegate];
patients = delegate.patients;
[[UIToolbar appearance] setTintColor:[UIColor brownColor]];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)add:(id) sender{
[self.tableView reloadData];
[self.tableView setEditing:YES animated:YES];
if(self.tableView) {
NSMutableArray *indices = [[NSMutableArray alloc] init];
for (int i=0; i < patients.count; i++) {
[indices addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
NSArray *lastIndex = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:patients.count inSection:0]];
if (self.tableView) {
for (int i=0; i < patients.count; i++) {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[indices objectAtIndex:i]];
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
}
}
}
[self.tableView setEditing:NO animated:YES];
treatmentController *AddPatient = [[treatmentController alloc] init];
[self.navigationController pushViewController:AddPatient animated:YES];
[super setEditing:NO animated:NO];
}
-(void)setEditing:(BOOL)editing animated:(BOOL) animated {
if ( editing != self.editing ) {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
}
#pragma mark UITableViewDataSource Methods
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"cell"];
if ( nil == cell ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
NSLog(#"indexPath.row = %d, patients.count = %d", indexPath.row, patients.count);
Patient *thisPatient = [patients objectAtIndex:indexPath.row];
if (thisPatient.treatmentName.length > 0) {
cell.textLabel.text = thisPatient.treatmentName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor blackColor];
} else {
}
if (self.editing) {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [patients count];
}
#pragma mark UITableViewDelegate Methods
- (void) tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if ( editingStyle == UITableViewCellEditingStyleDelete ) {
[patients removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
LSAppDelegate *delegate = (LSAppDelegate *)[[UIApplication sharedApplication] delegate];
PatientController *patient = [[PatientController alloc] initWithIndexPath:indexPath];
[delegate.navController pushViewController:patient animated:YES];
[tv deselectRowAtIndexPath:indexPath animated:YES];
}
#end
Please say if you want any more code or information and please answer as soon as you can
file for the button I am pushing with:
#import "PatientController.h"
#import "LSAppDelegate.h"
#import "Patient.h"
#import "PRViewController.h"
#interface PatientController ()
#end
#implementation PatientController
- (id)initWithIndexPath:(NSIndexPath *)indexPath {
if ( ( self = [super init]) ) {
index = indexPath;
}
return self;
}
- (IBAction)PatientRecords:(id)sender {
PRViewController *AddPatient = [[PRViewController alloc] initWithIndexPath:indexPath.row];
[self.navigationController pushViewController:AddPatient animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
LSAppDelegate *delegate = (LSAppDelegate *)[[UIApplication sharedApplication] delegate];
Patient *thisPatient = [delegate.patients objectAtIndex:index.row];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title = thisPatient.patientName;
patientNameView.text = thisPatient.patientName;
patientFirstNameView.text = #"Firstname:";
patientSurnameView.text = thisPatient.patientSurname;
patientSurnameNameView.text = #"Surname:";
patientDoBView.text = thisPatient.patientDoB;
patientDoBDateView.text = #"Date of Birth:";
patientHomeView.text = thisPatient.patientHomeNumber;
patientHomeNumberView.text = #"Home No:";
patientMobileView.text = thisPatient.patientMobileNumber;
patientMobileNumberView.text = #"Mobile No:";
patientAddressView.text = thisPatient.patientAddress;
patientAddressView.editable = NO;
patientAddressPlaceNumberView.text = #"Address:";
patientEmailView.text = thisPatient.patientEmail;
patientEmailAddressView.text = #"Email:";
patientPictureView.image = thisPatient.patientPicture;
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setEditing:(BOOL)editing animated:(BOOL) animated {
if ( editing != self.editing ) {
[super setEditing:editing animated:animated];
patientAddressView.editable = YES;
}
}
#end
Thanks in advance
- (IBAction)PatientRecords:(id)sender {
PRViewController *AddPatient = [[PRViewController alloc] initWithIndexPath:indexPath.row];
[self.navigationController pushViewController:AddPatient animated:YES];
}
This code looks to be the culprit, you're not declaring the variable indexPath anywhere, you need to use index rather than indexpath from what I can see.
Use of undeclared identifyer "indexPath"; did you mean "NSIndexPath"
This error only happen if your do not declare indexPath but try to use it. As Xcode compiler's code sense detect and show you hint that it may be NSIndexPath instead of indexPath. check the use and declaration of indexPath.

UITable Cell not appearing on XIB when navigated from another XIB

I did an empty project on UITable Cells and it worked. Using the same code I added in to an existing project where there are multiple xib linked. When navigating from another xib to this page, the table cells did appear but the content and the required number of cells did not run.
Below is the code of my current project:
OptionViewController.m:
#import "OptionViewController.h"
#import "FishAppDelegate.h"
#import "MainViewController.h"
#import "PetFishViewController.h"
#import "MarineFishViewController.h"
#interface OptionViewController ()
#end
#implementation OptionViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title =#"Choose a category :)";
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)PetFish:(id)sender
{
PetFishViewController *petFish = [[PetFishViewController alloc]initWithNibName:nil bundle:nil];
[[self navigationController] pushViewController:petFish animated:YES];
}
// this is the sub class containing table cells
-(IBAction)MarineFish:(id)sender
{
MarineFishViewController *MarineFish = [[MarineFishViewController alloc]initWithNibName:nil bundle:nil];
[[self navigationController] pushViewController:MarineFish animated:YES];
}
#end
At the current sub class: marine fish
MarineFishController.h
#import <Foundation/Foundation.h>
#interface MarineFishViewController : UITableViewController
{
NSMutableArray *fishList;
}
#end
MarineFishController.m :
#import "MarineFishViewController.h"
#import "MarineFish.h"
#implementation MarineFishViewController
{
/*NSArray *tableData;
NSArray *thumbnail;
*/
}
-(id) init{
//call the superclass
self = [super initWithStyle:UITableViewStyleGrouped];
if(self){
fishList = [[NSMutableArray alloc] init];
MarineFish *Item1 = [[MarineFish alloc] initWithName:#"Shark" imageName:#"Sea.jpg"];
[fishList addObject:Item1];
}
return self;
}
-(id)initWithStyle:(UITableViewStyle)style{
return [self init];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [fishList count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create an instance of uitableviewcell
//check for a reusable cell first, use if exist
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
//if there is no reusable cell of this type,create new one
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"UITableViewCell"];
}
//set text on the cell
MarineFish *p = [fishList objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[[NSString alloc] initWithFormat:#"%#",[p fishName]]];
}
#end
Regarding to the table. I only added one item to the fish list which is item1 and hence should only have one cell in display.
You don't need to override init and initWithStyle: methods. Move your code from init to viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
fishList = [[NSMutableArray alloc] init];
MarineFish *Item1 = [[MarineFish alloc] initWithName:#"Shark" imageName:#"Sea.jpg"];
[fishList addObject:Item1];
}
EDIT
Error was so oblivious...
-(UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
Must return a UITableViewCell object. So put return cell; at the end. Here is edited MarineFishViewController.m.

Resources