Stop UISearchDisplayController from showing empty cells - ios

I've implemented a UISearchDisplayController on a fairly standard tableview (same datasource for table + search). The problem I am having is when the results don't fill the screen there are "pseudo" rows below the actual results.
I was able to set the background color, but can't find a good way to suppress these rows/separators. They seem decoupled from my numberOfRowsInSection: delegate response.
If I set the searchResultsTableView.separatorColor (green) it only changes the actual results rows.
I was able to change separatorStyle to UITableViewCellSeparatorNone, but then I have to manually recreate the separators on the actual results and there are edge cases (like the selection color covers up my view).
Is there a clean way to hide the rows pointed out in the attached screenshot?

You can probably implement this delegate method for the Search Display Controller:
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView;
{
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 1)];
footer.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:footer];
}
This will make those last few rows disappear. You can of course do this in any other method you choose. For example, you can do this in viewDidLoad of a UIViewController if you want the same effect on a normal UITableView.

Related

UITableview displays limited cells

I have a UITableView that is setup correct and at some point I wanted
to display only a limited amount of cells. Now that I want to revert back
the functionality I am unable to do so.
The property that limits the visible cells (according to my git log):
// This will remove extra separators from tableview
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
After removing the above line, I still do not get the desired effect.
But something was introduced, and I am not sure what to search for.
I want the default behavior back with many empty cells.
Here is my methods for the sections and data, currently I only have
two cells visible.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
View Hierarchy Debugging data.
I am adding images of the View Debugger, "Wire Frame View" and "Content View" seperately.
From the images you will see two types of separator lines;
I mimicked a line by adding space at the bottom of the cell
so that I could have "thin lines".
Please check the tableview height. I think height of the table currently you are seeing is less so you are getting only 2 visible cells.
As one can see from the attached images, the wire frames show that the separators are in fact drawn but they are not visibly on the screen when you view the content.
Simply change the colors:
self.tableView.backgroundColor = [UIColor redColor];
self.tableView.separatorColor = [UIColor greenColor];

UITableView grouped hide section separator

I know this question has been asked before. But no person on the internet had a working and sufficient answer.
EDIT Obviously people don't read questions anymore, on SO. So I'm trying to clarify: I want to remove the SEPARATOR. The separator is neither the space above the section, nor the tableViewHeader or tableViewFooterView. It is only the thin line above (fully from left to right).
I have a grouped UITableView (I don't want to use a plain styled for many other reasons, take it as it is) which has multiple groups.
The first section should not have the separator line on top. Setting the separator style of the tableView is not an option, because I do need the other separators.
Setting the tableViews tableFooterView is something I often read, but it never worked.
I used the tableView with static content before and I was able to remove the separator in -[UITableViewController viewDidLoad] using this:
- (void)viewDidLoad {
[[[self headerTableCell] valueForKey:#"_topSeparatorView"] removeFromSuperView];
}
Since I now had to change the tableView to a dynamic one, the IBOutlet property won't work anymore (obviously).
So I tried everything, -[id tableView:willDisplayCell:atIndexPath:], -[UITableViewCell initWithStyle:reuseIdentifier:, prepareForReuse, awakeFromNib] and some others.
In any case, this separator is nil. So I need a method that gets called when the complete view hierarchy of the cell is setup.
what i get from your situation you have a grouped UITableView you want the first section without separator and you want to keep the separator in the other sections so
remove the separator from the whole tableview from the attributes inspector make Separator : None
create custom UITableviewCell in storyboard for other sections and add View at the end of it with height 1 and width the whole screen (like default separator)
it's maybe not the best idea but this will allow you to have the first section without separator
I faced a similar problem, wanted to remove the last line of the section in grouped table view, I am calling following method in view will appear and on every table reload. This is not the exact answer but problem can be solved by just changing y value of dummy view.
+(void)removeLastSectionSeparatorForTableView:(UITableView *)tableView
{
UIView *oldSeparatorView = [tableView viewWithTag:kTagDummySectionSeparator];
if (oldSeparatorView != nil)
{
[oldSeparatorView removeFromSuperview];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(),
^{
UIView *view = [[UIView alloc] init];
view.tag = kTagDummySectionSeparator;
view.backgroundColor = [UIColor colorWithRed:239.0/255 green:239.0/255 blue:244.0/255 alpha:1.0];//Group table background color
view.frame = CGRectMake(0,
tableView.contentSize.height-40,
tableView.bounds.size.width,
2);
[tableView addSubview:view];
});
}
Maybe this problem is the same as mine before.
Finally, my way to solve this problem: set table view delegate's method (CGFloat)tableView:(UITableView *)tableView heightForHeaderAtSection:(NSInteger)section, then return CGFLOAT_MIN;
add this override function in your Custom Cell Class
override func layoutSubviews() {
super.layoutSubviews()
for subview in subviews where (subview != contentView && abs(subview.frame.width - frame.width) <= 0.1 && subview.frame.height < 2) {
subview.removeFromSuperview()
}
}

How to add multiple cells in header of a table view (not section header)?

I would like to implement segmented controls like this (circled with red) :
(When we scroll this view, the segmented controls stay sticky at the top of the view, that makes me think it's a cell on its own ? but I may be wrong).
I have already implemented a custom cell (that displays kind of what is above the red ellipse in the above picture, in purple) and add it to the header of my table view like so :
BigCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"BigCell"];
self.tableView.tableHeaderView = cell;
Now, I think I should create a second cell with the segmented controls in it and add it also to the table header (and not in the section header, because I have many sections with their titles).
Then, I would create a UIView containing this two cells and add this view as the header of my tableView ? Is this a correct way to do it ?
Thank you very much for the help !
I think you have a couple of options.
1) Make a container view to host both of your "cells" (which needn't be UITableViewCells - just views...). Add the singular container view as the table header.
2) Forego using the table header altogether and just place your views above the table, making it shorter. This is more complicated if you're using a UITableViewController, but simple if you're just hosting a UITableView in some other custom UIViewController.
this is an example with uilabel that stick to the top - just change it to your uisegmentedcontrol
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *viewForSectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, SETTINGS_HEADER_HEIGHT)];
[viewForSectionHeader setBackgroundColor:[Utils colorHeaderBlue]];
UILabel *lblSectionTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, SETTINGS_HEADER_HEIGHT)];
lblSectionTitle.text = #"PROFILE";
lblSectionTitle.textAlignment = NSTextAlignmentCenter;
lblSectionTitle.textColor = [UIColor whiteColor];
[viewForSectionHeader addSubview:lblSectionTitle];
return viewForSectionHeader;
}

