Height of tableHeaderView seems to be 0 - iphone

I tried to override the second tableHeaderView. But it seems that the height of it in the method heightForHeaderInSection seems to be 0. Can't explain it, do I have to put it in a iVar because in the viewForHeaderInSection I can set the view without any problems.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0)
return #"Adding a new list";
else
return #"Other lists";
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(section == 0) {
return tableView.tableHeaderView;
} else {
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)] autorelease];
view.backgroundColor = [UIColor redColor];
return view;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 0)
return tableView.tableHeaderView.frame.size.height;
else
return 30;
}

I think you are confusing the table header and the header for each section, they are different. There is no "second tableHeaderView", a UITableView has one tableHeaderView and then depends on the viewForHeaderInSection and heightForHeaderInSection methods to place custom header views for each section, otherwise you just use titleForHeaderInSection to place text there.

Related

dynamically increase height of row -iphone

I am having table view.
I want to dynamically increase the height of the row to some size.
How can I do this.
I know its a silly question.But still can't find the solution.Help me please.
I have used this code.But it is not working :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *lblOutTime = [[UILabel alloc]init];
lblOutTime = [[UILabel alloc] initWithFrame:CGRectMake(2, 20, 200, 40)];
lblOutTime.text =#"Outtime : ";
lblOutTime.backgroundColor = [UIColor clearColor];
lblOutTime.font = [UIFont boldSystemFontOfSize:5.0f];
lblOutTime.font = [UIFont fontWithName:#"HelveticaNeue Heavy" size:5.0f];
[cell.contentView insertSubview:lblOutTime atIndex:1];
return cell;
}
-
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == myIndexPath)
{
return someHeight;
}
return normalHeight;
}
In this case the height is not dynamic. here
is only 2 fixed positions of cell height.
as the if conditions shows.
Here you can change the size of the cell:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.myRow == myCellIndex)
return NEW_HEIGHT;
else return NORMAL_HEIGHT;
}
And you can reload your table whenever you want with:
[myTable reloadData];
add this tableview delegate method to your class.m file.
Make sure your class conforms to UITtableViewDelegate protocol
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == myIndexPath)
{
return someHeight;
}
return normalHeight;
}
If You Want Increase the height of all row use
[tableView setRowHeight:44];
[tableView reloadData];
if You want increase the height of a particular row use UITableView Delegete heightForRowAtIndexPath:
and use this
[tableView reloadData];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row <[ListOfCartItems count])
{
CGFloat result = 44.0f;
NSString* text = nil;
CGFloat width =220 ;
clsCart *tempCartPro;
tempCartPro=[ListOfCartItems objectAtIndex:indexPath.row];
text=tempCartPro.clsProductItem.productName;
if (text)
{
CGSize textSize = { width, 20000.0f }; // width and height of text area
CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 90.0f; // top and bottom margin
result = MAX(size.height, result) ; // at least one row
}
return result;
}
else
{
return 230.0;
}
}

reducing space between cells in UITableView - why won't this code do this?

any ideas why I can't reduce/remove the space between cells in a Grouped UITableView (one cell per section). I'm including the following in the controller:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.0;
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.0;
}
I'm still getting spaces between cells even with this code. Put a border around the cells and the space isn't from the cells themselves.
As per PengOne's suggestion to look at the answer here, what I didn't have in place that made it work was the following:
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}

UITableView Random White line in section footer when returning properly sized footer

