How to set color in empty UITableViewCell? - ios

I want to replicate Gowalla UI of the detail part (the table with Map & Directions) picture below
I replicate it with a table with a contentInset, set backgroundColor to clearColor, and change color of UITableViewCell in - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath to white, but the result
isn't what I expected the empty cells are white only the cells with content (a number return by - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section ) here is a result I got
Is there any way to set empty cell color or any guide on how to achieve this ?

Set background color of view same as you are setting for your cell. and make cell transparent.

Due to no response for long time I would posted my solution here. I solved this problem by having UITableView under UIView and set parent view background color the same as UITableViewCell color.

Related

Conditional background color for UITableViewCell

I'm trying to set the background color for a subclassed UITableViewCell. I know that normally I'm supposed to use tableView:willDisplayCell:forRowAtIndexPath:. However, I have some logic to customize several aspects of the UITableViewCell in tableView:cellForRowAtIndexPath:, and I would like to set the background color of the cell based on that logic. Do I have to duplicate the whole set of conditions I have in tableView:cellForRowAtIndexPath: in tableView:willDisplayCell:forRowAtIndexPath: just to set the background color? That kind of code repetition seems wasteful and probably unwise.
The best solution I can think of is to add a UIColor property to the UITableViewCell subclass, set it in tableView:cellForRowAtIndexPath: based on the conditions there, and just have tableView:willDisplayCell:forRowAtIndexPath: set the cell's background color to its own color property. Is this the right way to handle this situation? It seems kinda clunky, but I can't think of anything better.
You can create function returning appropriate color and set it accordingly like
-(UIColor*)getColorForIndexPath:(NSIndexPath*){
UIColor *cellColor=nil;
//Do whatever your logic is for finding appropriate color assign to cellColor
return cellColor;
}
Now use this function to setcolor as, so you have to modify only 1 place to effect at both place.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
.... //Your code for cell
cell.backgroundColor = [self getColorForIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
.... //Your code for cell
cell.backgroundColor = [self getColorForIndexPath:indexPath];
}
Note : This will still work if you place only at any of the delegate method, so no need to call at 2 places.

How can I resize a UITableView without resizing a View inside it

I have a Tableview that has a UIView and a label inside each row, and this UITableView resize its rows using
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Well, my problem is that when I resize the cell, its view is resized automatically too. How can I avoid resizing the view inside the cell?
The property autoresizesSubviews of your cells prevents any subviews within your cells to resize automatically. By setting it at NO, it should fix your problem.
cell.autoresizingMask = NO;
However, I would suggest using the method below when playing with the size of cells.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Good luck
You should set a proper autoresizing mask or autolayouts rules of the cell subviews, to obtain the layout you want for your cells
you can set the label's contentMode.
for example:
UILabel *lb...(create)
lb.contentMode=UIViewContentModeScaleToFill (change that ,)
cell addSubView:lb;

How to change cell content on highlight

In my TableView i load cells based on a xib file. The cell has a imageView in the background. How to change the image when user holds finger on a row? I dont meen didSelectRowAtIndexPath, cause it means the user tapped the row.
Implement the - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated method on your cell subclass and change the image there based on the value of the highlighted parameter.
Hope this help
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
// update your image here
return YES;
}

UITableView - custom unused cells

If my table view is 480 in height for example, and for some reason I only end up with 2 or 3 cells, the rest below are displayed in the typical white with the light gray dividers. How can I fill those unused cells with a custom image? I know I can change the background color of the table, but I actually want to fill each unused cell with an image instead.
You will need to do exactly what you said: fill each unused cell with an image.
basically you'll need to do your logic in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
to calculate how many "fake cells" you'll need and then return them in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You can disable the dividers (set the table's cell divider style to none) and set the background to transparent, then add a UIImageView with your background to the background.
If you really want an image per cell, then you will need to calculate the number of extra cells needed and return them from numberOfRowsInSection, then render the cells in cellForRowAtIndexPath.

How set the background Image for a section in UITableview?

Hav a option to set background image or color to cell.
How can I suppose to do that for a section with number of rows and each row in the section will be in different color..
You can do the same by using delegate method of UITableView,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
In this method, you can set individual UIView according to section. Moreover, you can set individual section's height by overriding...
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

Resources