tableView cell height… how to customize it?

2019-01-18 10:49发布

问题:

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!

回答1:

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



回答2:

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:



回答3:

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:



回答4:

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