Hide a UITableView of a UIScrollview on a button click in iPhone - iphone

I have a UITableView with some data in -(void)ViewDidLoad. So it appears accordingly. But when I click a button then my tableview should be hidden and I should load a UIlabel with some text.
But for me on button click empty tableview with some rows is loading. I should avoid it. How can I do it?
Here is my code
-(void)ViewDidLoad {
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,380,320,200) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
-(void)DatePickerDoneClick:(id)sender {
[tableview setHidden:YES];
displayError =[[UILabel alloc]init];
[displaytError setFrame:CGRectMake(20,400,320,100)];
displayError.textAlignment=UITextAlignmentLeft;
displayError.backgroundColor=[UIColor clearColor];
displayError.text=[NSString stringWithFormat:#"Not found"];
[self.view addSubview:displayError];
}
Could not get where I'm going wrong?

To hide Table View you can use
myTableView.hidden = YES;
or
[mtTableView setHidden:YES];
Hope this will help you out.

if tableview.hidden = YES; is not doing the job
try the next code:
[self.tableview removeFromSuperView];

Related

UIActivityIndicatorView in an UIScrollview

I have a full screen UIScrollView to display my image. An UIActivityIndicatorView is added to the UIScrollView, it spinning well, but how could i make it always spinning in the middle of the screen while I am scrolling, zooming, rotating?
If you add the UIActivityIndicatorView directly to the scroll view it will scroll with the scroll view. But, if you add it to the parent of the scroll view it will remain where it was placed. So, the solution is to add it to the parent of the scroll view.
Notes:
I would recommend having a UIViewController in your window, and then adding these both to the UIViewController.
See the discussion here about adding views directly to your window:
View Controller being sent a message even though it has been deallocated
In ur .h file
UIView *primaryImage;
UIView *secondaryImage;
UIActivityIndicatorView *indicator;
In ur .m file
-(void)indicatorView
{
primaryImage = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
primaryImage.backgroundColor = [UIColor blackColor];
primaryImage.alpha =0.5;
//[self.view.superview insertSubview:primaryImage aboveSubview:self.view.superview];
//[theTableView addSubview:primaryImage];
[self.view addSubview:primaryImage];
secondaryImage = [[UIView alloc] initWithFrame:CGRectMake(127.50,215,65,50)];
secondaryImage.backgroundColor = [UIColor blackColor];
secondaryImage.alpha = 0.9;
secondaryImage.layer.cornerRadius = 12;
[primaryImage addSubview:secondaryImage];
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(30, 25, 25, 25)];
indicator.center = CGPointMake(32, 25);
//[indicator hidesWhenStopped];
[indicator startAnimating];
[secondaryImage addSubview:indicator];
}
-(void)dismissCoverImageView {
[indicator stopAnimating];
[indicator removeFromSuperview];
[secondaryImage removeFromSuperview];
[primaryImage removeFromSuperview];
}
and after that you can call [self indicatorView];
and [self dismissCoverImageView];
Define the UIScrollViewDelegate of your UIScrollView. And in the delegate method –(void)scrollViewDidScroll:(UIScrollView *)scrollView change the frame of UIActivityIndicator object.

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.

UITableView addSubview and table lines

I'm trying to add a custom view to an UITableView created with code from an UITableViewController. This view is a HUD window with a message (MBProgressHUD)
So I have a method reload() called from the overridden initWithStyle() method and from the refresh button of the table:
- (void) reload {
HUD = [[MBProgressHUD alloc] initWithView:self.tableView];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Downloading";
[HUD showWhileExecuting:#selector(reloadWithHUD) onTarget:self withObject:nil animated:YES];
}
The first time, the HUD appears behind the table lines. Once loaded, when I press reload button, the view shows as expected. The initWithStyle() method (with some code removed for clarity) is:
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
// Custom initialization.
self.title = NSLocalizedString...
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle ... action:#selector(reload)];
self.navigationItem.rightBarButtonItem = button;
[button autorelease];
[self reload];
}
return self;
}
I've tried changing the HUD view with a simple UILabel, with the same result.
I also changed the code to call reload() from viewDidLoad(), but doesn't work either. How can I resolve this issue? Thank you very much.
EDIT : to clarify this, here is a possible solution to this problem. Many thanks to Bill Brasky for his help:
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[[[UIApplication sharedApplication] keyWindow] addSubview:HUD];
Here is what I'm doing in my app with an almost identical application.
You need to add it to the main view WINDOW, not the tableView.
HUD = [[MBProgressHUD alloc] initWithView:self.view.window];
[self.view.window addSubView:HUD];
My project is based on IOS 7
This helps me
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

Update UIPickerView Number of Rows

