UITableView losing height when i search - objective-c

Im getting a little trouble here. I have a UISearchBar that searchs for me some content, that are included on UITableView. Perfect.
When my view loads, i set a 110 height to my table view row. Nice. When my view appears, it comes with 110 px. of height. Nice.
But when i click on UISearchBar, my rows loses the height setted.
I dont know what can be.
Any help, thanks!
- (void)viewDidLoad {
[super viewDidLoad];
theTableView.rowHeight = 110;
theTableView.backgroundColor = [UIColor clearColor];
}
And the search code:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.theTableView.allowsSelection = NO;
self.theTableView.scrollEnabled = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=#"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// All the actions goes here, like parsing xml.
[self.tableData removeAllObjects];
[theTableView reloadData];
}
Thanks!

Related

UISearchDisplay in existing UITableView

So I have a UITableView in my view controller (as well as many different things) and I wanted to add a search option, so UISearchBar will be probably the best way to do it.
Is it possible to add a search function in my existing UITableView or do I have to rewrite all my view controller?
Implement searchbar delegate method
UISearchBarDelegate
SearchDisplayController Apple Sample code iOS9
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
// called only once
return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[searchBar setPlaceholder:#"Search your languages"];
// called twice every time
[searchBar setShowsCancelButton:NO animated:YES];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
// called only once
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
// called only once
[searchBar setPlaceholder:#"Select languages"];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length > 0) {
//write your searching code and reload based on search result
//[self searchproduct:searchText];
}
else
{
//arrayLanguage=[searchResultArray mutableCopy];
//reload with original data array when no searching
}
if (searchText.length == 0) {
//when no search reload with original data array
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[_searchBar resignFirstResponder];
}
Thanks

Dynamic textView resize broken after update to iOS 7.1

I have a tableview that requires a subview that's used to add content. The subview contains a UITextView that's used to enter text. Prior to 7.1 The following would happen:
The subview is glued to the bottom of the tableview via scrollViewDidScroll
When activated, the keyboard would show, the view animates up with the keyboard
When text is typed in, the textView would expand upwards if required, with the bottom of the textView remaining in place.
NOW after 7.1, the following behavior takes place:
The subview animates upwards with the keyboard fine
If the text forces a new line, instead of expanding, textViewDidEndEditing get's triggered and the keyboard dismisses.
No further editing is allowed in the textview, any edits makes the keyboard immediately dismiss.
Any suggestions on what changed with 7.1 that would make existing, working code break? Here is all the relevant code. I'll be happy to provide any additional details. I've been at this a while now and it's driving me mental.
tableViewController.m - The code that adds in the subview. The subview has it's own xib. The xib was created in IB. I stripped out the parts that pertain to other components.
- (void)viewDidLoad {
//Setup contentBar
if (self.contentBar == nil) {
//Add subView to View
NSArray *subviewArray = [[NSArray alloc] init];
subviewArray = [[NSBundle mainBundle] loadNibNamed:contentIdentifier owner:self options:nil];
self.contentBar = [subviewArray objectAtIndex:0];
[self.view addSubview:self.contentBar];
//Setup textView
UITextView *addContentTextView = (UITextView *)[self.contentBar viewWithTag:2];
addContentTextView.text = [NSString stringWithFormat:#"text (optional)"];
addContentTextView.delegate = self;
}
}
My textView delegates
#pragma mark - TextView Delegate
- (void)textViewDidEndEditing:(UITextView *)textView{
NSLog(#"textViewDidEndEditing:");
if ([textView.text isEqualToString:#""]) {
textView.text = #"text (optional)";
textView.textColor = [UIColor lightGrayColor]; //optional
}
[textView resignFirstResponder];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"textViewDidBeginEditing:");
if ([textView.text isEqualToString:#"text (optional)"]) {
textView.text = #"";
textView.textColor = [UIColor blackColor]; //optional
}
[textView becomeFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
if ([newText length] > 0 || _image) {
_addContentButton.enabled = YES;
} else {
_addContentButton.enabled = NO;
}
if([text isEqualToString:[text stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]]) {
return YES;
} else {
NSLog(#"return pressed");
[textView resignFirstResponder];
[self addedContent:nil];
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
NSLog(#"textViewDidChange");
[self sizeTextView];
}
- (void)sizeTextView
{
NSLog(#"sizeTextView");
UIImageView *barImage = (UIImageView *)[self.contentBar viewWithTag:87];
UITextView *textView = (UITextView *)[self.contentBar viewWithTag:2];
CGSize sizeThatShouldFitTheContent = [textView sizeThatFits:textView.frame.size];
CGRect newTextViewFrame = textView.frame;
newTextViewFrame.size.height = sizeThatShouldFitTheContent.height;
CGFloat adjustment = sizeThatShouldFitTheContent.height - textView.frame.size.height;
newTextViewFrame.origin.y = textView.frame.origin.y - adjustment;
textView.frame = newTextViewFrame;
CGRect newImageViewFrame = barImage.frame;
newImageViewFrame.size.height = barImage.frame.size.height + adjustment;
newImageViewFrame.origin.y = barImage.frame.origin.y - adjustment;
barImage.frame = newImageViewFrame;
}
If you want to see anything else, I'll be glad to post it.
I have solved this issue by implementing the following:
Rewrote my tableview controller as a standard ViewController, not a TableViewController
Added a tableview to the ViewController and a separate "contentWrapper" view.
The content wrapper consists of the example shown here: https://github.com/datwelk/RDRStickyKeyboardView
I'm still not sure what in iOS 7.1 caused this issue, but using the RDRStickyKeyboardView works great and solved all of my issues.
I've read that tableviews shouldn't be parents of other views, so this solution also works well for good coding practice.

How to use UIPopOver to input text in to UITextField?

I would like to learn how would I make a UITextField get text from a UIPopover view. Let's say I have buttons with numbers on my popover and I want these numbers to be input into the textfield.
Some people sugested to us BOOL like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *) textField {
[self.myPopover presentPopoverFromRect:textField.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
return NO;
}
And one more:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if(popoverController == nil){ //make sure popover isn't displayed more than once in the view
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverDetailContent];
}
[popoverController presentPopoverFromRect:textView.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
popoverController.delegate = self;
return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
}
Both are not making much change, my textField still calls the default iPad keyboard instead of my popover.
Second example uses delegate popoverController and my controller is flipsidePopoverController.
Could I use an IBAction like this?
- (IBAction)togglePopover:(id)sender {
if (self.flipsidePopoverController) {
[self.flipsidePopoverController dismissPopoverAnimated:YES];
self.flipsidePopoverController = nil;
} else {
[self performSegueWithIdentifier:#"showAlternate" sender:sender];
}
}

Passing a string to another view

I have a UITextField in my first view that accepts search criteria however my search box is in my second view. The idea is to pass the textfield data that the user enters into the search box that filters a table view in the second view controller. I have tried setting the secondviewcontroller.searchText = self.search.text however it doesn't seem to be updating the search box in the second view controller with the new data. My code is as follows:
First View.m
- (IBAction)search:(id)sender
{
PlaceList *placelist = [[PlaceList alloc] initWithNibName:#"PlaceList" bundle:nil];
NSLog(#"%#", search.text);
placelist.searchBar.text = search.text;
[self.navigationController pushViewController:placelist animated:YES];
[placelist release];
}
PlaceList.m
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar’s cancel button while in edit mode
useSearchData = YES;
self.searchBar.showsCancelButton = YES;
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[tableData removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
self.searchBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if([searchText isEqualToString:#""] && [__searchBar.text isEqualToString:#""]){ //if nothing is in the search bar show normal table
useSearchData = NO;
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
return;
}
else
{
searchText = __searchBar.text;
useSearchData=YES;
NSPredicate * p = [NSPredicate predicateWithFormat:#"name contains[cd] %#",searchText]; //comparing stored locations to searchText
self.searchResults = [CoreDataBasicService fetchResultsForEnity:#"Place" WithPredicate:p andSortDiscriptor:#"name" Ascending:YES];
[self.tableView reloadData];
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
useSearchData = NO;
[self.searchResults removeAllObjects];
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
self.searchBar.text = #"";
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)_searchBar
{
[searchBar resignFirstResponder];
}
When the search IBAction is pressed I want to push to the placeList view and update the search with the text the user entered in the textfield on the first view controller.
Thanks in advance.
Try initializing PlaceList(VC) with the search Text, saving it to an instance variable. Then, in your PlaceList(VC)'s ViewDidLoad Method, set the text there

Objective C UISearchBar

Hey guys, i want to know why my SearchBar dont excludes the table view cell when i search again? It just add another cell above the first.
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.theTableView.allowsSelection = NO;
self.theTableView.scrollEnabled = NO;
[theTableView setRowHeight:110];
}
- (void)searchDisplayController:(UISearchDisplayController *)controller
willShowSearchResultsTableView:(UITableView *)tableView
{
[tableView setRowHeight:[[self theTableView] rowHeight]];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableData removeAllObjects];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=#"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
return NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// All functions goes here and bla bla bla
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
//Remove tudo da table e recarrega
[self.tableData removeAllObjects];
[theTableView reloadData];
[[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
}
Thanks
Please using a technic, which posted here with 2 tableviews and two fetch controllers.
it's a little bit complicated, but u can remove part of code, which u don.t need.