I am making a Calendar app and I am showing the events from the Calendar in a UITableView.
I am using a tableFooterView to hide separator lines in a UITableView when the table is empty. I do this with the following code:
self.tableView.tableFooterView = [[UIView alloc] init];
UILabel *footerTableViewLabel = [[UILabel alloc] init];
footerTableViewLabel.text = #"No items.";
[self.tableView.tableFooterView addSubview:footerTableViewLabel];
If there are elements in the table, I hide this view, and if not I show it. This works fine if the calendar is initially empty. However, if I delete an event from Calendar.app while my app is still running in the background, when I go back to the app, all I see is a bunch of empty cells and separator, and no label saying "No items".
Any ideas?
EDIT: I show/hide the footer view using:
self.tableView.tableFooterView.hidden = NO/YES;
As far as events are considered, I do not offer the possibility to delete them directly from my app, I just detect when they are deleted from Calendar.app. Unfortunately, I cannot show the code doing this detection since it is part of a private library, however, this works fine, i.e. I jump to right parts of code when the event is deleted.
You can try this un your code to hide separators: self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Use self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; to hide the separator lines. If you want the active tableview cells to have separators, you can always add a border to the bottom of each cell manually using a function like this:
- (void)addBottomBorderWithColor:(UIColor *)color andHeight:(CGFloat) borderHeight
{
CALayer *borderBottom = [CALayer layer];
borderBottom.backgroundColor = color.CGColor;
borderBottom.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, borderHeight);
[self.layer addSublayer:borderBottom];
}
** EDIT**
If you want to add a view that indicates 'no data', try adding this as a view underneath the tableview, then just set the tableview to hidden if there is no data present.
Hope this helps
So, I finally managed to figure this out. There were to issues:
For some reason (maybe when I do [self.tableView reloadData]), self.tableView.tableFooterView would become nil and that's why I could see empty cells. Reassigning the view to it fixed this issue.
I had to update the view constraints for the footer view and the label it contains.
Related
In my app I have a UITableViewCell with a UITextField in it. When the user starts typing in this textfield, an autocomplete view appears.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (self.suggestions != nil) {
self.autocomplete = [[AutoComplete alloc] initWithFrame:CGRectMake(10, 53, self.frame.size.width-20, 200)];
self.autocomplete.delegate = self;
self.autocomplete.suggestions = self.suggestions;
[self addSubview:self.autocomplete];
[self bringSubviewToFront:self.autocomplete];
}
return YES;
}
This is nothing more than a UIView with a UITableView inside it. However, what happens is that this view is hidden behind the section header and the next cell.
So while it appears to be inserted above the cell, it registers the tapp below it. When you click in the autocomplete it registers the click in the next cell. How can I fix this?
Your issue is you are adding it to the view and not the tableview which resides in the UIView as well as having a section header that will probably block it out as well. I see your y position is only 53, so your header is likely blocking it out.
I am not 100% sure where you want your result to be viewed and used, that's not clear in your question. If you want your view to be above everything else you could:
"I have a UITableViewCell with a UITextField in it. When the user starts typing in this textfield, an autocomplete view appears." - add the result view to your cell, not the main view; [cell.contentView addSubview...
Shift your tableview down by changing it's y position to self.autocomplete.view.frame.size.height. Add a nice little animation to it as it changes position too, always something that bit extra.
Consider adding it as a subview to your TableView and bringing to front, should work.
(little bit extra on the cell.contentView)
I implemented a horizontal table view and it looks like this
The category bar, Dining Shopping something, is a horizontal table view and here is the implementation code.
LogInfo(#"Show Category Table start");
// add categories to be subview controller
self.categoriesTable = [[UITableView alloc] init];
self.categoriesTable.transform = CGAffineTransformMakeRotation(-M_PI * 0.5);
[self.categoriesTable setFrame:CGRectMake(0, 64, SCREENWIDTH, 44)];
self.categoriesTable.showsVerticalScrollIndicator = NO;
self.categoriesTable.showsHorizontalScrollIndicator = NO;
self.categoriesTable.pagingEnabled = YES;
self.categoriesTable.delegate = self;
self.categoriesTable.dataSource = self;
self.categoriesTable.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.superView addSubview:self.categoriesTable];
LogInfo(#"Show Category Table Finished");
self.categoriesTable.backgroundColor= [UIColor redColor];
It works as expect but if I change view, for example, I click any button to go to other view and go back to this view. The tableview looks like this
This problem also happpens even if I disable the bounce effect of the table view. Does anyone know how to solve this problem? Thanks!
Rather than applying a transform to the table to make it horizontal, use a collectionView with a horizontal layout.
Edit: If you want to continue using your current setup, try disabling automaticallyAdjustsScrollViewInsets on your view controller.
self.automaticallyAdjustsScrollViewInsets = NO
Edit 2: If you're curious, since iOS 7 every view controller looks at its topmost/first scroll view and adjusts its contentInsets to compensate for the navigation bar transparency which is enabled by default. In this case of course, such behaviour isn't desired at all.
I am working on a project right now and I have to change the old code and using what have been done,
I am collecting information (item, label ..) by parsing Json,
in the old code we have a modal view that is divided in two parts :the first part display a picture of the Item and the second is a table-view with multiple button and a search bar,
what I have to do is to split that,
I need to have the search bar in a an other table view that I push by click on a button, when I click on a search-bar it push an other view controller which display a map,
the problem is what have been done used a storyboard and I have to do it programmatically!
How can I do it?
You can remove UISearchBar programmatically by sending removeFromSuperview message to it, and then go ahead and create a new instance of it and place it accordingly.
You can set the UISearchBar as the header of a tableView in a very simple way.
For eg. define your search bar like this
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.searchBar.barTintColor = weAskDefaultBkgColor;
self.searchBar.placeholder = #"Search users by their name or username";
self.searchBar.delegate = self;
Then, set this as tableView header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return self.searchBar;
}
I have a view which has two containers: Top_Container and Bottom_Container.
Each Container points to a VC with a TableView.
The Bottom_Container points to a TableView with a searchBar on top.
Whenever the searchBar gets activated in the TableView a white space appears below the searchBar between the searchBar and the greyed zone corresponding to the serachBarTableView (which superposes the TableView).
I have been trying with no success to get rid of this white space with no success.
Anybody has an idea how to customize:
- the white space which appears below the searchBar ?
- the greyed zone (searchBar TableView ?) on top of the TableView which appears whenever the searchBar gets active ?
Thank you.
Try with following code:
CGRect rect = self.searchBar.frame;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, rect.size.height-2,rect.size.width, 2)];
lineView.backgroundColor = [UIColor clearColor];
[self.searchBar addSubview:lineView];
It is fix.
I had similar problem and in my case it was caused by opaque NavigationBar. When I set NavigationBar to translucent, then the underlaying UITableView is correctly aligned to the active UISearchBar. (note: I'm using UISearchDisplayController in my view controller)
In your case maybe you can move (and animate) the underlaying table view in UISearchDisplayDelegate's methods willBeginSearch and willEndSearch. If you are using UISearchBar only, then you need to subclass it and override becomeFirstResponder and resignFirstResponder methods and implement the "table view moving" code there.
I just fixed this problem in my own code. For me, the issue was caused by 2 lines of code.
self.viewController.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationBar.translucent = NO;
After removing both these lines, everything worked as expected.
In short, I need to restore the default backgroundView for a UITableViewStyleGrouped cell.
cell.backgroundView = ??;
The long story:
I have a grouped tableview with some cells. For every cell the user needs to select some value or do some input. After that he can proceed to the next ViewController by touching a "next" button.
Anyway, to indicate to the user that he missed something, I display a nice red border around the cell with the wrong/missing user input. I do that by setting the backgroudView of the cell with a custom image like this:
cell.backgroundView = myErrorIndicatingImageView;
The cell now looks like this (screenshot, ignore the stars and label)
So far so good, this works like a charm. But after the user corrects the input I want to remove my red border background image and just show the original background again. The original background looks like this (screenshot):
And this seems to be a problem.
I tried several things
// try by setting the background to nil
cell.backgroundView = nil;
this removes the background completely and I'm lost with a cell without background.
// try it with addSubview and later remove the subview again
[cell.backgroundView addSubview:myErrorIndicatingImageView];
this does nothing. The myErrorIndicatingImageView is not visible. Even a [cell.backgroundView bringSubviewToFront:myErrorIndicatingImageView] does not help.
Right now the only solution I have is to create a new UITableViewCell for every single call to cellForRowAtIndexPath. This works but is just bad code and ignores completely the technique to reuse cells.
There must be a simpler solution...something like
cell.backgroundView = [self.tableView getDefaultBackgroundView];
what about trying this:
cell.backgroundView = [[UIView alloc] initWithFrame:cell.backgroundView.frame];
Or you can make the background view as a UIImageView and set 2 images in problem and fixed
Assuming you are doing this with a custom table cell class, save the original backgroundView in some ivar, then apply the new background. When you want to reset the background, set the backgroundView back to the original, saved background view.