How to change the height of table view cell
How to change the height of table view cell
i m trying to read some RSS data. I have the data that is of different sizes. the data is present in tabke view data object. I used the label to add the data and resize the 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]; } }
Answer by ennuikiller for How to change the height of table view cell
You need to implement the following method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // some code that compute row's height }
Answer by Nick Bedford for How to change the height of table view cell
You have to know the heights of all cells before the tableView:cellForRowAtIndexPath:
delegate call which may require you to store heights inside your view controller or some other list.
The function to customise the heights of cells is tableView:heightForRowAtIndexPath:
. The default height for a table view is 44 pixels, for reference.
You can find the documentation here.
Answer by darshan for How to change the height of table view cell
you need to use the following method.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 45; }
you need to change the height as per your requirements
It should be noted that the default height is 44.
Answer by Faizan for How to change the height of table view cell
- (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 Interface Builder. Open the *.xib file, select the table, show the Size Inspector, and change the row height. But, please note that the default height is 44 pixels.
Answer by Monjer for How to change the height of table view cell
There are two methods to set the UITableViewCell
's height .:
UITableView
has a property namedrowHeight
, with which you can use to set all theUITableViewCell
to the same row height;- If you implement the
UITableViewDelegate
protocol , and includetableView:heightForRowAtIndexPath:
method, it's you responsbility to decide the currentUITableViewCell'height
. In practice, we always use the delegate method to set the cell's height, only if each cell's height may be dynamic! And the method is preference to theUITableView
'srowHeight
property!
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 ; }
Answer by Jose Pose S for How to change the height of table view cell
I saw a lot of solutions but all was wrong or uncomplet. You no need calculate nothing (no font, no size, nothing)... You can solve all problems with 5 lines in viewDidLoad and autolayout. This for objetive 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 swift 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 with xib or into tableview in your Storyboard With this you no need implement nothing more or override. (Don forget number os lines 0) and the bottom label (constrain) downgrade "Content Hugging Priority -- Vertical to 250"
You can donwload the code in the next url: https://github.com/jposes22/exampleTableCellCustomHeight
References: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment