I am setting the row height of my UITableView using following code
[tableView setRowHeight: 100.00];
I am using the single line as separator in the UITableView.
Eventhough setting the height above, height of row does not get change.
You should implement
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
delegate method. and return 100.0 there.
You should avoid the heightForRowAtIndexPath if all your rows are of similar height and use the rowHeight property. According to the documentation:
There are performance implications to using tableView:heightForRowAtIndexPath: instead of rowHeight. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).
In the UITableViewController subclass it could be done, for instance, in the viewDidAppear method (the UITableViewController has a reference to the tableView):
self.tableView.rowHeight = 79.f;
The row height is baked into the cells when they are first displayed.
Did you set UITableView#rowHeight before setting the data source?
If not, do so.
If for whatever reason you can't, your other option is to call UITableView#reloadData after setting the row height.
I did like this, here tableobj is nothing but UITableView object in my application.
- (void)viewDidLoad
{
[super viewDidLoad];
[tableObj setRowHeight:100.0f];
}
Or handle it in numberOfRowsInSection: like:
- (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section {
[tableObj setRowHeight:100.0f];
return [soandso count]; // soandso is my object
}
Because set the rowHeight before setting the data source. It worked for me (for equal row heights).
The better and cleaner solution is to implement the delegate function (Maybe not the best one if your UITableView has many rows ...).
Also, think that UITableVieCell are UIView so you could change their height firstly...
Delegates are very powerful in iPhone dev, here the UITableViewDelage:
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html
You can also change the indentForRow, displayCellForRow, heightForRow,edit stuff .....
I dont think there is such a method in UITableView...
Instead you can use the property rowHeight...
Try,
tableView.rowHeight =100;
Related
Two UITableViewCell related questions:
In my custom UITableViewCell I loop through an array (of which I do not know how many objects it holds) and add a UILabel displaying some text for each object in that array.
This means I have to adjust the height of the cell so that these labels fit in. How can I do this?
When going into edit mode, I have the cells indent, however I do not want this. I have tried the following:
cell.shouldIndentWhileEditing = NO;
and
-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
Both sadly failed, I have I no idea why. How could I possible remedy this?
Any help is much appreciated with either of these issues, thanks.
You can specify the height for every row with the delegate method [UITableViewDelegate tableView:heightForRowAtIndexPath:].
Just change what the method returns and the reload your table.
This method actually has nothing to do with the indendation of cell content:
Asks the delegate whether the background of the specified row should be indented while the table view is in editing mode.
You can try to set indentationWidth but I never managed to make it work.
Fortunately, it's easy to change everything you want in [UITableView layoutSubviews] method.
Example:
- (void)layoutSubviews {
[super layoutSubviews];
self.contentView.frame = self.bounds;
}
You may also need to set
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return UITableViewCellEditingStyleNone;
}
For the dynamic height part there are plenty of other answers and a quick google found a tutorial here
I have a custom cell for my UITableView. I have put a swipe left right/left observer on my cell. In which that if a user swipes on a cell it will call a function. This function basically creates a UIView that needs to be added to the cell.
I would like my code to use a MVC best practice. So I think what is the appropriate thing to do is to pass in the UIView to the custom cell and let my implementation of my custom cell add it to the cell. In my custom cell I have a property of UIView as well.
The issue is that I will need to adjust the height of the cell as well. Now the method that I have is:
+ (CGFloat)tableView:(UITableView *)tableView rowHeightForObject:(id)item {
}
this is similar to what a regular heightForRowAtIndexPath, instead of index path it's object. In this method I need to determine whether there is a UIView that needs to be added or not. If there is, the height needs to be adjusted based on that. I can't seem to get my head around this. In this method I can't do self.optionsView, or access any of the property in my custom cell subclass. So how do I check whether the options is added or not?
You can add a rowHight property to your custom cell class, and add another height var to your table view datasource data.
Every time you change the custom cell, make sure to set the height property, and assign it to your data source data in the right position.
Then, inside heightForRowAtIndexPath just ask for the height data.
Hope it's clear.
As Idan pointed out it's good to just ask cell for it's content size. I would just use the standard heightForRowAtIndexPath method like this:
//Assuming myCustomCell contains a placeholder for my additional view called innerView.
//If the place holder is nil I don't need to adjust cells height.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//Get the cellPath and myCustomCell at this path
NSIndexPath *rowPath = [NSIndexPath indexPathForRow:row inSection:0];
MyCustomTableCell *myCustomCell = (MyCustomTableCell*)[self.table cellForRowAtIndexPath:rowPath];
if(myCustomCell.innerView != nil)
return myCustomCell.innerView.frame.size.height + someDefaultCellHeight;
else
return defaultCellHeight;
}
How do i display set number of rows in a UITableView?
i.e if my data source has only 4 objects then i want the table to display on those 4 rows without any blank rows. any ideas?
thanks!
EDIT: i wasn't too clear about the question so..
So this is my table: http://i.stack.imgur.com/glkWZ.png ..
i want to display only the 3 rows and not the blank rows below it. The number of rows change depending on my data source.
any ideas how to go about doing so?
You have to change your table appearance, I think. Once you correctly set the numberOfSectionsInTableView: and tableView:numberOfRowsInSection: methods, you should change your viewDidLoad method adding this line:
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
This way you will remove all lines from the table.
If you still want to make a line appear between lines, I suggest you to create a custom UITableViewCell, or to build a standard one adding a subview to mimic the line. Eg. something like this:
CGRect lineFrame = CGRectMake(0,cell.frame.size.height-1,cell.frame.size.width,1);
UIView *line = [[UIView alloc] initWithFrame:lineFrame];
line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
[cell.contentView addSubview:line];
[line release];
Let me know if this helps.
Check the UITableView DataSource protocol, there are two relevant methods you have to implement:
– numberOfSectionsInTableView:
– tableView:numberOfRowsInSection:
The first one should return 1, the second one 4.
If you want to keep a set number of lines in your tableview and not display the extra blank lines, you could insert a empty footer view in viewdidload function.
self.tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
I did not actually understand what you mean with "display on those 4 rows without any blank rows" but I guess what you ask is just a simple data retrieval process. I suppose the data source is NSarray.
-numberOfRowsInSection:(NSInteger)section{
return [MyArray count];
}
-cellForRowAtIndexPath(NSIndexPath*)indexPath{
NSInteger row=indexPath.row;
cell.textLabel.Text=[MyArray objectAtIndex:row]
return cell;
}
I assume you know what should be put in those blank spaces.I just wrote the major points.
try this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4
}
also implements the rest of delegate methods required to populate tableview..
Hope it helps you.......
You can use this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4
}
Or you this also
Check the UITableView DataSource protocol, there are two relevant methods you have to implement:
– numberOfSectionsInTableView:
–
tableView:numberOfRowsInSection:
The first one should return 1, the second one 4.
Or you can also use
GroupBy table in your view by changing the table style to gourpby table. use
Is it possible to create a custom UITableViewCell that resizes depending on the length of the text that I want to put into? If possible, how do I do this?
#EquinoX yes it is possible through heightForRowAtIndexPath delegate....Please have a look on this and Dynamic Height UITableViewCell they have same thing you are asking.
Good Luck!
Absolutely,
I like to set all the data on my cell by passing it a model (just an NSObject with data in it). Then I create a custom cell that can have its data set by the model, and I add a class level function to return the size. It looks like this.
TableViewDelegate:
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
XTableViewCellModel *model = [self modelForIndexPath:indexPath];
return [XTableViewCell cellHeight:model];
}
XTableViewCell:
+(CGFloat)cellHeight:(XTableViewCellModel*)model {
CGSize titleSize = [model.title sizeWithFont:model.titleFont
constrainedToSize:CGSizeMake(280, 9999)
lineBreakMode:UILineBreakModeTailTruncation];
return titleSize.height;
}
Note that setting lineBreakMode to UILineBreakModelTailTruncation only takes effect after the height of 9999 points is reached. Until then the text wraps normally.
Check out my open source framework around this stuff. It has a lot of resizable cell types built by default: https://github.com/andrewzimmer906/XCell
Look at this Sample Code it uses exactly what you looking for.
I have a custom UIView subclass that i'm trying to use as a header for one of my grouped tableview sections. I save an instance of that view in the tableViewController and use that to return the height for the header section as well as the view itself. the problem is that somehow that instance variable changes from a UIView to a CALayer in the middle of a reloadData call which causes a crash, since the instance has a special method to return it's expected height. this is the code that crashes:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return [self.dataHeader frameHeight];
}
return 0.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return self.dataHeader;
}
return nil;
}
I set a breakpoint at the first return in the if block of the heightForHeaderInSection method, and it hits it 4 times; the first three return the dataHeader successfully, while the fourth time shows it to be a CALayer and crashes with a doesNotRecognizeSelector exception (my tableview has 2 sections if that makes a difference). Is there any reason why this happens, and is there a way to stop it?
What does your initialization code for dataHeader look like? When you initialize dataHeader, are you properly retaining it?
My guess is that your dataHeader view is getting released before you intended.
The problem seems to be that you have 2 sections and somehow the app thinks there are 4. Here's how I would debug this problem:
1) What is numberOfSectionsInTableView returning (is it implemented)?
I assume that each header method should be called n times, where n is the number of sections in your table. I would also assume that the app asks the aforementioned delegate what n is.
2) What are the values of section each time these delegates are called?
There should only be one call per section, unless I'm missing something, and I would be amazed if the delegate gets called more than once with the same section value.