i struggled almost 1 hour but i couldn't figure it out as how to display UIactivity indicator in UItableview as i cant use IB in UItableview(its not dragging) on iphone
i used this and its working
but i want to display in viewDidload
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//loadingView =[LoadingView loadingViewInView:[self.view.window.subviews objectAtIndex:0]];//.window.subviews
NSLog(#"dgdddddddddddddddddddd");
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(130, 10, 25, 25)];
//UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 30.0, 320.0, 60.0)];
activityView.center=customView.center;
[activityView startAnimating];
[ customView bringSubviewToFront:activityView];
[customView addSubview:activityView];// [headerLabel release];
return [customView autorelease];
}
kindly suggest
if the view attribute of the file is linked to the UITableView , then the subview will not be added. Try replacing the view attribute with a normal view which contains a table view and try adding the indicator. Also try to make the file owner class from uitableviewcontroller to uiviewcontroller class.
Related
How to add a button on top off viewForHeaderInSection. I like to add a button above section header which can be scrollable with tableview. Any idea how to do this ?
The - rather hackish - solution I saw in a pull-to-refresh implementation is to simply add your 'extra' view to the table view as a subview - just make sure it's positionned using a negative y offset. That offset should be equal to (well, rather -1 times) the height of your view. Code:
UIView *myViewAboveHeader = // however you create it
CGRect f = myViewAboveHeader.frame;
f.origin.y = -1 * f.size.height;
myViewAboveHeader.frame = f;
[tableView addSubview:myViewAboveHeader];
Edit: it seems you don't want a header view AND a view above it. In this case, just simply add a button on top of the view you return in your table view delegate method:
- (UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)s
{
UIView *header = ...;
UIButton *btn = // create a button somehow;
[header addSubview:btn];
return header;
}
Use this code, placing it in viewdidload:
- (void)viewDidLoad {
UIView *headerViews = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
UIButton *managePrivacyButton = [UIButton buttonWithType:UIButtonTypeCustom];
[managePrivacyButton setFrame:CGRectMake(0, 45, 320, 45)];
managePrivacyButton.titleLabel.textAlignment = UITextAlignmentLeft;
[managePrivacyButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
[managePrivacyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[headerViews addSubview:managePrivacyButton];
[self.tableView setTableHeaderView:headerViews];
[super viewDidLoad];
}
This method in particular takes as a return an UIView. UIButton is a subclass of UIView. Just create a new UIButton and return it.
I want to add a table header (not section headers) like in the contacts app for example:
exactly like that - a label beside an image above of the table.
I want the all view be scrollable so I can't place those outside of the table.
How can I do that?
UITableView has a tableHeaderView property. Set that to whatever view you want up there.
Use a new UIView as a container, add a text label and an image view to that new UIView, then set tableHeaderView to the new view.
For example, in a UITableViewController:
-(void)viewDidLoad
{
// ...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:imageView];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;
[imageView release];
[labelView release];
[headerView release];
// ...
}
You can do it pretty easy in Interface Builder. Just create a view with a table and drop another view onto the table. This will become the table header view. Add your labels and image to that view. See the pic below for the view hierarchy.
In Swift:
override func viewDidLoad() {
super.viewDidLoad()
// We set the table view header.
let cellTableViewHeader = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewHeaderCustomCellIdentifier) as! UITableViewCell
cellTableViewHeader.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewHeaderCustomCellIdentifier]!)
self.tableView.tableHeaderView = cellTableViewHeader
// We set the table view footer, just know that it will also remove extra cells from tableview.
let cellTableViewFooter = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewFooterCustomCellIdentifier) as! UITableViewCell
cellTableViewFooter.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewFooterCustomCellIdentifier]!)
self.tableView.tableFooterView = cellTableViewFooter
}
You can also simply create ONLY a UIView in Interface builder and drag & drop the ImageView and UILabel (to make it look like your desired header) and then use that.
Once your UIView looks like the way you want it too, you can programmatically initialize it from the XIB and add to your UITableView. In other words, you dont have to design the ENTIRE table in IB. Just the headerView (this way the header view can be reused in other tables as well)
For example I have a custom UIView for one of my table headers. The view is managed by a xib file called "CustomHeaderView" and it is loaded into the table header using the following code in my UITableViewController subclass:
-(UIView *) customHeaderView {
if (!customHeaderView) {
[[NSBundle mainBundle] loadNibNamed:#"CustomHeaderView" owner:self options:nil];
}
return customHeaderView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the CustomerHeaderView as the tables header view
self.tableView.tableHeaderView = self.customHeaderView;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
headerView.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
headerView.layer.borderColor=[UIColor blackColor].CGColor;
headerView.layer.borderWidth=1.0f;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,100,20)];
headerLabel.textAlignment = NSTextAlignmentRight;
headerLabel.text = #"LeadCode ";
//headerLabel.textColor=[UIColor whiteColor];
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
UILabel *headerLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
headerLabel1.textAlignment = NSTextAlignmentRight;
headerLabel1.text = #"LeadName";
headerLabel.textColor=[UIColor whiteColor];
headerLabel1.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel1];
return headerView;
}
- (void)loadView
{
SettingsTitleBar=[[UINavigationController alloc] initWithRootViewController: self];
searchBar =[ [UISearchBar alloc] initWithFrame:CGRectMake(0, 44, 320, 40)];
searchBar.placeholder = #"Type your City Name";
searchBar.delegate = self;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
[searchBar setShowsCancelButton:YES];
[SettingsTitleBar.navigationBar addSubview:searchBar];
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 85, 320, 392)];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, 320, 302) style: UITableViewStyleGrouped];
[tableView setDelegate:self];
[tableView setDataSource:self];
[self.view addSubview:tableView];
}
I Have a UITableViewController,
I have utilized the first 44 pixels height for title bar, and then the next 40 pixels of height for search bar(44+40). These are added as navigation controller subviews. then i am adding my self.view at 85 pixel from top, finally tableview has been added as child to the self.view . But table view has been overlapped with the Searchbar. I dont what is wrong with it. I tried to change various yPositions of tableview and self.view but still nothing happened.
Can anyone able to help me out from this ?
Note : I dont want to add SearchBar into UITableviewHeader Section.
U can make the view of size 320X436 and add the tabble view and search bar in that view...
I wouldn't add subviews to the navigationBar. Add it to the view.
As a sidenote: anything you alloc (or retain or copy), you should release or autorelease
I had trouble with this too, initially. Don't worry about adding the searchBar as a subview. Simply do this:
self.tableView.tableHeaderView = searchBar;
The searchBar will be treated like a header. You'll want to change the CGRect's size to (0,0,320,44), however.
I want to do something pretty simple with my UITableView: I want to add a UIActivityIndicatorView to a section's header view, and make it animate or disappear whenever I want.
I had no trouble adding the UIActivityIndicatorView to the header view using tableView:viewForHeaderInSection:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];
// create the title
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 12.0, 310.0, 22.0)];
headerLabel.text = #"some random title here";
[customView addSubview:headerLabel];
[headerLabel release];
// Add a UIActivityIndicatorView in section 1
if(section == 1)
{
[activityIndicator startAnimating];
[customView addSubview:activityIndicator];
}
return [customView autorelease];
}
activityIndicator is a property of my controller's class.
I alloc it in the viewDidLoad method:
- (void)viewDidLoad
{
(...)
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(200, 10, 25, 25)];
}
This way I can send messages to it (like -startAnimating or -stopAnimating) whenever I want.
The problem is that the activityIndicator disappears as soon as I scroll the tableView (I guess it is because the tableView:viewForHeaderInSection: method is called a second time).
How else can I add an activityIndicatorView to the section's header view and still be able to send messages to it afterwards? (with the activityIndicator not disapearing when I scroll down of course)
Thank you very much!
If you try to use the same activity indicator in multiple places then it is probably getting moved from one place to the other. I believe you need a different one for every single section header. You might want to use a MutableArray to keep track of the header views you create so you can reuse them if you find one in the array that doesn't have a superview, sort of like dequeuing and reusing cells.
This is just a guess as I haven't done this, but I'm pretty sure the issue is trying to reuse the same view in multiple places.
The problem seemed to be caused by re-creating a customView and adding the activityIndicator as a subview every time tableView:viewForHeaderInSection: is called.
Not using subviews helped me fix it:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// Add a UIActivityIndicatorView in section 1
if(section == 1)
{
[activityIndicator startAnimating];
return activityIndicator;
}
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];
// create the title
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 12.0, 310.0, 22.0)];
headerLabel.text = #"some random title here";
[customView addSubview:headerLabel];
[headerLabel release];
return [customView autorelease];
}
(it looks quite ugly though, the activityIndicator takes the whole width of the section. I'd better create a unique customView for section 1 and add the activityIndicator as a subView once and for all).
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].