ios tableview不能显示最下面一个cell

今天项目中又出现了之前遇到的一个问题,虽说之前解决的,现在还是花了一些时间才想起来解决办法,故在此记录一下。

之前遇到的问题是用storyboard创建了一个UIViewController,然后再UIViewController中添加一个UITableView,在真机调试时,总是出现最后一个cell不能显示,拼命往上拽是可以看到的,但是一松手有下去不见了。虽然在代码中修改了tableview的frame属性,打印出来tableview的高度也是对的,但是还是会出现这问题。

今天遇到的问题是在xib中创建的界面,问题还是一模一样。

原因:这是ios7及7.0以后遇到的一个页面布局的问题,苹果默认选中了autolayout属性,就是自动布局,所以才造成的这个bug

解决办法:

xib文件

打开xib文件,然后在右边的属性表中点击最左边的属性页面,如图1。

在Interface Builder Document中最下面有个选项 Use Autolayout,默认是选中的,去掉就好了。

一定要注意是修改最左边的属性页(file inspector),不是左边第三个(size inspector)。

然后就是可以同过修改tableview的frame来控制tableview的位置了。


storyboard中的也是一样的。

同样是打开storyboard文件,然后修改最左边的file inspector属性页。找到Use Autolayout,去掉选中即可。下一步就是设置控件的frame啦,相信大家都会的。


好的,以下是一个简单的iOS Tableview代码示例: 首先,在你的ViewController.h文件中添加UITableViewDelegate和UITableViewDataSource协议: ``` @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> ``` 然后,在你的ViewController.m文件中,初始化一个UITableView,并实现UITableViewDataSource和UITableViewDelegate协议的方法: ``` - (void)viewDidLoad { [super viewDidLoad]; UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; tableView.delegate = self; tableView.dataSource = self; [self.view addSubview:tableView]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; // 返回10行 } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = [NSString stringWithFormat:@"Row %ld", (long)indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSLog(@"Selected row %ld", (long)indexPath.row); } ``` 这个示例会在你的ViewController上创建一个UITableView,其中包含10行简单的文本。当你点击任何一行时,它会在控制台上打印所选行的索引。 希望这可以帮助你开始实现你自己的表视图!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值