Brett
Brett

Reputation: 12007

iPhone SDK - Adding a static image over a UITableViewController

I am trying to add a static fixed image to a UITableViewController, but when I do the standard [self.view addSubview:imageView]; the image is placed on the tableview and moves with the scrolling.

Is there any way to do this so that the image stays fixed? I know one method would be to create a UIViewController, then add the UIImageView and a UITableView, but unfortunately, I am using a custom UITableViewController (just a library found on gihub to do what I needed), so my controller must be a UITableViewController.

Is there any way to do this? I've been going at this for a while with no luck.

Cheers, Brett

Upvotes: 1

Views: 454

Answers (5)

Lukasz
Lukasz

Reputation: 19916

Do NOT use UITableViewController at all (I never use it and as I've heard nearly any developer uses it). It is a nightmare when you want to customize design with it.

Create your own subclass of UIViewController (MYTableViewController), add UITableView *tableView instance @property and @synthetize it:

@interface MYTableViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {

    UITableView *tableView;


}
@property (nonatomic, retain) IBOutlet UITableView *tableView;

@end

Then in implementation add it to the view (using XIB or viewDidLoad method):

@implementation MYTableViewController
@synthesize  tableView;

// If not XIB used:
-(void)viewDidLoad{

[super viewDidLoad];

CGRect frame = self.view.bounds;
self.tableView = [[[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain] autorelease];

tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];

// And here you van add your image:
[self.view addSubview:imageView];


}



// Do not forget to release it and clear delegate and datasourcce when view uloads:

 #pragma mark - Memory management:

 -(void)dealloc{


    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    self.tableView = nil;


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

    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    self.tableView = nil;


    [super didReceiveMemoryWarning];
}
-(void)viewDidUnload{

    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    self.tableView = nil;


    [super viewDidUnload];
    }

@end

Upvotes: 0

cnu
cnu

Reputation: 815

In my case, I am adding a an Image ( actually button with image) and when user touches on image, it will disappear and tableview will be shown.

so i am disabling scroll first then enable it back

find code below

// in viewDidLoad
[self.view addSubview:imgview];
tbl.scrollEnabled = NO;

// in -(IBAction)btnClicked:(id)sender
[imgview removeFromSuperview];
tbl.scrollEnabled = YES;

Thats working for me.

Upvotes: 0

Martin
Martin

Reputation: 12215

As https://stackoverflow.com/a/6961973/127493 say, UITableViewControllers can be replaced by simple UIViewControllers.

In fact, the trick is to add an UITableView to you UIViewController, make it delegate and etc..., and add it to your UIViewController.view.

So you will be able to add some "sister" views to your controller main view.

Upvotes: 0

mspasov
mspasov

Reputation: 451

Yes, there are few ways. You could create your view hierarchy programmatically at viewDidLoad or use a NIB file. Make sure that you correctly link the delegates and view properties.

If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property. If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.

Upvotes: 0

nhahtdh
nhahtdh

Reputation: 56819

There is no problem using UIViewController idea. You just keep 2 view controllers: 1) UIViewController, which has the UIImageView inside, and subview the view of 2) the UITableViewController. If necessary, make the UITableViewController a strong reference of the UIViewController.

I have done something similar all the time.

Upvotes: 2

Related Questions