Я создал приложение для iPad, в котором я использовал searchBar, в searchBar данные поступают из базы данных, и я сохраняю их в массиве с именем tableData.
После этого я передаю этот массив tabledata в tableViewназывается myTableView, я объявил высоту табличного представления в методе didLoad.
Я хочу, чтобы высота tableView была динамической в соответствии с количеством элементов в таблице,
здесь приведен фрагмент кода,
- (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 textDidChange:(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];
}
объявление myTableView в didLoad:
myTableView.frame=CGRectMake(550, 00, 220,400);
см. Скриншот, в первом изображении данные есть, но tableView не увеличивает его размер, а во втором изображении есть только 1 данные, но высотавсе еще исправлено.