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.
Related
I have UITableView that contains 4 different types of customized cells in storyboard. Each cell has customized UILabels which get variable amount of text data from backend. I am struggling with making the cells resizing correctly. I would really want to change the height of each cell but I can not use heightForRowAtIndexPath because it is called before cellForRowAtIndexPath, but the height is actually calculated within each customized cell.
I tried writing in each cells' height into an array while the UITableView is loading, then just reloading it all over again once, but no effect. I tried using CGFloat rowHeight = UITableViewAutomaticDimension with no success either. The customized labels in each cell definitely grow with text which I see when I just statically change row height to higher numbers. So, I would need somehow my labels to push on rows to make them grow, not sure.
Different similar posts on stackoverflow that I found did not help.
The issue was that I needed to set up top and bottom constraints to the ContentView and NOT to the cell itself in the storyboard.
Label -> ContentView top and bottom constraints need to be set up. And then UITableViewAutomaticDimension specified in viewDidLoad:
self.tableView.rowHeight = UITableViewAutomaticDimension;
estimatedRowHeight should be set too. For example:
self.tableView.estimatedRowHeight = 76.0f;
First Method called is:
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
Second:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Then:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
set a break point in the above methods and test it. So if you want to preset the height use estimatedHeightForRowAtIndexPathmethod.
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;
In my tableview, I have one section, and in that section there are two rows.
The first row contains the name of an image and the second row contains the actual image. When inserting the image, I want to resize the cell based on size of that image. I tried out with cell frame and size, but it's not working.
What can I do to solve this problem?
The cells are laid out by the UITableView, you can't resize the cells directly.
You have to implement - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath in your UITableViewDelegate
this is my grouped tableview:
Is it possible to avoid the spacing between sections and display the grouped table as if there is no sections?
Thanks in advance, yassa
Probably not what you want to hear, but no. No nice way to do this. I Think you are able to minimize the Space between the Groups but this will look very bad.
Another way would be to use one Section, but then you wont have this nice little Letters at the right side.
But if you want no Space between the Cells why dont you use UITableViewPlainStyle?
It appears that you don't have any content for header or footer in your table view, so I think you could achieve that by implementing table view's delegate methods :
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
Those two methods might work correctly if you also implement :
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
returning nil for each.
In iOS 7 I customized space between sections in grouped table view by implementing next method:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10; // there might be your height
}
To make this method work prior to iOS 5, you should also return non-nil view in tableView:viewForFooterInSection:.
Prior to iOS 5.0, table views would automatically resize the heights
of footers to 0 for sections where tableView:viewForFooterInSection:
returned a nil view. In iOS 5.0 and later, you must return the actual
height for each section footer in this method.
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.