I have a problem with memory leak in UITableView. If I'm at the bottom of DetailViewController table and I want to go back to MasterViewController I have a crash. If I use UITableViewController, crash does not happen.
AppDelegate
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor = [UIColor blackColor];
_window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[MasterViewController alloc] init]];
[_window makeKeyAndVisible];
MasterViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
DetailViewController *detailViewController = [[DetailViewController alloc] init];
[self.navigationController pushViewController:detailViewController animated:YES];
}
DetailViewController
#interface DetailViewController ()
#property (nonatomic, strong) UITableView *tableView;
#end
#implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 60;
[self.view addSubview:_tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 30;
}
- (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];
}
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"scrollViewDidScroll");
}
#end
Related
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.
In my TableViewController, I set the data I want to load in my DetailView (ws)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath sender:(id)sender{
detailViewController *detailVC = [[detailViewController alloc] init];
detailVC.ws = [self.tabAssociation objectAtIndex:indexPath.row];
[detailVC viewDidLoad] ;
//detailVC.descriptionTextView = [[UITextView alloc] init];
//[self.navigationController pushViewController:detailVC animated:YES];
}
And this is what my DetailView must load
- (void)viewDidLoad
{
[super viewDidLoad];
self.barre.title = self.ws.associationName ;
self.descriptionTextView.text = self.ws.associationDescription ;
}
But when I select the row, a white page appears and not my associationDescription in a text view, neither my associationName
But my viewDidLoad seems to be called before detailVC.ws is loaded, ie my detailVC is empty
Here is my TableViewController.h
import UIKit/UIKit.h
#interface MasterViewController : UITableViewController{
NSArray *tabAssociation;
}
#property (nonatomic, retain) NSArray *tabAssociation;
#end
And the TableViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MasterViewController () {
}
#end
#implementation MasterViewController
#synthesize tabAssociation ;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *dictFromFile = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"AssociationTest" ofType:#"plist"]];
NSMutableArray *associationToAdd = [[NSMutableArray alloc] init];
NSEnumerator *enumerator = [dictFromFile objectEnumerator];
NSDictionary *anObject;
while ((anObject = [enumerator nextObject])) {
association *newAssocition = [[association alloc] initWithDictionaryFromPlist: anObject];
[associationToAdd addObject: newAssocition];
}
self.tabAssociation = [NSArray arrayWithArray:associationToAdd];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tabAssociation.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// On récupère l'objet Website qui correspon à la ligne que l'on souhaite afficher
association *ws = [self.tabAssociation objectAtIndex:indexPath.row];
cell.textLabel.text = ws.associationName;
// On renvoie la cellule configurée pour l'affichage
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 didSelectRowAtIndexPath:(NSIndexPath *)indexPath sender:(id)sender{
detailViewController *detailVC = [[detailViewController alloc] init];
detailVC.ws = [self.tabAssociation objectAtIndex:indexPath.row];
//[detailVC viewDidLoad] ;
[self presentViewController:detailVC animated:YES completion:nil];
//detailVC.descriptionTextView = [[UITextView alloc] init];
//[self.navigationController pushViewController:detailVC animated:YES];
}
/*- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}*/
#end
You should never call -viewDidLoad directly. What you want to do is present you detailViewController somehow.
If you're inside a UINavigationController, something like:
[self.navigationController pushViewController:detailVC animated:YES];
should work. If you're not, then you have to think about how you're going to present your view controller.
You can try something like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath sender:(id)sender{
detailViewController *detailVC = [[detailViewController alloc] init];
detailVC.ws = [self.tabAssociation objectAtIndex:indexPath.row];
[self presentViewController: detailVC animated:YES completion:nil];
}
I am having some issues with my app, I am currently using the WEPopover library (ARCified) to create some custom popovers. However, everytime i present a popover and it's corresponding view (consisting of a view and a tableview populated with an array) the allocations in instruments grow, even if im only re-adding the same popover again and again it grows. What am I missing, or is this normal behaviour, see code below. Should I instead make the table view weak or something?
ThemesPopOverViewController.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface ThemesPopOverViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
NSArray *themes;
}
#property (nonatomic, retain) UITableView *tableView;
#end
ThemesPopOverViewController.m
#import "ThemesPopOverViewController.h"
#interface ThemesPopOverViewController ()
#end
#implementation ThemesPopOverViewController
#synthesize tableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
themes = [[NSArray alloc] initWithObjects:kRegexHighlightViewThemeArray];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width - 40, 350)];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
[self.view addSubview:self.tableView];
}
-(void) viewDidAppear:(BOOL)animated{
int item = [themes indexOfObject:theDelegate.codeView.currentTheme];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; //count of section
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//NSLog(#"%d",themes.count);
return [themes count]; //count number of row from counting array hear cataGorry is An Array
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor colorWithRed:25/255.0f green:185/255.0f blue:152/255.0f alpha:1.0f]];
[cell setSelectedBackgroundView:bgColorView];
}
[cell.textLabel setFont:[UIFont fontWithName:#"Helvetica" size:16]];
cell.textLabel.text = [themes objectAtIndex: indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"%d", indexPath.row);
[theDelegate.codeView setHighlightThemeFromString:[themes objectAtIndex:indexPath.row]];
[theDelegate.codeView setNeedsDisplay];
themes = nil;
[theDelegate removePop];
}
#end
I add the popover from:
-(void) settingAct:(UIButton *)sender{
if (!popover) {
ThemesPopOverViewController *newView = [[ThemesPopOverViewController alloc] initWithNibName:#"SettingPopOverViewController" bundle:[NSBundle mainBundle]];
self.popover = [[WEPopoverController alloc] initWithContentViewController:newView];
[self.popover setContainerViewProperties:[self improvedContainerViewProperties]];
[self.popover setPopoverContentSize:CGSizeMake(128, 360)];
[self.popover presentPopoverFromRect:CGRectMake(sender.center.x+12, sender.center.y, 0, 20)
inView:self.window.rootViewController.view
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown|
UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight)
animated:YES];
newView = nil;
}else{
[self removePop];
}
}
And the remove method in "theDelegate" for dismissing popovers:
-(void) removePop{
[self.popover dismissPopoverAnimated:YES];
self.popover = nil;
}
Baseline = no popovers, Heapshot1 = added popover, Heapshot2 = popover dissmissed, Heapshot3 = added same popover again, Heapshot4 = popover dissmissed.
Snapshot Timestamp Heap Growth # Persistent
- Baseline - 00:07.464.624 1.59 MB 25041
Heapshot 1 00:11.759.060 50.34 KB 787
Heapshot 2 00:16.278.744 0 Bytes 0
Heapshot 3 00:24.312.874 30.39 KB 608
Heapshot 4 00:28.361.293 288 Bytes 7
In Your SettingsAct button~action~method do the code like this
if (self.popoverController)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
else
{
ThemesPopOverViewController *newView = [[ThemesPopOverViewController alloc] initWithNibName:#"SettingPopOverViewController" bundle:[NSBundle mainBundle]];
self.popover = [[WEPopoverController alloc] initWithContentViewController:newView];
[self.popover setContainerViewProperties:[self improvedContainerViewProperties]];
[self.popover setPopoverContentSize:CGSizeMake(128, 360)];
[self.popover presentPopoverFromRect:CGRectMake(sender.center.x+12, sender.center.y, 0, 20)
inView:self.window.rootViewController.view
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown|
UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight)
animated:YES];
}
it works fine for me :)
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;
}
I have been struggling a quite long time with this problem. Maybe you could help me. So i want to create programmatically a basic UINavigationController with simple UITableView.
For example UITableView will contain some info about "Colors". Item list would be something like: "Yellow", "Orange", "Green", "Purple".
I would be really really happy if you can help me to get this right.
Try this:
#interface MyViewController: UITableViewController {
NSArray *data;
}
#end
#implementation MyViewController
- (id)init
{
if ((self = [super init]))
{
data = [[NSArray alloc] initWithObjects:#"Red", #"Green", #"Blue", nil];
}
return self;
}
- (void)dealloc
{
[data release];
[super dealloc];
}
- (int)tableView:(UITableView *)tv numberOfRowsInSection:(int)s
{
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)ip
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"CELLID"];
if (!cell) cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault identifier:#"CELLID"] autorelease];
cell.textLabel.text = [data objectAtIndex:ip.row];
return cell;
}
#end
Use it like:
MyViewController *ctrl = [[MyViewController alloc] init];
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:ctrl];
[ctrl release];
etc.