Redraw or Reset table view header and/or footer - iphone

Is there a way other than [tableView reloadData] to refresh table header and/or footer? With respect to both:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
OR
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
For example, I'm showing a small summary in the footer. When the data in the row changes, the affect should be reflected in the footer as well.

Create a UILabel and set it as section header/footer. Update the UILabel whenever you want.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return sectionFooterView;
}
- (void)viewDidLoad {
// create sectionFooterView in Interface Builder, change the frame here
// use Font-size:15 , BG-Color: clearColor , text-Color: RGB=(97,105,118)
// and Text-alignment:Center to look like table view's original footer
sectionFooterView.frame = CGRectMake(0, 10, 320, 12);
}
- (void)changeFooter {
sectionFooterView.text = #"Footer";
}

Related

Custom IOS separator

How is the 'or' separator below made? is it an image used in the section header?
if that's really done with a table view, then it's done via a delegate method of "tableView:viewForHeaderInSection:".
Yes, it is an image used for section header.
That login page is modifying the header/footer views of the UITableView.
When you are setting up the table view you need use the following UITableView delegate methods in order to create a header and footer view in each section of your table view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// Set your custom view for the header in the section
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// Set your custom view for the footer in the section
}
- (CGFloat) tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
// Set the height of the header in each section
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
// Set the height of the footer in each section
}
NOTE: If you notice the first two methods return a UIView therefore it does not just have to be an image. You can create any custom view you want and place it in the tableView header/footer. The view could be UIKit elements, UIImage, or even made with CoreGraphics.

UITableView resize header for section does not stretch according to its size

i have a table view which has header for the tableview and another header for section.
the header for section has a button on top of it, when the button is pressed i need to change the size of this header.
i did changed the header size but the content within it does not change accordingly.
i even fixed it using this-
_isHeaderExtended = !_isHeaderExtended;
[self.testTable beginUpdates];
CGPoint point = testTable.contentOffset;
point.y = (_isHeaderExtended)? point.y - 1: point.y + 1;
[testTable setContentOffset:point animated:NO];
[self.testTable endUpdates];
my entire code is below
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 30;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return testView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
[self.testTable beginUpdates];
CGFloat fl = (_isHeaderExtended)?200:100;
[self.testTable endUpdates];
return fl;
}
-(IBAction)buttonPushed:(id)sender {
_isHeaderExtended = !_isHeaderExtended;
[self.testTable beginUpdates];
CGPoint point = testTable.contentOffset;
point.y = (_isHeaderExtended)? point.y - 1: point.y + 1;
[testTable setContentOffset:point animated:NO];
[self.testTable endUpdates];
}
basically i need the content of the header to stretch according to the resize i use,
any ideas ???
I think it should be changed into:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if( _isHeaderExtended ){
return bigTestView;
} else {
return smallTestView;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return _isHeaderExtended?200:100;
}
-(IBAction)buttonPushed:(id)sender {
_isHeaderExtended = !_isHeaderExtended;
[self.testTable reloadData];
}
Then you need to create a bigTestView and a smallTestView for the header.
Do this:
Eliminate the calls to beginUpdate and endUpdate in heightForHeaderInSection. They have no business in the datasource protocol.
Make sure the view you return in the header view is initialized and configured as intended. Presumably you do this in viewDidLoad.
In order to let the content of the header stretch, you can set this view's autoresizingMask to the desired value. Alternatively, you can use IB to configure the views to resize with the container view. You will have to return a properly sized view in viewForHeaderInSection.
A recommended design pattern would be quite different: put all the logic of the size into your datasource methods (heightForHeaderInSection and viewForHeaderInSection), i.e. you could check your _isHeaderExpanded variable. Then, in the toggle method, just reload the section.

Open UITableview or view when the main UITableView cell selected

I have a UITableView which has master records in it's cell. When the user selects a cell, some detail record is displayed on the same table and this record would be set below that particular cell.
One more thing is the cells that are below the selected cell of the master table will be displayed below the detail view.
In short I want to design a popup that will display the details of selected cell below that particular cell and rest of the cells(cells below the selected cell) of master will be moved down, so that the detail popup can be accomodated between the selected cell and the cells below it.
//Take int selectedCellIndex in your .h file, initialize selectedIndex with -1
//Take BOOL isSelected in your .h file
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedCellIndex = indexPath.row;
isSelected = YES;
[yourTable reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==selectedCellIndex)
{
return 100;
}
return 50; //Your default cell size
}
- (UITableViewCell *)tableView:(UITableView *)tV cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//after creating your cell
if(isSelected && selectedIndex>-1)
{
//Show your custom View , something like [cell.contentView addSubView:customView];
isSelected = NO; //Reset
selectedIndex = -1; //Reset
}
}
You can simply use this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method . In here, You can allocate a new UIView every time the user clicks on the cell and display your information in it.
If you have any queries please feel free to reply . :)
Check these projects out, maybe they will of some help ...
Combo Box

Padding on TableViewCell

I want to add padding to my table view cells. Thought I could do something like this but that didnt work.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height + 20;
}
See the below link. Explains the basics of getting a variable height (for cells) UITableView along with proper padding that your require. Example just includes text. Change it to suit your requirements within the cell.
http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/
Are you sure you placed this method in your delegate? it also depends on which cells. There are other methods...
// UITableViewDelegate Method
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
}
// UITableViewDelegate Method
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
}
also, if you are using the contentView of the cell then you will need to change the frame of your custom view
When adding your cells you will need to set the y value of the cell's frame to half your padding to make it start at the right place. Also ensure that the cell does not have flexible height or it will expand to fill the height of the row.

Table HeaderView iPhone

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.