Reduced cell in grouped UITableview - ios

I am adding one more section to the existing tableView and getting this:
my new cell is reduced by height. Appropriate methods:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cells[indexPath.section][indexPath.row];
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ([headers[section] isKindOfClass:[UIView class]])
return [headers[section] frame].size.height;
return 10.0f;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if ([headers[section] isKindOfClass:[UIView class]])
return headers[section];
return nil;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = cells[indexPath.section][indexPath.row];
if (cell == clientXibCell) return 100.0f;
if (cell == agencyXibCell) return 145.0f;
return 46.0f;
}
I can't understand what I need to do to fix this. Any ideas where the source of issue can be?
Update
I am now sure that predefining custom cell visual interface making this trouble.
supervisorCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
bgView = [[UIImageView alloc] initWithFrame:supervisorCell.backgroundView.frame];
[bgView setImage:stretchableImageByHorizontal([UIImage imageNamed:#"cell_bgd_bottom"])];
[supervisorCell setBackgroundView:bgView];
bgView = [[UIImageView alloc] initWithFrame:supervisorCell.backgroundView.frame];
[bgView setImage:stretchableImageByHorizontal([UIImage imageNamed:#"cell_bgd_bottom_active"])];
[supervisorCell setSelectedBackgroundView:bgView];
When I am uncommenting everything except first statement of creating the cell, everything works fine except custom appearance of cell. What do I need to change in this simple code to fix this?

The height of your cells are controlled by the heightForRowAtIndexPath:. Taking a look on your code, it seems that this method is always returning 46.
Your two ifs are comparing pointers, i. e., instancies of your cells. It means that of all your cells, one will have the height 100, one 145 and all others 46.f.
I think what are you trying to accomplish is set this height for all cells of the same kind, so you should change your heightForRowAtIndexPath: method, like below:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ( [cell isKindOfClass:[YourCustomCell1 class]] ) return 100.0f;
if ( [cell isKindOfClass:[YourCustomCell2 class]] ) return 145.0f;
return 46.0f;
}
Ps1: Change YourCustomCell class for your own classes. If you don't have subclasses, try to set tags or something like that to differentiate them.
Ps2: always use tableview's method cellForRowAtIndexPath to get the reference of the cell by the indexPath.

Related

How to Set a Space between Tableview Cell in iOS?

I am newbie in iOS development and i know this question is asked many times but i confuse for it. I want to know how to set a space between UITableview cell in section.In my app UITableview contain two section first section contain only one data value so not any problem. but my second section contain 5 to 7 data value But not a space between them how to set a space in footer between second section cell in UITableview.
OK so you have two methods for sections
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
but neither serve the purpose of putting space between cells in a single section.
For my money there are two options... first and most complicated / hacky
in - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView you could return the count of the cells in the second section and in cellForRowAtIndexPath if (section == 0) put the data in the usual way... else you can pull the info out of the array or whatever using [theArray objectAtIndex:indexPath.section] instead.
(much simpler, and better practice).. Create a UITableViewCell subclass and use that in your cellForRowAtIndexPath like so
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *MyCellIdentifier = #"MyCustomCell";
switch (indexPath.section) {
case 0:
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// setup your 1st section cells here
return cell;
}
default:
{
MYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
if (!cell)
cell = [[MYTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];
// all other layout code here if indexPath.row == 0 etc.
return cell;
}
}
in your custom cell .m you can set
-(void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,0,0); // lay them out at the top / middle / wherever in your cell
self.textLabel.frame = CGRectMake(0,0,0,0); // which will allow for some space at the bottom / edges
}
then finally use
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
return THE_CELL_HEIGHT;
else
return THE_CELL_HEIGHT + PADDING;
}
This way you can set padding in the cell itself, which is cleaner and reusable. If you want different colours to mark the spacing, you should create UIViews in the custom UITableViewCell subclass method
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
and add them using [self.contentView addSubView:someView]; you can always set the backgroundColor of a cell to [UIColor clearColor]; if you have an image / content behind the tableView

How to discover why heightForRowAtIndexPath is crashing

I am not a full-time iOS dev and I'm trying to return the cell at the current indexPath but this is crashing and I am not sure exactly why. I maintain the height dynamically on the cell. My cell is a custom UITableViewCell called MICell and I have a property called height which is dynamically calculated in the updateCell method.
#import "MICell.h"
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MICell *cell = (MICell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
// hardcoded to 200.0f but still crashing
NSLog(#"here is my height; %f",cell.height);
return 200.0f;
}
Edit 1
As a workaround, I am able to make an instance variable and just write the current cell height which is then accessed in heightForRowAtIndexPath, like the following. I do feel that this is potentially making assumptions that I'm not fully comfortable with:
#interface ListViewController (){
NSMutableArray *_data;
CGFloat _currentCellHeight;
}
...
-(MICell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MICell";
MICell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
// Configure the cell here ...
NSDictionary *tmp=[_data objectAtIndex:indexPath.row];
[cell updateCell:tmp];
_currentCellHeight=cell.height;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return _currentCellHeight;
}
Edit 2
Here's a reference to this exact technique. How to get the cell object in tableView:(UITableView *)tableView heightForRowAtIndexPath function?
Edit #3
The above code works fine - there was an issue with IB connections.
It's because you're causing an infinite cycle. You cannot call:
[self tableView:tableView cellForRowAtIndexPath:indexPath];
In height for row at index path.
[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
will cause issues when called from height for row at index path. You must call tableView dequeueReusableCellWithIdentifier: without the forIndexPath. You should probably create a configureWithCell:(YourCellClass *)cell method separate from these so you can just configure your cell after properly dequeueing/allocating it.
Also, you do not grab the height from the cell as you're doing above (unless you're assigning a height property with some weird forwarding stuff that breaks class containment... which you shouldn't do either). Use the following for dynamic cell heights via auto layout:
CGFloat cellHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
Something like the following below should do the trick:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCellClass *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyCellClass class]) forIndexPath:indexPath];
[self configureMyCell:cell forIndexPath:indexPath];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCellClass * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyCellClass class])];
[self configureMyCell:cell forIndexPath:indexPath];
CGFloat cellHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return cellHeight;
}
In ios, the height of the cell is calculated in heightForRowAtIndexPath. What you are trying to do is to get height of the cell inside heightForRowAtIndexPath. It is definitely not the way to do it. To have dynamic height, calculate the height inside heightForRowAtIndexPath using the features/values you might need and return that height.
Bottom line is you cannot call cell.height inside heightForRowAtIndexPath.
Edit 1:
To help you deal with the dynamic heights in cell, please look at this tutorial. It might help you understand the issue : Dynamic Cell
Just go straight to the tableView delegates method to get an understanding of how to proceed.

