UITableView,仅显示带有数据的UITableViewCells [英] UITableView, only showing UITableViewCells with data

查看:39
本文介绍了UITableView,仅显示带有数据的UITableViewCells的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UITableViewCell,它占据了屏幕的一部分.但是,当只有一行数据时,我希望UITableView(随IB一起插入)缩小并仅显示一行.UITableView是否可以重绘自身?谢谢.

I have a custom UITableViewCell that takes up a portion of the screen. However, when there is only one row of data, I'd like the UITableView (that I dropped in with IB), to shrink and only show the one row. Is there a way for the UITableView to redraw itself? Thanks.

推荐答案

您可以使用以下代码设置tableView的高度:

You can set the tableView height using the following code:

CGRect newFrame = tableView.frame;

frame.size.height = newHeight;

tableView.frame = newFrame;

如果要设置高度变化的动画,则需要执行以下操作:

If you want to animate the height change you need to do:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.5];

CGRect newFrame = tableView.frame;

frame.size.height = newHeight;

tableView.frame = newFrame;

[UIView commitAnimations];

您可以将上述所有代码放入一个方法中,当 numberOfRowsInSection 数据源方法返回值1时,可以调用该方法.

You can put all of the above code in a method which you can call when your numberOfRowsInSection data source method returns a value of 1.

这篇关于UITableView,仅显示带有数据的UITableViewCells的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