开发者

Convert UITableViewController to UITable VIew Object

I currently have a NSArray loading values into a UITableViewController NIB. I realize that I want to use a table along with other objects in my view, such as labels and buttons etc. Therefore I need to use a UITableView object within my UIView.

What is the proper way to code this? Can I copy the methods from my current TabeViewController class to my UIView Class?

#import "LogTableViewController.h"


@implementation LogTableViewController
@synthesize logArray;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (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];

    logArray = [[NSArray alloc]initWithObjects:@"Today's Workout", @"Last Workout", @"Past Week", @"Past Month", @"All Workouts", nil];

    // 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)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(B开发者_Python百科OOL)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
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [logArray 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] autorelease];
    }

    // Configure the cell...

    cell.textLabel.text = [logArray objectAtIndex:indexPath.row];

    return cell;
    }

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}

@end


STEP-1

In your .h(header file), change

@interface <viewControllername>:UITableViewController

to

@interface <viewControllername>:UIViewController

STEP-2:

In your XIB, remove tableView as main view and add a UIView from the library. Connect the view Outlet of the File's Owner to that UIView.

Now you can use the ViewController as normal UIViewController class.

Add a tableView to the View now and give its dimensions.

Also you will need to put an outlet to the tableView now as it is no longer a UITableViewController but a UIViewController. Also dont forget to connect table's datasource and delegate connections to File's owner

Now you can easily put other controls along side the tableView.

Hope this helps you.


If I understand your question correctly, it sounds like you currently have a UITableViewController subclass and you want to change it to be a UIViewController subclass.

There are a few things that you'll need to go through in order to do this, and hopefully I won't forget any of the steps.

First in your @interface file, change your classes' superclass, conform to the datasource and delegate protocols and finally declare an outlet for a UITableView:

@interface YourViewControllerName : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
}
@property (nonatomic, retain) IBOutlet UITableView *myTableView;

Move on to your implementation file and @synthesize the tableView property:

@implementaion YourViewControllerName
@synthesize myTableView;

You also need to change your designated initializer if you do create your viewcontroller programatically and are doing any customisation during it's initialisation (it doesn't seem to be the case for you as you mentioned having a NIB file, but here it is anyway):

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

Copy your table view delegate and datasource methods from the old view controller, plus any custom initialisation code for your (NSArray*) data source. This is usually created in your viewDidLoad: method.

Move on to your NIB file (the one with the tableview object only). Add a UIView object to the nib and move the table view object as a subview of it.

Connect the UIView to your file owner's view outlet, and the UITableView with the file owner's myTableView outlet by dragging from the file owner to the respective objects. Make the file owner your tableview datasource and delegate but control dragging from the tableview to the file owner.

And unless I'm forgetting something, that should be all you need to do.

Feel free to ask any questions in the comments section.

Cheers,

Rog


Parth's answer above was great - but I had to do one additional thing in my code.

One of the things you lose when you change from a UITableViewController to a UIViewController is the property clearsSelectionOnViewWillAppear. So I added an implementation of viewDidAppear as follows:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];
    if (selectedRows) {
        for (NSIndexPath *selectedPath in selectedRows) {
            [self.tableView deselectRowAtIndexPath:selectedPath animated:NO];
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