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.
Related
i have an UITableView which includes a list of UITableViewCells. And I set the heights of UITableViewCells.
Your UITableViewDelegate should implement tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row] * 20;// or whatever you need to set height
}
or
If all the cells are the same, set the rowHeight property on UITableView to the size of your cell. If they're all different based on content, you're going to have to implement -tableView:heightForRowAtIndexPath: and calculate the height for each row based on your data source.
Trying to understand when the cell height is set.
I have a UITableView that is created in layout, not code.
Then:
#define TABLE_VIEW_ROW_HEIGHT 100.0
Followed by:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Setting row height");
return TABLE_VIEW_ROW_HEIGHT;
}
and
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row height: %f", tableView.rowHeight);
// ... other happy stuff that does not change row height but could sure use the real row height
The last NSLog reports 44 rather than 100. Which means that the code within that method that needs to get the row height is not using the real height.
I've verified that
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
runs before
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
And, as a result, the rows are of the desired size, not the default size of 44.
Do I need to explicitly set the height somewhere else?
EDIT AFTER DOING SOME RESEARCH:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
...is more appropriately used to set the height of rows when not all rows are of the same height. I was wrongly using this to set all rows to the same height.
The right way to do it is to call
the_tableview.rowHeight = TABLE_VIEW_ROW_HEIGHT;
...from somewhere like viewDidLoad. I actually did that (code omitted) but it didn't work. Tonight I realized that this is yet another instance of being had by the graphical layout tool...I forgot to connect the IBOutlet for the UITableView...so, my code did nothing and the entire tableview remained at the default height of 44. That's why I was getting 44 out of my NSLog.
In your code, you are calling the rowHeight on tableView and not on a specific row at IndexPath. That is the reason why you are getting the default value of 44. If you want to change the default value then you can do it in size inspector for the table. Changing the default value is better if all the rows have the same height. If there are rows with different heights then you will have to implement heightForRowAtIndexPath for that the rows with different heights.
You have defined TABLE_VIEW_ROW_HEIGHT in code then in this case you will also have to set the tableView.rowHeight in code by assigning it the defined TABLE_VIEW_ROW_HEIGHT name.
Here I'm using the default UITableViewCell & the thing I don't like is that the detailedText is too close to the textLabel so I wanted to add some spacing between them, but I cannot find how to do that. Any suggestsions?
Override this below delegate method.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Is it possible to increase the size of particular cell in the TableView.
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
return 100;
}
By using this we can increase the size of the table cell for a tableView but it will increase all the tableCell size.
But I want to increase the height of first cell is up-to 130 and other cell are of height 100. Can any one suggest me the best way for doing this...
Make height value dependent on cell's position (indexPath), e.g. to make 1st cell higher then others do the following:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath{
if (indexPath.row == 0)
return 130.0f;
return 100.0f;
}
*Note that with that code if you have several section then 1st cells in all sections will be higher then others.
Is it possible to modify height of only one cell in a grouped table view?
I have a table view with 2 sections of 3 and 2 rows... I would change row height of the second row of the second section...
How can I do this?
Thanks!
You can look at this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
In your case, the code should look like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1 && indexPath.row == 1) {
return SPECIAL_HEIGHT;
}
return NORMAL_HEIGHT;
}
You can look for more details about the method here
In iOS 8 and above we can use the Dynamic Table View Cell Height.
Using this feature UITableviewCell get its height from its content and We don't need to write heightForRowAtIndexPath
All I have to do in viewDidLoad()
tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
If cell contains uilabel. then apply constraints to uilabel
Make sure you change Label --Lines-- to 0
The cell will grow automatically with content of uilabel:
You can implement the following method to return the height for the row at a given index path:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html%23//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
Have a look at
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
For the indexpath just check for which row and section and adjust the height accordingly