Creating a table through a view controller


#1

The chapter demonstrates how to create a table and subview it through the app delegate. How can we do this from a view controller. Let’s say that we have implemented a tabViewController and in one of the views we would like to implement a UITableViewContoller. I have some trouble implementing it.


#2

I got it. Converted the UIViewController to UIViewTableViewController. In the ViewDidLoad method I filled up the possessions array.


#3

I am in a position where I have a UITabBarController and one of the tabs is a UITableViewController. In there I was able to fill the table with the Possessions and implement the edit, delete and moving rows functionality. Now going to chapter 12 I would like to add a UINavigationBarController. How would I do that programmatically?

Shall I implement something similar to (UIView *) headerView ?

I have implemented the following code by pushing a a navigation bar in the headerview. The program crashes when press the Edit button. Do you anything to suggest to me. Should I follow a different route?

[code]- (UIView *) headerView
{
if (headerView) return headerView;

navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];

float w = [[UIScreen mainScreen] bounds].size.width;
CGRect headerViewFrame = CGRectMake(0, 0, w, 48);
headerView = [[UIView alloc] initWithFrame: headerViewFrame];
[headerView addSubview:navBar];

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonSystemItemEdit target:self action:@selector(editingButtonPressed:)];
							   

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Possessions"];

item.leftBarButtonItem = editButton;
item.hidesBackButton = YES;
[navBar pushNavigationItem:item animated:NO];

return headerView;

}[/code]


#4

[quote=“yianni”]I am in a position where I have a UITabBarController and one of the tabs is a UITableViewController. In there I was able to fill the table with the Possessions and implement the edit, delete and moving rows functionality. Now going to chapter 12 I would like to add a UINavigationBarController. How would I do that programmatically?

Shall I implement something similar to (UIView *) headerView ?

I have implemented the following code by pushing a a navigation bar in the headerview. The program crashes when press the Edit button. Do you anything to suggest to me. Should I follow a different route?

[code]- (UIView *) headerView
{
if (headerView) return headerView;

navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];

float w = [[UIScreen mainScreen] bounds].size.width;
CGRect headerViewFrame = CGRectMake(0, 0, w, 48);
headerView = [[UIView alloc] initWithFrame: headerViewFrame];
[headerView addSubview:navBar];

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonSystemItemEdit target:self action:@selector(editingButtonPressed:)];
							   

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Possessions"];

item.leftBarButtonItem = editButton;
item.hidesBackButton = YES;
[navBar pushNavigationItem:item animated:NO];

return headerView;

}[/code][/quote]

You should create an instance of UINavigationController and init with your table view controller as the root view controller, then add that UINavigationController instance to your tab bar controller. At least that’s what I did.