I have a UITableView inside of a View created from a NIB. the table has one section containing 6 rows and 1 footer. when i use:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 80;
}
to return the height of the footer, which is being loaded from the same NIB. the UITableViewCell in the nib has a height of 80. the UITableViewCells are assigned using this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0){
if (indexPath.row == 0){
return companyCell;
}
if (indexPath.row == 1){
return firstNameCell;
}
if (indexPath.row == 2){
return lastNameCell;
}
if (indexPath.row == 3){
return emailCell;
}
if (indexPath.row == 4){
return passwordCell;
}
if (indexPath.row == 5){
return passwordConfirmCell;
}
}
return passwordConfirmCell;
}
i get a white line at the bottom of my table like the image at http://swnsn.com/white.png
if i return a footer height of 79, or 81, the white line goes away and the UITableViewCell scales to the width of the window
any thoughts?
I figured it out!! I was passing an instance of UITableViewCell to the footer. i have update my code to only pass a UIView and the problem has been resolved!
Have you tried more row manipulation like this in terms of it's height?
- (CGFloat)tableView:(UITableView *) theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
Question *question = [self.questions objectAtIndex:row];
NSString *theText = question.description;
UIFont *font = [UIFont fontWithName:#"Helvetica" size:14.0]; // You can choose a different font ofcourse.
int heightExtra = 0;
if (selectedIndexSegment == 0){
heightExtra = 54;
}
if (selectedIndexSegment == 1) {
heightExtra = 24;
}
CGSize withinsize = CGSizeMake(theTableView.frame.size.width -60, 1000); //1000 is just a large number. could be 500 as well
// You can choose a different Wrap Mode of your choice
CGSize sz = [theText sizeWithFont:font constrainedToSize:withinsize lineBreakMode:UILineBreakModeWordWrap];
return sz.height + heightExtra; // padding space of 20 should be fine!
}
And also how about
self.tableView.separatorColor = [UIColor clearColor];
Hope it helps...

Frame of viewForHeaderInSection is always the same size

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section != 0) {
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
view.backgroundColor = [UIColor redColor];
return view;
} else {
return tableView.tableHeaderView;
}
}
This is my implementation of viewForHeaderInSection but whatever frame I make it's always showing me the same red frame. Do you see any problem with my code?
Image:
UPDATE:
Mhm now my red block is higher but my first tableHeader is now somehow hidden. The first one was implemented with the titleForHeaderInSection. I thought I just implement the height of the tableHeader height but that doesnt work
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 1)
return 30;
else
return tableView.tableHeaderView.frame.size.height;
}
You need to implement this delegate method
- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section;
In your case, you can simply return 30;.
Also, you are leaking view!
Your [view release] happens after the return. But as soon as the return happens the method execution is aborted and your release is never called.
So you want this instead
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
And get rid of the explicit release down below.

UITableView's background color becoming white when section is empty

Here is the deal: I have a UITableView with 2 sections, and I want to display a "no data" cell when the first section is empty, so that the 2 section headers are not stuck together (cause it looks weird).
It works great (even though I had trouble making it work at first, see this thread). I'm using viewForFooterInSection :
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if(section == 0)
{
if([firstSectionArray count] == 0)
return 40;
else
return 0;
}
return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
if(section == 0)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 44)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
label.textAlignment = UITextAlignmentCenter;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
label.text = #"No row";
return [label autorelease];
}
return nil;
}
But the background color turns plain white when I display the section footer view. See image:
alt text http://img683.yfrog.com/img683/9480/uitableviewproblem.png
I like it better when the background is filled with empty cells. Does anyone have any idea how to do that? Thanks
The background is filled with empty cells when you have no footer. So do not implement a viewForFooterInSection (or titleForFooterInSection) method, and you will get the "empty cells" effect.
I'd recommend that you return a cell indicating that there are no entries to show, like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (matches.count>0) {
// Do your usual thing here
} else {
static NSString *cellId = #"noDataCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor grayColor];
}
cell.textLabel.text = #"Aucun match";
return cell;
}
}
And of course, you would have to tell UIKit that you always have at least one cell in your section... I've added the isDeletingRow case that seems to trouble you (in comment).
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0) return matches.count>0 ? matches.count : (isDeletingRow ? 0 : 1);
// Set 'isDeletingRow' to YES when a delete is being committed to the table, in that case we let UIKit know that we indeed took care of the delete...
// And cover the other sections too...
}
When you are committing the edits, you need to set isDeletingRow for numberOfRowsInSection to return a satisfactory value...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
isDeletingRow = YES;
// Your current code when a row is deleted here...
isDeletingRow = NO;
if (matches.count==0) [self.tableView performSelector:#selector(reloadData) withObject:nil afterDelay:0.5];
}
}