How to hide a static UITableViewCell?

How do you hide a static cell?
I would like to hide and static cell if an image does not exist.
I tried:
imageCell.hidden = YES; //did not work
I have seen answers suggesting to change datasource or use:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;// will hide all cell
}
But I couldnt find a way to do this with a specific view cell.
what I want to achieve:
if(image==nil){
//hide imageCell
}
Now here is the catch , the image is downloaded asynchronously, so deleguate methods might be called before the attempted downlaod.
Do the following :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 2 && !myImageIsLoaded)
return 0; // Will hide just the third row of your table if myImageIsLoaded is false
return 44;
}
And you can use the following to animate all whenever you want (e.g. each time an image as loaded) :
[myTable beginUpdate];
[myTable endUpdate];
If your cells are static so it should work. Otherwise you could encounter some problems.
If you want literally hide the cell that image does not exist, you can try this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
return [cell imageView] ? 44.0f : 0.0f;
}

How to set custom background color of UITableViewCell even there's no data inside

I want to accomplish something like this :
see there's only one data but, background color continue until end.
I understand I can do inside tableview delegate of tableView:willDisplayCell:forRowAtIndexPath:. but then it doesn't go to empty cell, hence my empty cell always be white.
I used the following code to display cell alternative color even if cell is not initialized.I have done this work on scrollViewDidScroll as showing below:--
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
UIView *view=[[UIView alloc] initWithFrame:tblView.frame];
view.backgroundColor=[UIColor greenColor];
UIView *cellView;
int y=0;
int i=0;
for (UIView *view in tblView.subviews) {
if ([NSStringFromClass([view class]) isEqualToString:#"_UITableViewSeparatorView"]) {
cellView=[[UIView alloc] initWithFrame:CGRectMake(0, y, 320, 44)];
if (i%2==0) {
cellView.backgroundColor=[UIColor redColor];
}else{
cellView.backgroundColor=[UIColor greenColor];
}
[view addSubview:cellView];
i++;
}
}
tblView.backgroundView=view;
}
And got the correct result on scrolling table view. But the problem is it works when user scrolls the tableView atleast once a time.
If you will get success to fire event on tableView completes its reloading.Then it will be fine.
Here is output I got on scrolling tableView.
I also write this method to call didScrollMethod manually but doesn't seems to work perfectly.
[tblView.delegate scrollViewDidScroll:(UIScrollView*)tblView.superclass];
But calling method like code below absolutely works fine.
- (void)viewDidLoad
{
tblView=[[MyFirstView alloc] init];
tblView.delegate=self;
[tblView setFrame:self.view.frame];
[self.view addSubview:tblView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
[tblView.delegate scrollViewDidScroll:(UIScrollView*)tblView.superclass];
}
Means after loading tableView in viewDidLoad call didScroll in viewDidAppear works fine.
Insert below code if fluctuates first row while scrolling.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[[UIView alloc] init];
return view;
}
You have to set the backgroundColor to the contentView of a UITableViewCell.
Sample as below:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
if (cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"identifier"]autorelease];
cell.contentView.backgroundColor= [UIColor greenColor];
}
return cell;
}
To have alternate colors in your cells of tableView, you can do the following;
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
if (cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"identifier"]autorelease];
}
if(indexPath.row % 2)
{
cell.contentView.backgroundColor= [UIColor greenColor];
}
else
{
cell.contentView.backgroundColor= [UIColor yellowColor];
}
return cell;
}
A table with plain style doesn't show rows below the last row so there is no way to produce the desired effect using table view cells. About your only option would be to create a view with the alternating pattern and make the view the table view's footer view.
This view would need to deal with being updated as the number of actual rows in the table changes to/from odd and even. And you need to make it tall enough so if the user scrolls the table up a bunch, the footer still reaches the bottom of the screen.
You can setup some placeholder cells in addition to your cell with 'Monthly meeting', something like:
return amount of rows as 1 + (rows to fill screen) in the tableView:numberOfRowsInSection:
In the tableView:cellForRowAtIndexPath: - check for index path of the cell, if its row = 0, then this is your action cell, otherwise, update cells background, do the same in the tableView:willDisplayCell:forRowAtIndexPath:. Make sure to remove selectionStyle for your placeholder cells.
Or, you can use 2 cells - first one - again, your 'Monthly meeting' cell, and second one - a cell with height enough to cover screen from first cell to the bottom with image of striped cells.
This is simple to do. Just have as many items in your data source array as you want to see rows, and have all but the first one be empty strings. In willDisplayCell:forRowAtIndexPath: apply a background color to all the odd numbered cells.
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = #[#"Monthly Meeting",#"",#"",#"",#"",#"",#"",#"",#"",#""];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.theData.count;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 1) {
cell.backgroundColor = [UIColor colorWithRed:232/255.0 green:238/255.0 blue:222/255.0 alpha:1];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.row];
return cell;
}
Set backgroundColor to the contentView of a UITableViewCell with the help of simple mathematics, Example:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
if (cell==nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"identifier"]autorelease];
if (i%2==0) {
cell.contentView.backgroundColor= [UIColor greenColor];
}else{
cell.contentView.backgroundColor= [UIColor redColor];
}
}
return cell;
}

UITableview grouped style only showing one value from array?

MY UITableview Group style only showing one value from the array of 4 elements.
in viewdidload i add following array
_displayThemeItems=[[NSArray alloc]initWithObjects:#"Default",#"Night",#"Sepia" ,#"Blue" ,nil];
_displayThemeIcons=[[NSArray alloc]initWithObjects:[UIImage imageNamed:#"day.png"],[UIImage imageNamed:#"night.png"],[UIImage imageNamed:#"sepia.png"], [UIImage imageNamed:#"blue.png"],nil];
and my table delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_displayThemeItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// No cell available - create one.
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];}
NSLog(#"rray%#",_displayThemeItems);
cell.textLabel.text = [_displayThemeItems objectAtIndex:indexPath.row];
cell.imageView.image=[_displayThemeIcons objectAtIndex:indexPath.row];
return cell;
}
Your code look fine to me, the only the only reason that I can think amy effect the result you see is how you set up your table view.
Make sure that your table view frame is bug enough to show the 4 items, and make sure you set the tabe view data source.
Add the "}" just before the return such that the code where you set the title and image comes under the "if" statement.

Resources