纯代码编写UITableView和UICollectionView中的坑
来源:互联网 发布:mac上的翻墙工具 编辑:程序博客网 时间:2024/06/07 06:02
当你用纯代码编写UITableView或者UICollectionView时,这里只说UITableView,UICollectionView类似,调用方法->
dequeueReusableCellWithIdentifier:forIndexPath:时发现崩溃了,查看控制台输出的信息如下:
根据提示要求你添加nib文件或者注册一个类,官方文档也做了详细的说明:
文档给了重要提示...好吧,所以你应该添加下面这一句:
示例代码如下:
#import "MainViewController.h"#import "MainTableViewCell.h"// 获取屏幕高度#define kDeviceHeight [UIScreen mainScreen].bounds.size.height// 获取屏幕宽度#define kDeviceWidth [UIScreen mainScreen].bounds.size.width@interface MainViewController ()@property (nonatomic,strong) UITableView *mainTableV;@end@implementation MainViewController- (void)viewDidLoad { [super viewDidLoad]; _mainTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, [[UIApplication sharedApplication] statusBarFrame].size.height, kDeviceWidth, kDeviceHeight) ]; _mainTableV.delegate = self; _mainTableV.dataSource = self; [_mainTableV registerClass:[MainTableViewCell class] forCellReuseIdentifier:@"Cell"]; _mainTableV.scrollEnabled = YES; _mainTableV.userInteractionEnabled = YES; [self.view addSubview:_mainTableV];}#pragma mark - UITableViewDelegate methods- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 50;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; if (cell == nil) { cell = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } [cell setTheImage:nil andTitle:@"123"]; return cell;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 50;}好吧,没有仔细看文档,掉坑里了~
0 0
- 纯代码编写UITableView和UICollectionView中的坑
- UICollectionView纯代码
- UICollectionView纯代码
- UITableView 和 UICollectionView
- UICollectionView Section 纯代码代码控制 HeaderView和FooterView的显示,uicollectionview
- 如何纯代码给UICollectionView添加HeaderView和FooterView
- Swift纯代码走进UICollectionView
- UIScrollView嵌套UITableView和UICollectionView
- UICollectionView Section 纯代码代码控制 HeaderView和FooterView的显示
- iOS UICollectionView 纯代码,无xib
- iOS 开发 UICollectionView纯代码设置
- iOS 开发 UICollectionView纯代码设置
- iOS 纯代码创建UICollectionView【原创】
- iOS8自动调整UITableView和UICollectionView布局
- iOS8自动调整UITableView和UICollectionView布局
- UITableView和UICollectionView的cell重用问题
- UICollectionView详解和UITableView的区别
- UITableView和UICollectionView列表出现重叠问题
- Google 的开源技术protobuf 简介与例子
- Js数组的操作push,pop,shift,unshift等方法
- 我的第一篇博客
- mavlink解析
- 从一个xib文件中加载另一个xib文件
- 纯代码编写UITableView和UICollectionView中的坑
- 2、BI开发环境
- IOS中获取各个文件的目录路径的方法和NSFileManager类
- UIViewController
- Ubuntu系统下bash和dash的区别(修改默认sh为bash)
- JS技巧总结
- matlab 计算大型距离方阵,distance matrix
- 文章标题 关于MySQL卸载几点注意事项
- 如何对待Release与Debug运行结果不一致