How can I show an empty view in place of a UITableView? - iphone

I have a UITableViewController. When a certain BOOL is set, I want to show another view instead of the UITableView. How can I do this? I need to be able to bring the UITableView back also.

I implemented the following method in my Table view controller subclass to reload its background.
- (void)reloadBackground
{
if(myBool)
{
//load the view to be displayed from a nib
NSArray* nibs = [[NSBundle mainBundle] loadNibNamed:#"EmptyView" owner:self options:nil];
UIView* emptyView = [nibs objectAtIndex:0];
self.tableView.backgroundView = emptyView;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
else
{
self.tableView.backgroundView = nil;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
}
HTH,
Akshay

Instead of a UITableViewController, you can put two views inside a UIViewController:
#interface MyViewController : UIViewController <UITableViewDelegate> {
UITableView *tableView;
UIWebView *anotherView;
}
...
if (someBool) {
tableView.hidden = YES;
anotherView.hidden = NO;
} else {
tableView.hidden = NO;
anotherView.hidden = YES;
}

How about..
if(myBOOL == YES) {
ADifferentViewController * vc = (ADifferentViewController *)[[ADifferentViewController alloc]init];
[self.navigationController pushViewController:vc animated:NO];
[vc release];
}

Possibly something like:
if (myBool == YES) {
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:myView];
}
Now to get rid of it:
[myView removeFromSuperview];
Of course there are plenty of other methods...
Even: self.view = myView;

Use a UIView instead if UITableViewController.... from .h file change
UITableViewController to UIViewController Connect view to Files owner
from xib
take UITableView in a UIView... take another UIView for a blank view
and set its background color as Tableview color
If your bool if true
then show tableview
and hide blank View else
hide tableview
show blank view

Related

ViewController with subviews, UIScrollView not scrollable first time

Im using a UIViewController with several subviews from XIBs, (ViewController's views), they are chosen between using UISegmentedControl. One of these views are containing a UIScrollView. The problem is that the UIScrollView is not scrollable the first time this subview is added. If I choose another segment/view and then the one with the UIScrollView again, now it's scrollable.
The UIScrollView is added in IB only. This is some of the codes for the views and UISegmentedControl in MainViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
nameSubViewController = [[NameSubViewController alloc] initWithNibName:#"NameSubViewController" bundle:nil];
nameSubView = (NameSubView *)[nameSubViewController view];
priceSubViewController = [[PriceSubViewController alloc] initWithNibName:#"PriceSubViewController" bundle:nil];
priceSubView = (PriceSubView *)[priceSubViewController view];
[self.view addSubview:nameSubView];
currentView = nameSubView;
}
- (void) segmentAction:(id)sender
{
segmentedControl = sender;
if([segmentedControl selectedSegmentIndex] == 0) {
[currentView removeFromSuperview];
[self.view addSubview:nameSubView];
currentView = nil;
currentView = nameSubView;
}
if([segmentedControl selectedSegmentIndex] == 1) {
[currentView removeFromSuperview];
[self.view addSubview:priceSubView];
currentView = nil;
currentView = priceSubView;
}
In this code, let's say segment 1 (priceSubView) contains the UIScrollView. If I choose this segment, the UIScrollView does not react on the scrolling unless I choose segment 0 (nameSubView) again and re-select segment 1.
What is causing this and how to fix it?
If you need to scroll at first load.
You need to add the scrollView at firstLoad.
You are adding the scroll view after changing the segment control, that's why it's not scrolling. Check it out.
Change viewDidLoad like:
- (void)viewDidLoad
{
[super viewDidLoad];
nameSubViewController = [[NameSubViewController alloc] initWithNibName:#"NameSubViewController" bundle:nil];
nameSubView = (NameSubView *)[nameSubViewController view];
priceSubViewController = [[PriceSubViewController alloc] initWithNibName:#"PriceSubViewController" bundle:nil];
priceSubView = (PriceSubView *)[priceSubViewController view];
[self.view addSubview:priceSubView];
currentView = priceSubView;
}

Navigating from ScrollViewController Content view to another view on stack

I am trying to navigate from a UIScrollView's content view to another view by using a UIButton. I do not want to display this other view modally, and pushing the view on the stack is not working for me.
Example:
My Viewcontroller contains a Scrollview that has 2 content views. Within these content views I would like to push another view onto the stack.
Is what I am trying to do possible?
Ok let's see if I understood well enough your problem.
#interface MyViewController ()
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) UIView *firstPlayerView;
#property (nonatomic, strong) UIView *secondPlayerView;
#end
#implementation MyViewController
-(UIView *)firstPlayerView
{
if (!_firstPlayerView) {
_firstPlayerView = [[UIView alloc] initWithFrame:self.view.bounds];
// set up your view as you like and place the button to go to the second player
// view when you need to.
// let's suppose that you called that button "goToSecondPlayerViewButton"
[goToSecondPlayerViewButton addTarget:self
action:#selector(switchPlayer)
forControlEvents:UIControlEventTouchUpInside];
}
return _firstPlayerView;
}
-(UIView *)secondPlayerView
{
if (!_secondPlayerView) {
_secondPlayerView = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0)];
// set up your view as you like and place the button to go to the first player
// view when you need to.
// let's suppose that you called that button "goToFirstPlayerViewButton"
[goToFirstPlayerViewButton addTarget:self
action:#selector(switchPlayer)
forControlEvents:UIControlEventTouchUpInside];
}
return _secondPlayerView;
}
-(UIScrollView *)scrollView
{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.autoresizingMask = UIViewAutoresizingNone;
[_scrollView addSubview:self.firstPlayerView];
[_scrollView addSubview:self.secondPlayerView];
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height);
_scrollView.pagingEnabled = YES;
_scrollView.scrollEnabled = NO;
_scrollView.bounces = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
}
return _scrollView;
}
-(void)switchPlayer
{
if(self.scrollView.contentOffset.x == 0) {
[self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) animated:YES];
self.title = #"Second Player";
} else {
[self.scrollView scrollRectToVisible:self.scrollView.bounds animated:YES];
self.title = #"First Player";
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:self.scrollView];
self.title = #"First Player";
}
#end
I hope that solves your problem! I haven't tested the code so if you encounter problems just comment and I'll try to help.
Edited : To add a view you just enlarge the scroll view contentSize and add that view as its subview
-(UIView *)thirdView
{
if (!_thirdView) {
_thirdView = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds * 2, self.view.bounds.size.width, 0)];
// set up your view as you like
}
return _thirdView;
}
-(void)addThirdView
{
self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width * 3, self.scrollView.bounds.size.height);
[self.scrollView addSubview:self.thirdView];
[self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width * 2, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) animated:YES];
}
You can generalize this and set your content size first then have a single method that takes an index as an argument.
-(void)scrollToViewAtIndex:(NSInteger)index
{
[self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width * index, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height)
animated:YES];
}

