为了账号安全,请及时绑定邮箱和手机立即绑定

动态更改UITableView高度

动态更改UITableView高度

慕容708150 2019-08-30 15:56:51
我想基于其单元格高度的总和从另一个viewcontroller更改tableview的高度,因为它们是动态的。它可能吗?谢谢添加在:我基本上拥有一个UserProfileViewController,它在屏幕的一半上有一个容器视图。在那里我添加了不同的其他viewcontrollers:在wall按钮的情况下,这是我添加viewcontroller的方式,它是后续的tableview:- (IBAction)wallButtonPressed:(id)sender{    //Check if there is an instance of the viewcontroller we want to display. If not make one and set it's tableview frame to the container's view bounds    if(!_userWallViewController) {        self.userWallViewController = [[WallViewController alloc] init];//        self.userWallViewController.activityFeedTableView.frame = self.containerView.bounds;    }    [self.userWallViewController.containerView addSubview:self.userWallViewController.activityFeedTableView];    //If the currentviewcontroller adn it's view are already added to the hierarchy remove them    [self.currentViewController.view removeFromSuperview];    [self.currentViewController removeFromParentViewController];    //Add the desired viewcontroller to the currentviewcontroller    self.currentViewController = self.userWallViewController;    //Pass the data needed for the desired viewcontroller to it's instances    self.userWallViewController.searchURLString = [NSString stringWithFormat:@"event/user/%@/", self.userID];    self.userWallViewController.sendCommentURLString = [NSString stringWithFormat:@"event/message/%@", self.userID];    [self.userWallViewController.activityFeedTableView reloadData];    self.userWallViewController.totalCellHeight = ^(float totalCellHeight){在那个viewcontroller中,这是我初始化tableview的地方:-(UITableView *)activityFeedTableView{    if (!_activityFeedTableView) {        _activityFeedTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 850.0) style:UITableViewStylePlain];    }    return _activityFeedTableView;}我正在计算单元格高度的总和,问题是在调用te tableview的getter之后调用单元格的高度方法。因此,我需要某种方式来了解何时为所有细胞完成细胞的高度方法,然后我可以调整我的tableview。谢谢
查看完整描述

3 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

通过xib或storyboard创建您的单元格。给出它的出口内容。现在在CellForRowAtIndexPath中调用它。例如。如果要根据Comment的标签文本设置单元格高度。 在此输入图像描述


所以设置你的commentsLbl.numberOfLine = 0;


所以设置你的commentsLbl.numberOfLine = 0;


然后在ViewDidLoad中


 self.table.estimatedRowHeight = 44.0 ;

self.table.rowHeight = UITableViewAutomaticDimension;

现在


-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return UITableViewAutomaticDimension;}


查看完整回答
反对 回复 2019-08-30
?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

这里的许多答案都不尊重桌子的变化或太复杂。使用autolayout时,使用UITableView正确设置的子类intrinsicContentSize是一个更容易的解决方案。不需要高度限制等。


class UIDynamicTableView: UITableView

{

    override var intrinsicContentSize: CGSize {

        self.layoutIfNeeded()

        return CGSize(width: UIViewNoIntrinsicMetric, height: self.contentSize.height)

    }


    override func reloadData() {

        super.reloadData()

        self.invalidateIntrinsicContentSize()

    }

UIDynamicTableView在接口构建器中设置TableView的类并观察魔法,因为此TableView将在调用后更改其大小reloadData()。


查看完整回答
反对 回复 2019-08-30
  • 3 回答
  • 0 关注
  • 606 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信