I am making an iPad app where in I have a grouped TableView with headers for the sections.
i want to add four label in header of tableview
text of four label's are "company", "current", "prev close" ,"change"
or
instead of adding four labels in header, i want to add four text "company", "current", "prev close" ,"change"
What should I do?
Please Help and Suggest.
Thanks.
There is a delegate method of UITableView for custom view of Header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)];
UILabel *objLabel = [[UILabel alloc] initWiThFrame:CGRectMake(0,0,320,40)];
objLabel.text = #"Post";
[headerView addSubview:objLabel]; //Similarly You can Add any UI Component on header
[objLabel release];
return [headerView autorelease];
}
If you want to show the Company, Current, prev next as text in section title, then
just append the text in a single string and return this value in the following delegate method.
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
Hope this helps.
you can implement - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section and you can return what ever view you wanted for each header
Related
I have a created custom UIButton in UITableView viewForHeaderInSection. Now when i click on the button within the section how will i know which section is clicked? Please help.
when you add button in viewForHeaderInSection , just set tag with section number like bellow.,..
button.tag = section;
and when your button clicked at that time you can get that number with bellow code...
- (IBAction)yourButton_Clicked:(id) sender
{
NSLog(#"Section Number => %d",[sender tag]);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIbutton *btn =[[UIButton alloc]init];
btn.tag=section;// this tag property will differentiate button for different section
}
by this you can access btn and add event on that burron
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIbutton *button =[[UIButton alloc]init];
button.tag=section;// this tag property will differentiate button for different section
}
In didselectrowatindexpath check for if(index path.section) is equal to the section you want the action.
I have a UITableView that needs to have something inserted at the top in some cases (not always).
The code (if it were to be a NORMAL) cell would be as follows
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,98,100)];
imgView.image = [UIImage imageNamed:#"image.png"];
cell.imageView.image = imgView.image;
//sets cell text
cell.textLabel.text = #"Number";
but I was told on here that it would be easier to implement a headerView than to change the indexPath each time.
So my question is how would I implement this? Would I use - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section or would I use - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section? Keep in mind, I have an image to place into the cell (headerView).
Thanks
You would use
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
titleForHeaderInSection: only sets the header as text, where as viewForHeaderInSection: will allow you to add a view, UIImageView, etc.
UIImageView is a subclass of UIView, so it can be returned from
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
This is what you should do since it allows full customization of the header image.
If you instead use
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
then the only thing you have control over is the NSString that you return. You cannot add a background, change the font color, etc. Since you have an image, use the former method.
i am very much new to iphone development ,I wanted to reduce the size of a section in the grouped table view i.e reducing the width of the section . how can it be implemented
thanks in advance
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return kHeightForHeaderInSection;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// wrapperView - by default has the width of the table, you can change the height
// with the heightForHeaderInSection method
UIView *aView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
// your real section view: a label, a uiview, whatever
CGRect myFrame; // create your own frame
myFrame.origin = CGPointMake(10, 0);
myFrame.size = CGSizeMake(tableView.bounds.size.width-10,kHeightForHeaderInSection);
UILabel *label = [[[UILabel alloc] initWithFrame:myFrame] autorelease];
label.text = #"myText"
[aView addSubview:label];
return aView;
}
Hi add this UITableviewdelegate method
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return height;
}
its a very simple soln ...
#interface MyController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableview *myTableview;
just right click on "UITableViewDelegate" and "UITableViewDataSource"
n implement all the delegates....u will find all types of adjustment over there....weather u want to change or adjust height or width of section or row ...or how many section or how many rows in a section do u want....
its simple n called automatically.... regards
Just came across some very strange behavior in my app. I've recreated the problem in a simplest-case scenario:
NSMutableArray *data;
- (void)viewDidLoad {
[super viewDidLoad];
data = [[NSMutableArray arrayWithObjects:#"1", #"2", #"3", nil] retain];
}
- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section {
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 32.0)];
header.backgroundColor = [UIColor lightGrayColor];
[header addSubview:self.button];
return header;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[data removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
cell.textLabel.text = [data objectAtIndex:indexPath.row];
return cell;
}
- (void)dealloc {
[super dealloc];
}
Every time I delete a row; the button in my header disappears! This happens no matter what type of rowAnimation I use. If I scroll the table up so that the header scrolls off; the button returns when the header returns. The button is created in the xib file.
I can work around it in one of 2 ways:
Reloading the tableView data after the delete; with a delay so that the deletion animation completes first.
Creating the button in viewForHeaderInSection instead of in the interfaceBuilder.
I'd really like to understand what's going on here. Where is the button going? I've confirmed that viewForHeaderInSection is called when I delete a row.
Edit I tried changing it so that the button is created in viewForHeader, instead of in the xib, but it's causing other strange issues... when I create or delete the button, I am setting certain properties such as the title and enabled depending on how many items there are in the table. When I delete the last row in the table, I don't see the update in text and enabled status until I scroll the button off the screen and back on again.
Because you only have one instance of your button, if the table view decides to create a new header view then the button will be removed from its current parent and moved to the new one. Even if you only have one section in your table, the table view may be doing some strange things internally and recreating header views off-screen so you can't rely on just one being in existence at any one time.
You should create the button in viewForHeaderInSection: and work around your other problems. Rather than only updating the button properties in viewForHeaderInSection you should handle any delete events so that deleting a row will also update the button.
Where is your implementation of the delegate method tableView:heightForHeaderInSection: ? That is necessary for tableView:viewForHeaderInSection: to work correctly. Check the docs.
Reference for UITableView delegate
I've confirmed that
viewForHeaderInSection is called when
I delete a row.
Have you confirmed that viewForHeaderInSection is called for the particular header with the added button?
Then, try adding
[header bringSubviewToFront:self.button];
after adding the button.
Well I at least managed to get around my issue... I made an iVar and property for the view that I create in viewForheaderAtSection, and then I only create a new view if I don't have one already. Otherwise I just return the header I already had; something like this:
- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section {
if (!self.myHeader){
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 32.0)];
header.backgroundColor = [UIColor lightGrayColor];
[header addSubview:self.button];
self.myHeader = header;
[header release];
}
return self.myHeader;
}
This works, but it would still be great to understand what exactly is going on. As far as I can tell, viewForHeaderInSection is being called by the system, but then the instance of the view that I return in that method is not actually being used / shown; at least not until I do something that causes the view to redraw...
I have a table view wherein the number cells are not fixed and will keep on changing. I have to show two action buttons after my last cell in the table footer. How can I place them properly after my last cell? I know the pixel spacing between last cell and these buttons. Any code sample will be really helpful.
Have you tried sticking them in a view and setting that as the footer view?
You need to give it the correct height before you add it; the width is automatically set to the width of the table. Either set it to a sensible width (e.g. 320) and use autoresizing or use a custom UIView subclass and implement -layoutSubviews.
You could always add two buttons to the final cell.
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
return [myCellDataArray count]+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.row > [myCellDataArray count]) {
cell = [tableView dequeueReusableCellWithIdentifier:#"Button Cell"];
if (cell == nil) {
UIButton *firstButton = [[UIButton alloc] init];
//customization
UIButton *secondButton = [[UIButton alloc] init];
//customization
[cell addSubView:firstButton];
[cell addSubView:firstButton];
}
} else {
// normal stuff
}
If you want to customize existing buttons you need to set it's tag to something unique, i.e. firstButton.tag = 100 and then set firstButton by firstButton = (UIButton *)[cell viewWithTag:100];. Make sure you define firstButton so that it's in scope!
Hope this helps!