I have a UIPickerView that is populated by an NSMutableArray called sectionNamesArray. When an item is added to sectionNamesArray, how do I manually get this delegate to be called?
-- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return ([sectionNamesArray count] + 1);
}
The problem is that I add this pickerView to the center of a navBar in the titleView. If I try to nil the UIPickerView and reallocate it in order to hopefully get the delegate called again, the PickerView shows up on the left side of the navbar, instead of the center. I am guessing maybe the interface builder settings got it in the center...?
Here's the code for that:
-(void) setupPickerView{
myPickerView = nil;
myPickerView = [[UIPickerView alloc] init];
myPickerView.backgroundColor = [UIColor clearColor];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.navigationController.navigationBar setBackgroundView:myPickerView];
CGSize pickerSize = [myPickerView sizeThatFits:CGSizeZero];
UIView *pickerTransformView = [[UIView alloc] initWithFrame:CGRectMake(-40.0, -43.0, pickerSize.width, pickerSize.height)];
pickerTransformView.transform = CGAffineTransformMakeScale(.55f, .55f);
[pickerTransformView addSubview:myPickerView];
[self.navigationController.navigationBar setBackgroundView:pickerTransformView];
}
When you add something to your array can't you use [UIPickerView reloadAllComponents] or - (void)reloadComponent:(NSInteger)component?
That will most likely query your delegate and call everything necessary to check your array and reload the view.

iPhone UITableView PlainStyle with custom background image - done "entirely" in code

I have been all over the place, seems the UITableView with a static background issue is well documented, but no one with a straight forward solution?
Im building my TableViews entirely in code, like this:
UIViewController *tableViewController = [[TableViewController alloc] init];
navigationController = [[UINavigationController alloc]
initWithRootViewController:tableViewController];
[tableViewController release];
[window addSubview:navigationController.view];
The window is my main UIWindow build for me in the app delegate. From here on I need to build a few different TableViews (controlled by the navigationController), some with fetchedResultsControllers, custom cells and so on. I prefer to do this completely in code, not using nib's as this would result in either having customization spread between code and IB or having to build and maintain 6+ different Nibs.
I simply can't find a working example where a tableViewController Class sets it's own background image. If I do this inside one of my TableViews (extending UITableViewController):
self.tableView.backgroundColor = backgroundColor;
I, of course, get the tableView's background colored (which incidentally colors the cell's as well, think the cell's inherits their color from the tableView?) but I wish to have a static background image that my cells slide up and down on top of. Not a "background image" that slides up and down with the users gestures.
Exactly what the GroupedStyle tableView offers, but in a PlainStyle tableView:) .. and done using code, not IB.
I guess I have to clear the background color of the table view, then set the Cells color when configuring them so they don't turn out transparent. And then somehow "sneak" a background image below the tableView view from inside the tableView instance?
How will I go about this, the best solution would to be able to do this in viewDidLoad or any other function inside my TableViewController, to keep all my customization in one place.
Hope someone can help me, Im all 'googled out' :) Thanks!
You need to set up your controller as a UIViewController, not a UITableViewController. Then add the tableview programmatically above a background imageView.
#interface SomeController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
...
UITableView *tableView;
...
}
#property (nonatomic, retain) UITableView *tableView;
#end
#implementation SomeController
#synthesize tableView;
...
- (void)loadView {
[super loadView];
UIImageView *v = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[v setImage:[UIImage imageNamed:#"table_background.png"]];
[self.view addSubview:v];
self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];
}
...
#end
All of this jumping through hoops is unnecessary if you're targeting > iOS 3.2, where you can use the new backgroundView property to set a UIImageView directly, e.g.:
// In your UITableViewController subclass
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
self.tableView.backgroundView = view;
}
Ok, now it is running:)
My tableView was not populated with my cells, so breakPointing through the thing I found out
that even though I had implemented the TableViewDataSource and TableViewDelegate, this was only in the main view, I needed to set the delegate and datasource of the tableview to = self.
For others seeking an answer to this here is the method as it ended up with Coneybeares help:
- (void)loadView {
[super loadView];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[imageView setImage:[UIImage imageNamed:#"carbon_background.png"]];
[self.view addSubview:imageView];
[self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
Thanks Coneybeare.
It doesn't crash anymore and the background image turns up just perfect (along with my navigationController in the top)
However, still no visible tableView? just the background image and the navigationControllerBar:
This is my implementation:
- (void)loadView {
[super loadView];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[imageView setImage:[UIImage imageNamed:#"carbon_background.png"]];
[self.view addSubview:imageView];
[self.tableView = [[UIView alloc] initWithFrame:self.view.bounds] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Hello, World";
return cell;
}
//commit edit, didSelectRow, memory … etc.
The forum wasn't up for an entire .m file in one go.
Please tell me if I left something out that could help indicate an error.
I thought maybe it was the order of the layers and tried this without any luck:
[self.view sendSubviewToBack:imageView];
Hope I missed something obvious.
Thanks for your time:)
Some tips:
If using a UISplitViewController:
splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
If using a UINavigationController:
navigationController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
If using both a UISplitViewController and a UINavigationController:
navigationController.view.backgroundColor = [UIColor clearColor];
splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
Be sure to set the background color of any UIView you want to see through that is on top of the UINavigationController or UISplitViewController to [UIColor clearColor].