How to change the height of a table cell
I am trying to read some RSS data. I have data of different sizes. the data is present in a tabke data object. I used a shortcut to add data and resize dat. No success. Please, help.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSLog(@"in the tabel view cell");
heightOfCell=[self tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Default"];
if (cell == nil)
{
//cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,heightOfCell) reuseIdentifier:@"Default"] autorelease];
UILabel *label = [[UILabel alloc] init];
NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:10.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
label.text = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
CGSize labelSize = [[[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
label.lineBreakMode=UILineBreakModeWordWrap;
[label sizeThatFits:labelSize];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,heightOfCell) reuseIdentifier:@"Default"] autorelease];
//[label sizeToFit];
[cell addSubview:label];
[label release];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
// This is just Programatic method you can also do that by xib !
}
You can also change it in the Interface Builder. Open the * .xib file, select the table, show the "Dimension inspector" and change the line height. But, note that the default height is 44 pixels.
source to share
There are two ways to set the height UITableViewCell
.:
-
UITableView
has a property namedrowHeight
, with which you can setUITableViewCell
the same line height for everyone ; - If you implement the protocol
UITableViewDelegate
and include a methodtableView:heightForRowAtIndexPath:
, you can decide the current oneUITableViewCell'height
. In practice, we always use the delegate method to set the cell height only if each cell height can be dynamic! And this method prefers the propertyUITableView
rowHeight
!
So, I think you may prefer the second method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellText = [[TableViewData news] valueForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:10.0];
CGSize labelSize = [cellText sizeWithFont:cellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return lableSize ;
}
source to share
I've seen a lot of solutions, but everything was wrong or not. You don't need to calculate anything (no font, no size, nothing) ... You can solve all problems with 5 lines in viewDidLoad and autoplay. This is for object C:
_tableView.delegate = self;
_tableView.dataSource = self;
self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;
For fast 2.0:
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
Now create your cell using xib or as a table in your storyboard With this you don't need to do anything else or override. (Don forget number os lines 0) and lower label (limit) lower "Crawl priority - vertical to 250"
You can download the code in the following url: https://github.com/jposes22/exampleTableCellCustomHeight
source to share
You must know the heights of all cells before calling the delegate tableView:cellForRowAtIndexPath:
, which may require you to store the heights inside your view controller or other list.
The function of setting the heights of the cells tableView:heightForRowAtIndexPath:
. The default height for a table view is 44 pixels for reference.
You can find the documentation here .
source to share