Don't see UITableView - ios

My application starts with Root controller called TaskController : UINavigationController as as root view controller of UINavigationController i created class
TaskRootController : UIViewController<UITableViewDelegate> (it has add as view UITableView); When I start application i see only Title form TaskRootController and background color from it. But I don't see table view. If my application starts with TaskRootController as a rootViewController I see table view.
How can I make to see table view in may case ?
PS. Even if I switch TaskRootController to TaskRootController : UITableViewController the behavior is the same.
my code is below:
AppDelegate.m
#implementation AppDelegate
#synthesize window = _window;
#synthesize taskController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.taskController = [TaskController alloc];
self.window.rootViewController = self.taskController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
#end
TaskController.m
#implementation TaskController
#synthesize taskRootController;
- (void) pushInboxController
{
TaskBoxController *taskBoxController = [[TaskBoxController alloc] initWithNibName:nil bundle:NULL];
[self pushViewController:taskBoxController animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self.navigationBar setBarStyle: UIBarStyleBlack];
[self.navigationBar setTranslucent: NO];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.taskRootController = [[TaskRootController alloc] initWithNibName:nil bundle:NULL];
UIViewController *root = self.taskRootController;
[self initWithRootViewController: root];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear: animated];
[self performSelector:#selector(pushInboxController)];
}
#end
TaskRootController.m
#implementation TaskRootController
#synthesize taskRootView;
- (id)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.
NSLog(#"DUPA");
NSLog(#"SIZE x:%f,y:%f ; %f:%f", self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height);
self.view.backgroundColor = [UIColor grayColor];
self.title = #"Root";
self.taskRootView = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
self.taskRootView.delegate = self;
self.taskRootView.dataSource = self;
[self.view addSubview:self.taskRootView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1; // put number for section.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 6; // put number as you want row in section.
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat result = 20.0f;
if([tableView isEqual:self.taskRootView])
{
result = 40.0f;
}
return result;
}
#end

Add this delegate method .
- (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 = #"this is row";
return cell;
}
EDIT :
put init at when you create object of TaskController in AppDelegate.m
self.taskController = [[TaskController alloc]init];
And also put both delegate and datasource to TaskController.h
<UITableViewDataSource, UITableViewDelegate>
and add its relavent methods.

In your AppDelegate
self.taskController = [[TaskController alloc]initWithNibName:#"TaskController" bundle:[NSBundle mainBundle]];
UINavigationController *task = [[UINavigationController alloc] initWithRootViewController:self.taskController];
self.window.rootViewController = task;
In your TaskController
- (void)viewDidLoad
{
[super viewDidLoad];
TaskRootController *tc = [[TaskRootController alloc] initWithNibName:#"TaskRootController" bundle:[NSBundle mainbundle]];
[self addChildViewController:tc];
[tc didMoveToParentViewController:self];
[self.view addSubview:tc.view];
}

Try this ::
set delegate and datasource of your tableview
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [yourArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
cell.textLabel.text = #"row";
return cell;
}
Hope it will help you

in Appdelegate File :-
self.TaskViewController = [[TaskViewController alloc] initWithNibName:#"TaskViewController" bundle:nil];
self.navigationController=[[UINavigationController alloc]initWithRootViewController:self.TaskViewController];
[self.window setRootViewController:navigationController];//ios-6
or
[self.window addSubview:navigationController.view];//<ios-6
Add a Delegate Method in UITableViewDelagates:-
- (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 = #"Name";
return cell;
}

Related

EXC_BAD_ACCESS when creating table view programmatically

I'm trying to create a view programmatically which will have two views inside of it, one is for searching and the other one is a tableview which will shows photos;
But i'm having EXC_BAD_ACCESS error with code=2, all of the controller code is below. I suspected there is an infinite loop, but don't understand why.
Thanks for any help...
#interface PhotosViewController () <UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) UITableView *tableView;
#end
#implementation PhotosViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = #"Instagram";
[self.tableView registerClass:[PhotoTableViewCell class] forCellReuseIdentifier:CellIdentifier];
self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
self.tableView.allowsSelection = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadView
{
_tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView reloadData];
[self.view addSubview:_tableView];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PhotoTableViewCell *cell = (PhotoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// NSLog(#"%f", self.navigationController.navigationBar.bounds.size.height);
return /*tableView.bounds.size.height -self.navigationController.navigationBar.bounds.size.height -*/40.0;
}
#pragma mark - Helper Methods
- (void)configureCell:(PhotoTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// configure photo cell
if (cell == nil) {
cell = [[PhotoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.namelabel.text = #"Mister Tester";
cell.dateLabel.text = #"2 hours ago";
}
In loadView should you not call [super loadView] or assign self.view first.
I believe reading self.view without that can cause an infinite loop.

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;
}

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.

Error with SplitView

I have an iphone app that I'm trying to make a universal app. I created a separate project to play around with creating a split view app for iPad. I got the basics of it working so I'm trying to implement it in my existing project but I'm getting an error when running on iPad.
2013-06-06 08:57:08.716 KFBNewsroom[26898:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "KFBMasterViewController" nib but didn't get a UITableView.'
The code for the split view is the same as it was in my test project so I can't figure out why it won't work here. Any ideas?
Here is the code from my didFinishLaunchingWithOptions method where I say what to load on different devices.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
self.viewController = [[KFBViewController alloc] initWithNibName:#"KFBViewController" bundle:nil];
self.window.rootViewController = self.tabBarController;
}
else
{
masterViewController.detailViewController = detailViewController;
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = #[masterNavigationController, detailNavigationController];
self.window.rootViewController = self.splitViewController;
}
MasterViewController.h:
#import <UIKit/UIKit.h>
#class KFBDetailViewController;
#interface KFBMasterViewController : UITableViewController
#property (strong, nonatomic) KFBDetailViewController *detailViewController;
#end
MasterViewController.m:
#import "KFBMasterViewController.h"
#import "KFBDetailViewController.h"
#import "DetailViewManager.h"
#interface KFBMasterViewController () {
NSMutableArray *_objects;
NSMutableArray *menu;
}
#end
#implementation KFBMasterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Master", #"Master");
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// self.navigationItem.leftBarButtonItem = self.editButtonItem;
// UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
// self.navigationItem.rightBarButtonItem = addButton;
menu = [NSMutableArray arrayWithObjects:#"Home", #"Public Affairs", #"Action Alerts", #"Market Updates", #"Ag Stories", #"KFB News", #"Member Benefits", #"Monthly Video", #"Photos", #"Social Media", #"About Us", #"Contact Us", #"KYFB.com", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return menu.count;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIImageView *image = [[UIImageView alloc]init];
image.image = [UIImage imageNamed:#"CellImage.png"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// NSDate *object = _objects[indexPath.row];
// cell.textLabel.text = [object description];
cell.textLabel.text = [menu objectAtIndex:indexPath.row];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
cell.textLabel.font = [UIFont fontWithName:#"FranklinGothicStd-ExtraCond" size:20.0];
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundView = image;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[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;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#end
You probably have to change the super-class of your rootViewController from UITableViewController to UIViewController.

iOS: Cells are not registering with TableView

I am following this tutorial and stuck with Getting Started part itself. In this article cells are being registering programatically. I followed all the steps, downloaded the code and compared it mine but can't figure out the problem
SHCAppDelegate.m
#implementation SHCAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[SHCViewController alloc] initWithNibName:#"SHCViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
SHCViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_toDoItems = [[NSMutableArray alloc] init];
[_toDoItems addObject:[SHCToDoItem toDoItemWithText:#"Feed the cat"]];
[_toDoItems addObject:[SHCToDoItem toDoItemWithText:#"Buy eggs"]];
[_toDoItems addObject:[SHCToDoItem toDoItemWithText:#"Pack bags for WWDC"]];
}
}
- (void)viewDidLoad:(BOOL)animated
{
[super viewDidLoad];
self.tableView.dataSource = self;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cell"];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _toDoItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *ident = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
int index = [indexPath row];
SHCToDoItem *item = _toDoItems[index];
NSLog(#"%#",item.text);
cell.textLabel.text = item.text;
return cell;
}
Here NSLog inside tableView:(UITableView *)tableView cellForRowAtIndexPath: are not getting print on console.
after setting datasource call the reload table method as
[self.tableview reloadData];
Try this
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource = self;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cell"];
[self.tableview reloadData];
}

Resources