How to fix border in UITableView for the UITableViewStyleGrouped

For a UITableViewStyleGrouped UITableView a small extra line is drawn below the tableview
How do I fix this?
I have tried my best. No luck so far.
Thank's in advance.
My Code
-(void)myTableView
{
// if(mytableView != nil)
// [mytableView release];
if (mytableView == nil) {
mytableView = [[UITableView alloc]initWithFrame:myFrame style:UITableViewStyleGrouped];
}
mytableView.delegate=self;
mytableView.dataSource=self;
mytableView.backgroundView=nil;
mytableView.scrollEnabled = FALSE;
[self.view addSubview:mytableView];
[self myPortraitMode];
}
Background of the table is fixed is an image.
I think what you are referring to is the Bevel color of the table view.
The answer is in this post.
Basically, you need to set the table separatorStyle to UITableViewCellSeparatorStyleNone or to UITableViewCellSeparatorStyleSingleLine.
You need to set a background view:
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor yourColor];
tableview.backgroundView = backgroundView;
[backgroundView release];
You don't need to set the properties every time.
-(void)myTableView
{
if (mytableView == nil) {
mytableView = [[UITableView alloc]initWithFrame:myFrame style:UITableViewStyleGrouped];
mytableView.delegate=self;
mytableView.dataSource=self;
mytableView.backgroundView=nil;
mytableView.scrollEnabled = NO;
[self.view addSubview:mytableView];
[self myPortraitMode];
}
}
I don't really see what the problem is though.

Where can I specify which UIView is used for the view property of a UIViewController?

