< p>After this I pass this tabledata array to the tableView named myTableView, I declare the height of the table view in the didLoad method.
I want the height of the tableView to be dynamic according to the number of elements in the table ,
This is the code snippet,
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar's cancel button while in edit mode
sBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[tableData removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
sBar.showsCancelButton = NO;
[myTableView setHidden:TRUE];< br />}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[myTableView setHidden:FALSE];
[tableData removeAllObjects];// remove all data that belongs to previous search
myTableView.backgroundColor=[[UICo lor alloc]initWithRed:4.0 / 255 green:24.0 / 255 blue:41.0 / 255 alpha:1.0];
[self.view bringSubviewToFront:myTableView];
if([searchText isEqualToString:@""]||searchText==nil){
[myTableView setHidden:TRUE];
[myTableView reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in arr)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the naames.
{
[tableData addObject:name];
}
}
counter++;
[pool release];
}
[myTableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)s earchBar
{
// if a valid search was entered but the user wanted to cancel, bring back the main list content
[tableData removeAllObjects];
@try {
[myTableView reloadData];
}
@catch(NSException *e){
}
[sBar resignFirstResponder];
sBar.text = @ "";
}
// called when Search (in our case “Done”) button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[myTableView reloadData];
}
Declare myTableView in didLoad:
< p>
myTableView.frame=CGRectMake(550, 00, 220,400);
Look at the screenshot,
In the first image data, however, tableView will not increase it In the second image, there is only 1 data, but the height is still being fixed.
p>
CGRect frame = myTableView. frame;
frame.size.height = MIN(40 * [tableData count], 400); // 400 is the maximum height that the table view can have. You can change it to whatever you like
myTableView.frame = frame;
If this is useful to you, please let me know.
I created an iPad application in which I use searchBar , the searchBar data comes from the database, I store it in an array named tableData.
After this I pass this tabledata array to the tableView named myTableView, I am in the didLoad method The height of the table view is declared.
I want the height of the tableView to be dynamic according to the number of elements in the table,
This is a code snippet,
p>
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar's cancel button while in edit mode
sBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[tableData removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
sBar.showsCancelButton = NO;
[myTableView setHidden:TRUE];
}
- (void)searchBar:( UISearchBar *)searchBar textDidChang e:(NSString *)searchText
{
[myTableView setHidden:FALSE];
[tableData removeAllObjects];// remove all data that belongs to previous search
myTableView.backgroundColor= [[UIColor alloc]initWithRed:4.0 / 255 green:24.0 / 255 blue:41.0 / 255 alpha:1.0];
[self.view bringSubviewToFront:myTableView];
if([searchText isEqualToString:@""]||searchText==nil){
[myTableView setHidden:TRUE];
[myTableView reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in arr)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the naames.
{
[tableData addObject:name];
}
}
counter++;
[pool release];
}
[myTableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
// if a valid search was entered but the user wanted to cancel, bring back the main list content
[tableData removeAllObjects ];
@try{
[myTableView reloadData];
}
@catch(NSException *e){
}
[sBar resignFirstResponder];
sBar.text = @"";
}
// called when Search (in our case “Done”) button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[myTableView reloadData];
}
Declare myTableView in didLoad:
myTableView.frame=CGRectMake(550, 00, 220,400);
Look at the screenshot,
in the first In the image data, however, the tableView will not increase its size, and in the second image there is only 1 data, but the height is still being repaired.
As Atulkumar V. Jain said Try: in – (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText method to adjust the size of the table frame as follows:
CGRect frame = myTableView .frame;
frame.size.height = MIN(40 * [tableData count], 400); // 400 is the maximum height that the table view can have. You can change it to whatever you like
myTableView.frame = frame;
If this is useful to you, please let me know.