Background Texture of a grouped UITableView won't scroll with the table

I'm working on an app that has lots of UITableViews and I'm trying to give them a textured background color. I need to use the Grouped style because I don't want the section headers to float over the text fields when the user scrolls.
The problem I'm having is that when I use the Grouped style, the background texture doesn't scroll with the table; it stays in place while the table scrolls above it. I feel like this is kind of weird and I would rather have the background scroll with the table, the way it does in the Plain style. Unfortunately, because I can't have the header views floating on top of everything, that doesn't appear to be an option.
Has anyone been able to accomplish this?
Here's some relevant code:
- (void)loadView {
[super loadView];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texturedPattern.png"]];
// this prevents the cells from replicating the background texture
self.tableView.backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// disable the default grouped border since we're doing it manually with the textField
cell.backgroundView = nil;
cell.backgroundColor = [UIColor clearColor];
}
And my table view currently looks like this:
UPDATE
As Amit Vyawahare suggested, I tried applying the background texture to the background of the headers and each cell. There are two problems that are both much more obvious when you see it in motion, but I'll do my best.
First, the background is always visible. I've removed the background color from the table to make it more obvious:
Everywhere you see black, the background texture of the tableView would be visible and it will not scroll with the tableView. The Grouped tableView style inserts the 5 pixel border on either side of every cell and can't be changed. Additionally, there is no footer beneath the Staff ID section, and I've even implemented -tableView:heightForFooterInSection: to return 0.0, but there's still a gap there.
Second, even if I were able to get rid of these gaps, the textures don't line up anyway. Again, this is difficult to see, so I've uploaded a retina screen shot to make it a little easier:
This is most obvious above the Password section, you can see the textures don't align properly and it looks kind of like a "fold" in the paper. Which would be cool, I guess, if that's what the client wanted. It's visible, but less obvious on just about every edge from the second screen shot. This is because the texture is actually quite large: 200x200 (400x400#2x), and there are slight variations in color that aren't noticeable unless this sort of misalignment happens.
First replace your UITableViewController by a UIViewController and add a UITableView to it. Set the autoresizingMask to flexible width/height. Now you have something equivalent to a UITableViewController, but with more control over the view hierarchy.
Then add a view below the tableview (actually: add that one first) which holds the background.
Finally, set the delegate of the scrollview to your class, and respond to scroll events by updating your background view accordingly.

UITableView has cells, but shows no cells

So, the correct number of rows shows up. On pressing any row, the correct action takes place. However, the cells themselves are nowhere to be seen.
I added a NSTimer in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for each cell just to trace it out beyond the function - they all say that their superview is equal to the tableview in question (and is not nil, so i'm not checking nil == nil or something).
They all contain labels with the correct text.
The separator lines are being drawn.... If I change the TableView background, the whole visible area's background shows as that color.
I'm checking that each cell is neither hidden nor set to an alpha of 0.
Is there anything else I could be missing?
Are you loading from your cells from a nib file or creating programmatically?
Are you overlaying another object over your cell in the cell subview? Perhaps a subview is covering it; I can't tell, since you have not posted any code yet. Given the information you have provided, it is difficult to determine why you cannot see the cells backgroundView.
Try changing the color with
UIView *tmpView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
tmpView.backgroundColor = [UIColor blackColor];
myCell.backgroundView = tmpView;
It sounds like you have set the backgroundView of your cell to [UIColor clearColor].

Resources