I want to set the view property of a UIViewController at runtime. I have an .xib file with two views, and I want my UIViewController subclass that owns the .xib file to decide which UIView to use at runtime. I thought I could do this in loadView by just saying
if(some condition)
self.view = thisView;
else
self.view = thatView;
but that didn't work. How can I do this?
If you want to choose your view dynamically, set it inside -[UIViewController loadView]. A word of caution though: calling -[UIViewController view] will call -[UIViewController loadView] if the view hasn't been loaded yet, so if you do this:
-(void)loadView
{
self.view = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
self.view.backgroundColor = [UIColor redColor];
}
The second line of that method will call -loadView, and you'll get infinite recursion (which will lead to a stack overflow, and a crash). You need to setup your view, then set the .view property when you've set it up, like this:
-(void)loadView
{
UIView *newView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
newView.backgroundColor = [UIColor redColor];
self.view = newView;
}
So you'll probably want to do something like this:
-(void)loadView
{
UIView *newView = nil;
if (self.theSkyIsBlue) {
newView = [[[BlueSkyView alloc] initWithFrame:CGRectZero] autorelease];
newView.backgroundColor = [UIColor blueColor];
}
else {
newView = [[[GraySkyView alloc] initWithFrame:CGRectZero] autorelease];
newView.backgroundColor = [UIColor grayColor];
}
self.view = newView;
}
Addendum 1 - update to show how to use a container view for different views defined in a XIB
If you want to reference other stuff in your XIB, a better approach is to use your .view as a "container view" for your other views. Set it up in -viewDidLoad, like this:
- (void)viewDidLoad
{
UIView *childView = nil;
if (someCondition) {
childView = self.blueView;
}
else {
childView = self.grayView;
}
[self.view addSubview:childView];
childView.frame = self.view.bounds;
}
Note that if you want to swap your views later on, you should make childView a property, instead of a local variable, so you can remove the old childView when inserting a new one.
Inside -(void)loadView; method is where you create your view, so there is where you want to set it conditionally ;)

UIViewController shows two UITableView

I have a weird problem.
I'm subclassing UIViewController and adding a tableView property in which I load a UITableView.
Now I'm adding this UITableView to the parent's subviews.
Works fine but I get TWO TableViews. One standard styled TableView and one the way I wanted it to be on top of the standard one. Both contain the correct data though.
#interface RootViewController : UIViewController <ABPersonViewControllerDelegate, UITableViewDelegate, UITableViewDataSource> {
UITableView *tableView;
ToolbarController *toolbar;
...
}
#property(nonatomic, retain) UITableView *tableView;
#property(nonatomic, retain) ToolbarController *toolbar;
...
#end
#implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect mainViewBounds = self.parentViewController.view.bounds;
CGFloat toolbarHeight = 44;
toolbar = [[ToolbarController alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(mainViewBounds), toolbarHeight) parentView:self];
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, toolbarHeight, CGRectGetWidth(mainViewBounds), CGRectGetHeight(mainViewBounds) - toolbarHeight) style:UITableViewStylePlain];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.rowHeight = 60.0;
[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
[self.parentViewController.view addSubview:tableView];
[self.parentViewController.view addSubview:toolbar];
}
#end
------UPDATED------
I just wrongly pasted one part
[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;
is actually in code
tableView.delegate = self;
tableView.dataSource = self;
-------UPDATE 2--------
If I don't create my own UITableView by
tableView = [[UITableView alloc] ...]
then there is one automatically set for me.
This would be fine for me... I don't need to init one, but I can't change the frame on the standard one.
tableView.frame = CGRectMake(0, toolbarHeight, CGRectGetWidth(mainViewBounds), CGRectGetHeight(mainViewBounds) - toolbarHeight);
Setting frame has no effect whatsoever...
i Had the same issue you probably be converting the UITableViewController to UIViewController and Your RootViewController might have the uitableview.
Make the Property of _tableview in your RootviewController with IBOutlet. And link it with the tableview in your XIB .
Don't alloc _tableview in RootviewController and Don't addsubview it .
Instead of this,
[tableView tableView].delegate = self;
[tableView tableView].dataSource = self;
Have you tried just using:
tableView.delegate = self;
tableView.dataSource = self;
I don't know why it would be that, but it's the only thing that's standing out. Also make sure you haven't got another UITableView set up in IB.
It might also have something to do with toolbarController.
I'm assuming it's a custom class, so make sure you're not creating a UITableView in that.
I ran into same issue...
Instead of adding tableview to the view controller directly ([self.parentViewController.view addSubview:tableView];), use following way..
UIView *totalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[totalView addSubview:tableView];
self.view=totalView;
Hope this fixes it (for #kris too!)