Ios 无法在目标c中设置UITableView动态的高度

Ios 无法在目标c中设置UITableView动态的高度,ios,cocoa-touch,uitableview,Ios,Cocoa Touch,Uitableview,我创建了一个iPad应用程序,其中我使用了搜索栏,搜索栏中的数据来自数据库,我将它存储在一个名为tableData的数组中 在此之后,我将这个tabledata数组传递给名为myTableView的tableView,我在didLoad方法中声明了表视图的高度 我希望tableView的高度根据表中元素的数量而动态变化 下面是代码片段 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { // only show

我创建了一个iPad应用程序,其中我使用了搜索栏,搜索栏中的数据来自数据库,我将它存储在一个名为tableData的数组中

在此之后,我将这个tabledata数组传递给名为myTableView的tableView,我在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];
}
在didLoad中声明myTableView:

myTableView.frame=CGRectMake(550, 00, 220,400);
看截图, 在第一个图像中,数据存在,但tableView不会增加其大小,在第二个图像中,只有1个数据存在,但高度仍然是固定的

您可以使用 UITableView委托方法,-(CGFloat)tableView:(UITableView*)tableView-heightForRowAtIndexPath:(nsindepath*)indepath

在这里面,您可以在
-(void)searchBar:(UISearchBar*)searchBar textdichange:(NSString*)searchText
方法中动态设置行的高度根据数组中存在的记录数更改tableview的高度。由于您可能知道行的高度,因此可以使用以下行动态更改tableview的高度

tableview.frame.size.height = ROW_HEIGHT*NO_OF_DATA_IN_ARRAY

希望这能解决你的问题。

正如Atulkumar V.Jain所说,尝试以下方法:在
-(void)搜索栏中:(UISearchBar*)搜索栏文本DIDCHANGE:(NSString*)搜索文本
方法调整表框大小,如下所示:

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;

让我知道这是否适合你。

我认为你问题的标题是错误的。据我所知,您希望更改tableView的高度,而不是
UISearchBar
-我错了吗?我尝试了
myTableView.frame.size.height=40*[tableData count],但它给我以下错误
表达式不可赋值
。。。。有其他选择吗?首先,你必须将旧的rect保存在一个变量中,然后更新高度,并将更新后的CGRect重新分配给tableview;然后更改矩形的高度。然后将rect重新分配给tableview,如下所示。frame=rect;。。。。。。。。希望这能解决你的问题。