tableView cell height... how to customize it? - iphone

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

Related

Change heightOfPrevious UITableViewCell

Is there anyway I can change the height of previous UITableViewCell when setting height for current cell in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath?
Have two rows in a UITableView. When in first row, it will set the default height.. When in row two I want to change the height of first row. Is this possible? If so how?
Thanks,
Bharathi.
Try to work with a boolean check if the requirement is fulfilled.
You could do it like this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0 && !secondRowIsSynced) {
return otherHeight ;
}
else {
return defaultHeight;
}
}
I think you misunderstand tableView:heightForRowAtIndexPath: is called multiple times (once for each row in your table).
If your rows have different heights then you need to determine which row you are on and what height it should have.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
CGFloat height;
if (0 == indexPath.row) {
// This is the first row
height = // what ever you want
} else if (1 == indexPath.row) {
// This is the second row
height = // what ever you want
}
return height;
}
If you later decide that a row needs to be a different height then it is still this method that needs to calculate the correct height to be used for each row. You can force this to be called without reloading the tableView like this
[tableView beginUpdates];
[tableView endUpdates];
bharathi:
You have to reload your table in the didSelectRowAtIndexPath method of UITableView.
You should check that the second row is selected, then reload all the table rows, so all tableview delegate and datasource methods get called again.

Iphone Table Cell Sizes

I'm using custom cells in my table view. I have the height of the cells set but for only the 10th cell in the table I need to resize.
You need to implement the UITableViewDelegate method heightForRowAtIndexPath:
Something like this:
- (CGFloat) tableView: (UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( indexPath.row == 10 )
return 100.0;
return 40.0;
}

Padding on TableViewCell

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.

How do I make a table view cell dynamic in size?

I am using the
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
to make all the cells big enough for the biggest cell however I would like to make the cell big enough for its perspective contents.
Any help is appreciated.
You can't change the width of individual cells, that is part of the UITableView. You can return a different value for the tableView:heightForRowAtIndexPath: for each row, that is how you make rows different heights. For example:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
MyThing *theThing = [arrayOfData objectAtIndex:indexPath.row];
return theThing.linesOfData * 17; //made up number for font height
}

Increase the size of tableView Cell..?

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.