I have created two different tableviewcell into single tableview, Now I have set separate height for separate cells at storyboard "row height". Now the problem is whenever I run the application Its not changing both height are same.
You should implement
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
and return different heights for Parent and Child cells.
Sample Implementation :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 0;
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.reuseIdentifier isEqualToString:#"ParentCell"]) {
height = 100.0; //Parent Cell height
} else if ([cell.reuseIdentifier isEqualToString:#"ChildCell"]) {
height = 50.0; //Child Cell height
}
return height;
}
Related
MY GOAL:
Each UITableView Cell Contain 1 UIColletionViewController (dynamic items count)
PROBLEM: UITableView 'HeightForRow's delegate, is call before UIColletionViewController finish load his view + height.
RESULTS:
UITableView 'HeightForRow = 0
How can i set the UITableView "HeightForRow" delegate correctly?
the UIColletionViewController's content height haven't loaded yet
the result should be like in the following draw:
ALREAD TRY:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 300.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//not working well
return UITableViewAutomaticDimension;
//also tried this:
ResultsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Object * object = [_array objectAtIndex:indexPath.section];
[cell setCellObject:object]; //this is build the cell ui
return cell.collection.rect.size.height;
}
return the "content size" (instead of view.size)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//1. get the cell (by identifier)
ResultsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ResultsTableViewCell"];
//2. build cell ui
Object * object = [_array objectAtIndex:indexPath.section];
[cell buildCell:object];
//3. return content size
return cell.collection.collectionView.contentSize.height;
}
I have created a simple UITableView with a prototype cell and I have to enlarge the height of row. Unfortunately, I cannot achieve that. I just put the custom size of height for the prototype cell in Interface Builder (Xcode) and put method in ViewController:
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200.0;
}
And cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:#"albumCellID"];
if (cell == nil) {
cell = [[AlbumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"albumCellID"];
}
cell.albumTitle.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Simulator returns my table with still small height for rows (ca. 40 px). How can I enlarge the height of a row?
I am dequeuing the second prototype cell but the height of the cell appears to not be respected.
Can anyone lead me in the right direction ? I have tried searching google with no avail.
The problem has been solved,my solution is implementing in code:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height;
if ([indexPath row] == 0) {
height = 170.0f;
}
else {
height = 72.0f;
}
return height;
}
you need to implemented the table view delegate method heightForRowAtIndexPath: in order to change a cell's height. you can't do this purely in storyboards.
Remember
storyboards or xib can not dequeue cell without code,so you have to setDelegate and write in code tableView:heightForRowAtIndexPath ,
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height;
if ([indexPath row] == 0) {
height = 100.f;
}
else {
height = 50.f;
}
return height;
}
I have a table with a custom cell, which has only one UIImageView. I want each cell to have its own height based on an image height I will place in it. In my heightForRow method I have
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
FAppFormula *formula = self.formulas[indexPath.row];
UIImage *img = [formula getFormulaImageSmallSize];
if (img.size.height <= 100){
return 30;
}
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *cellIDSmall = #"FormulaCellSmall";
FormulaCell *cell = (FormulaCell *)[tableView dequeueReusableCellWithIdentifier:cellIDSmall
forIndexPath:indexPath];
cell.formulaImage.image = formulaImg;
return cell;
}
The cells themselves do get proper heights, however images don't get scaled appropriately. In storyboard cell prototype I set UIImageView's mode to Aspect Fit, however looks like all images get scaled based on a default(?) row height, but not on the one which heightForRowAtIndexPath returns...
Any ideas on why it happens and how to fix it? How it looks now img
I need to set the height for just one row programmatically, all the other rows are static cells created in the Storyboard with different heights.
In my code the 5th cell should be "hidden" as default, therefore I want to set the height of this row to 0.0. All the other cells should use their height from the Storyboard, I'm using the method tableView:heightForRowAtIndexPath to set the height of the cells.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = self.tableView.rowHeight;
if (indexPath.row == 4)
{
height = 0.0f;
}
return height;
}
The problem is that self.tableView.rowHeight always returns 658 as height, which is a totally wrong value.
If you implement heightForRowAtIndexPath you need to set a value for all cells or you will run into stuff like this. What you can do is the following:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row){
case 4:
return 0;
default:
static NSNumber *height;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCustomCell"];
height = #(cell.bounds.size.height);
return [height floatValue];
}
}