I have a static table view set up in interface builder, but in some circumstances I don't want some cells to show. What's the best way to accomplish this with static cells? I'd really like to use static cells because a) it makes sense because there is no datasource, and b) I'm rendering my static views using IBDesignable/IBInspectable and would like to keep it that way.
You should be able to add the UITableViewDelegate method tableView:heightForRowAtIndexPath: and return 0 for any row that you don't want to show.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat rowHeight = 44.0;
if (indexPath.row == /* a row you want to hide */) {
rowHeight = 0.0;
}
return rowHeight;
}
Related
I have three custom TableViewCells. I have one of the three cells that I need to set heightForRowAtIndexPath for in code because I can't use AutoLayout in Storyboard for it.
Is there a way to set heightForRowAtIndexPath for a specific custom TableViewCell?
My heightForRowAtIndexPath:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
id model = self.combinedModel[indexPath.row];
// Custom Table View Cell Two
if ([model isKindOfClass:[Data self]]) {
return 500;
}
// Custom Table View Cell One and Three
else {
// Not sure what to put here, but need to put something without setting a height
}
}
I do the following in my code:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
id model = self.combinedModel[indexPath.row];
// Custom Table View Cell Two
if ([model isKindOfClass:[Data self]]) {
return 500;
} else {
return tableView.rowHeight; // return the default height
}
}
"Put something without setting a height" doesn't make sense. You need to give the table view a height for every cell. Give it the default height you're using for other cells.
I'm trying to create detail view controller as a list of information and I think it would be nice and clean to present this with a static UITableView. But after that it came to my mind that on some level it might be difficult, so please resolve my doubts!
Every UITableViewCell has different style (some are custom, some are basic and few are right-detailed etc.).
What is more, content size of each cell may vary as I have long names put inside labels so they use autolayout to fit.
There is no problem when I have the same cells repeating but with different tex inside UILabels. In that case I use a simple:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.prototypeCell) {
self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:#"ActivityCell"];
}
[self fetchedResultsController:[self fetchedResultsController] configureCell:self.prototypeCell atIndexPath:indexPath];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;
}
I don't know how to deal with heightForRowAtIndexPath. I can give an identifier to each cell, call cellForRowAtIndexPath:, and make a big switch or if statement, but is it right? The same problem occurs while I think of cellForRowAtIndexPath: and populating those UITableViewCells. With those testing statements this code won't be pretty and readable.
Any ideas on that case?
In the delegate function of the table view named heightForRowAtIndexPath try to calculate the height for each row and then return it.
//return height for row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==tblLanguage)
{
//Here calculate the dynamic height according to songs count for specific language
return (([[arrSongListForSpecificLanguage objectAtIndex:indexPath.row] count]*40)+40);
}
return 40.0;
}
I am creating a simple sign up/login screen. I am using static cells as follows:
The cells are static. I want that when I click the sign up button I add 2 more cells to the tableview. Is that possible considering that I am using static cells and not dynamic?
UPDATE:
I am trying to set the height to 0.0f inside the heightForRowAtIndexPath but it is blowing up.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // this line
if(cell.tag == 2)
return 0.0f;
return cell.frame.size.height;
}
When you reload the table and it asks for the size you would add 2 more to it. Just based on the row index you would decide to use the static cells or whatever else you are inserting.
Your question is not very clear but it appears that you have 2 cells in the image. That means you're trying to go from 2 to 4 cells once your signup code has run. Simply put the 4 static cells in your storyboard UITableView and implement the following method and call [self reloadData] to trigger it:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (SIGNUP_CODE_HAS_RUN == YES)
{
// where SIGNUP_CODE_HAS_RUN is set to YES after you've run your signup code
return 4;
}
else
{
return 2;
}
}
I have many cells which contains several resizable UITextViews. I want to make dataSource to call heightForRowAtIndexPath before every cellForRowAtIndexPath call, not only when table is reloading data. In that case I could return fake heights on reloading, and I would not need to redraw ALL textViews all the time when updating (performance is very low when I need to redraw lets say 1000 textViews), but just in time, when these textViews are truly needed.
Use the function :
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
in your controller
Yes Do it with UITableViewDelegate implement tableView:heightForRowAtIndexPath: Like
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.row == 1) {
return 90;
}
else{
return 45;
}
}
I want to make a UITableViewCell bigger. How can I do this, I have tried adjusting its frame but nothing happens. What can I do?
You need to implement the
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
method in your UITableViewDelegate.
You can do like this..
//Change the Height of the Cell [Default is 44]:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 1) {
return 90;
}
}
return 44;
}
I hope this will help you..
There are 2 steps to change height of a table cell without any additional code:
Select your Table View from your Storyboard. In the Size Inspector change Row Height.
Select your Table View Cell and while it's selected in the Size Inspector change the Row Height.
Note that you MUST set BOTH of Table View's Row Height attribute and Table View Cell's Row Height attribute.
If you just set one of them, it won't work.
I found this answer to be the most informative and most up-to-date to change the height of a table view cell.
TL;DR
Take extra care when making your Auto Layout constraints
Enable row height estimation on your table view in the viewDidLayoutSubviews method.
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is
You can use this code for changing the height of different tableview
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == tableView1)
{
return 55;
}
else if(tableView == tableView2)
{
return 75;
}
}
This may help you.