i was able to hide row on Table View Cell by using this code
cell.hidden = YES;
the problem is it gives blank spaces (show on the picture).
Is there any way on eliminating this blank spaces?
Thank you.
You need to set the correct height for the cell in the UITableViewDelegate.
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == someIndex) {
return 0.0f;
}
return 65.0f;
}
Better would be to not have the cell in tabelview at all.
Related
Is there anyway I can change the height of previous UITableViewCell when setting height for current cell in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath?
Have two rows in a UITableView. When in first row, it will set the default height.. When in row two I want to change the height of first row. Is this possible? If so how?
Thanks,
Bharathi.
Try to work with a boolean check if the requirement is fulfilled.
You could do it like this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0 && !secondRowIsSynced) {
return otherHeight ;
}
else {
return defaultHeight;
}
}
I think you misunderstand tableView:heightForRowAtIndexPath: is called multiple times (once for each row in your table).
If your rows have different heights then you need to determine which row you are on and what height it should have.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
CGFloat height;
if (0 == indexPath.row) {
// This is the first row
height = // what ever you want
} else if (1 == indexPath.row) {
// This is the second row
height = // what ever you want
}
return height;
}
If you later decide that a row needs to be a different height then it is still this method that needs to calculate the correct height to be used for each row. You can force this to be called without reloading the tableView like this
[tableView beginUpdates];
[tableView endUpdates];
bharathi:
You have to reload your table in the didSelectRowAtIndexPath method of UITableView.
You should check that the second row is selected, then reload all the table rows, so all tableview delegate and datasource methods get called again.
There seems to be a UITableView bug, simple UITableView is created and the editing mode is set to be YES, all rows have varying height. For example,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0)
return 58;
else if(indexPath.row == 1)
return 520;
else
return 100;
}
Now on touching reorder control of second row application crashes.
Perhaps these row heights enforce the UITableView to auto scroll an invisible UITableViewCell from the bottom, but UITableView should have handled it.
Any idea….
You need to exchange it..not remove and re-insert it..
[self.itemArray exchangeObjectAtIndex:sourceIndex
withObjectAtIndex:targetIndex];
[self.tableView reloadData];
Is it possible to modify height of only one cell in a grouped table view?
I have a table view with 2 sections of 3 and 2 rows... I would change row height of the second row of the second section...
How can I do this?
Thanks!
You can look at this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
In your case, the code should look like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1 && indexPath.row == 1) {
return SPECIAL_HEIGHT;
}
return NORMAL_HEIGHT;
}
You can look for more details about the method here
In iOS 8 and above we can use the Dynamic Table View Cell Height.
Using this feature UITableviewCell get its height from its content and We don't need to write heightForRowAtIndexPath
All I have to do in viewDidLoad()
tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
If cell contains uilabel. then apply constraints to uilabel
Make sure you change Label --Lines-- to 0
The cell will grow automatically with content of uilabel:
You can implement the following method to return the height for the row at a given index path:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html%23//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
Have a look at
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
For the indexpath just check for which row and section and adjust the height accordingly
I've set my UITableView row height to in Interface Builder to 54.0. I have a UISearchDisplayController on that view. When the user taps the search bar in it, the table resizes properly. However, when they start typing (and actually doing the search) the row height decreases. It stays wrong until the search taps Cancel.
I could find no documentation on this behavior on Apple's site.
I've tried setting the row height in UISearchDisplayDelegate delegate calls. This might be the right approach, but I don't know the details and couldn't get it to work.
I've also tried implementing - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;. This worked, but I have thousands of entries in this list and can't take the performance hit.
What's the right way to fix this?
The correct way to do this is to use the following delegate.
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
tableView.rowHeight = 54.0f; // or some other height
}
It's called when the UITableView is created or shown.
If you happen to call searchDisplayController.searchResultsTableView in other code when a search is not being performed, it will create the UITableView in advance. If you use the willShow delegate method to set the rowHeight, it will miss the timing (for some reason) to change the rowHeight if the tableView has been created beforehand.
I found it!
Assuming the table is stored in tableView:
- (void)viewDidLoad;
{
[super viewDidLoad];
self.searchDisplayController.searchResultsTableView.rowHeight = tableView.rowHeight;
}
Nothing else is necessary.
Edit: See netmikey's answer for a better solution.
Steve, your solution didn't work for me: The first time the search result would display fine, but when a user was hitting "Cancel" (closing away the search bar) and then reopening it and entering a search term into it, the height would be wrong again.
DigDog's solution of using searchDisplayController:didShowSearchResultsTableView was kinda working, but users saw the cell height "jumping" when the search started.
The solution I found, that fixed both these issues is using:
- (void)searchDisplayController: (UISearchDisplayController *)controller
willShowSearchResultsTableView: (UITableView *)searchTableView {
searchTableView.rowHeight = myTableView.rowHeight;
}
... in the UISearchDisplayDelegate.
Regards,
netmikey
In viewDidLoad:
you can set the search display controller's delegate to self:
- (void)viewDidLoad
{
self.searchDisplayController.delegate = self;
}
Then, implement the table view delegate in your view controller:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableview == self.searchDisplayController.tableView) {
return 55;
} else if (tableView == self.tableView) {
return 44;
}
}
Overriding the method didLoadSearchResultsTableView should do the trick.
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
tableView.rowHeight = 82.0; // Change height of search result row
}
More explanation from this blog
You need to set both
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 54.;
}
and
self.tableView.rowHeight = 54.;
also in your UISearchDisplayDelegate
- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
tableView.rowHeight = 54.;
}
Otherwise, when "No Result" happened in searching, cell height will fall back to default.
I've been using ios 7.0 and Xcode 5.0. I think you should change you row height in this method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView)
{
tableView.rowHeight = ...;// change your row height here!
return [self.searchResults count];
}
else
{
...
return ...;
}
}
Because every time a tableview try to display its layout, this method is called, so everytime you type a letter into search bar, this method is called,and the row height won't have a chance to change.
None of the solutions worked for me...
Did my own hit and trial... and got it working...
If you want different row heights for normal & search table views...
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// for normal results
if (tableView==self.tableView) return 54;
// for search results
else return 54; }
And if you want common height...
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 54; }
PS: Do keep in mind, if you have same row height for each row, this method is not very efficient resource usage wise, as the row height is re-calculated for each cell... Although, this is unnoticeable in most cases, I am still searching for a better solution!
I'm set editing mode for UITableView to have a possibility of cell reordering. UITableViewCellEditingStyleNone is returned by editingStyleForRowAtIndexPath: method for every cell, but it reserve some area on the left of cell. Is it possible to prevent such an area reserving, because I'm not need an insert or delete icon on left? In short, i want have a cell that occupate all available area and still can be reordered.
// UITableViewDelegate
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
See the docs: You can set a boolean on the cell to make it not indent. Just add
cell.shouldIndentWhileEditing = NO;
to wherever you create your cell.
Set this to like 2 or 3
tableView:indentationLevelForRowAtIndexPath:
The shouldIndentWhileEditing property only works with grouped tables. I found that setting an indentation level of -3 does the job for plain tables. Is there a better way? Here's what I'm using now:
if (self.tableView.style == UITableViewStylePlain) {
cell.indentationLevel = -3;
} else if (self.tableView.style == UITableViewStyleGrouped) {
cell.shouldIndentWhileEditing = FALSE;
}
Do both
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
and
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
in your UITableViewDelegate. Otherwise the cell content is indented.