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

ios – 无法在目标c中设置UITableView动态的高度,第1张

概述我创建了一个iPad应用程序,其中我使用searchBar,searchBar数据来自数据库,我将它存储在一个名为tableData的数组中. 在此之后我将这个tabledata数组传递给名为myTableView的tableView,我在didLoad方法中声明了表视图的高度. 我希望tableView的高度根据表中元素的数量是动态的, 这是代码片段, - (void)searchBarText 我创建了一个iPad应用程序,其中我使用searchbar,searchbar数据来自数据库,我将它存储在一个名为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个数据,但高度仍在修复.

解决方法 正如Atulkumar V. Jain所说的尝试:在 – (voID)searchbar:(UISearchbar *)searchbar textDIDChange:(Nsstring *)searchText方法调整表格框架的大小如下:

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 likemytableVIEw.frame = frame;

如果这对您有用,请告诉我.

总结

以上是内存溢出为你收集整理的ios – 无法在目标c中设置UITableView动态的高度全部内容,希望文章能够帮你解决ios – 无法在目标c中设置UITableView动态的高度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1017500.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存