UITableviewCell允许自定义cell进行重用,步骤是这样的:
1.storyboard创建tableView,连接委托数据源,添加cell,设置identifier,并添加定义控件
2.创建cell子类,绑定storyboard里地cell,并拖拽属性
3.在视图控制器里面添加委托和数据源,在cellForRowAtIndexPath里添加[tableView dequeueReusableCellWithIdentifier:@"identifier"]。
这样就行了,然而我这几天在一个项目里面通过这种方法添加自定义cell,却发现tableView在加载cell的时候并没有显示出cell类似下图而且更奇怪的是,如果我设置rowforsection为多个的时候,往下滑动,有些cell却能显示,有些却不能,如下如图:之后我重新创建了一个新的工程实践自定义cell,是成功的。请问这是为何?下面是viewcontroller的代码:
static const CGFloat CHKUserInfoMainViewHeaderImageHeight = 250.0f;
//static const CGFloat CHKUserInfoMainViewBarHeight = 64.0f;@interface CHKUserInfoMainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *tableHeaderView;
@property (weak, nonatomic) IBOutlet UIImageView *tableHeaderBackground;
@property (weak, nonatomic) IBOutlet UIImageView *tableHeaderIcon;
@property (weak, nonatomic) IBOutlet UILabel *tableHeaderUserName;@property (nonatomic) BOOL isNavigationBarColorAlpha1;@end@implementation CHKUserInfoMainViewController- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[_tableHeaderView setFrame:CGRectMake(0, 0, self.view.bounds.size.width, CHKUserInfoMainViewHeaderImageHeight)];
_tableHeaderBackground.contentMode = UIViewContentModeScaleAspectFill;
_tableView.tableHeaderView = _tableHeaderView;
_isNavigationBarColorAlpha1 = NO;
}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!_isNavigationBarColorAlpha1) {
[self.navigationController.navigationBar CHK_setNavigationBarBackgroundColor:[CHKNavigationBarColor colorWithAlphaComponent:0.0f]];
[self.navigationController.navigationBar CHK_hideUnderline:YES];
}
}
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CHKToBeTopUsrInfoBeanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"beanCell"];
return cell;
}-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY <= 0) {
[CHKLayoutConstaint changeConstraintWithView:_tableHeaderBackground constraintStyle:HKLayoutConstraintsTop constant:offsetY - 1];
}else if (offsetY >= 125.0f) {
if (!_isNavigationBarColorAlpha1) {
[UIView animateKeyframesWithDuration:0.2f delay:0.0f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[self.navigationController.navigationBar CHK_barBackgroundView].backgroundColor = [CHKNavigationBarColor colorWithAlphaComponent:1.0f];
} completion:nil];
_isNavigationBarColorAlpha1 = YES;
}
}else if (offsetY <= 125.0f) {
if (_isNavigationBarColorAlpha1) {
[UIView animateKeyframesWithDuration:0.2f delay:0.0f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[self.navigationController.navigationBar CHK_barBackgroundView].backgroundColor = [CHKNavigationBarColor colorWithAlphaComponent:0.0f];
} completion:nil];
_isNavigationBarColorAlpha1 = NO;
}
}
}
1.storyboard创建tableView,连接委托数据源,添加cell,设置identifier,并添加定义控件
2.创建cell子类,绑定storyboard里地cell,并拖拽属性
3.在视图控制器里面添加委托和数据源,在cellForRowAtIndexPath里添加[tableView dequeueReusableCellWithIdentifier:@"identifier"]。
这样就行了,然而我这几天在一个项目里面通过这种方法添加自定义cell,却发现tableView在加载cell的时候并没有显示出cell类似下图而且更奇怪的是,如果我设置rowforsection为多个的时候,往下滑动,有些cell却能显示,有些却不能,如下如图:之后我重新创建了一个新的工程实践自定义cell,是成功的。请问这是为何?下面是viewcontroller的代码:
static const CGFloat CHKUserInfoMainViewHeaderImageHeight = 250.0f;
//static const CGFloat CHKUserInfoMainViewBarHeight = 64.0f;@interface CHKUserInfoMainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *tableHeaderView;
@property (weak, nonatomic) IBOutlet UIImageView *tableHeaderBackground;
@property (weak, nonatomic) IBOutlet UIImageView *tableHeaderIcon;
@property (weak, nonatomic) IBOutlet UILabel *tableHeaderUserName;@property (nonatomic) BOOL isNavigationBarColorAlpha1;@end@implementation CHKUserInfoMainViewController- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[_tableHeaderView setFrame:CGRectMake(0, 0, self.view.bounds.size.width, CHKUserInfoMainViewHeaderImageHeight)];
_tableHeaderBackground.contentMode = UIViewContentModeScaleAspectFill;
_tableView.tableHeaderView = _tableHeaderView;
_isNavigationBarColorAlpha1 = NO;
}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!_isNavigationBarColorAlpha1) {
[self.navigationController.navigationBar CHK_setNavigationBarBackgroundColor:[CHKNavigationBarColor colorWithAlphaComponent:0.0f]];
[self.navigationController.navigationBar CHK_hideUnderline:YES];
}
}
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CHKToBeTopUsrInfoBeanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"beanCell"];
return cell;
}-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY <= 0) {
[CHKLayoutConstaint changeConstraintWithView:_tableHeaderBackground constraintStyle:HKLayoutConstraintsTop constant:offsetY - 1];
}else if (offsetY >= 125.0f) {
if (!_isNavigationBarColorAlpha1) {
[UIView animateKeyframesWithDuration:0.2f delay:0.0f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[self.navigationController.navigationBar CHK_barBackgroundView].backgroundColor = [CHKNavigationBarColor colorWithAlphaComponent:1.0f];
} completion:nil];
_isNavigationBarColorAlpha1 = YES;
}
}else if (offsetY <= 125.0f) {
if (_isNavigationBarColorAlpha1) {
[UIView animateKeyframesWithDuration:0.2f delay:0.0f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[self.navigationController.navigationBar CHK_barBackgroundView].backgroundColor = [CHKNavigationBarColor colorWithAlphaComponent:0.0f];
} completion:nil];
_isNavigationBarColorAlpha1 = NO;
}
}
}
CHKToBeTopUsrInfoBeanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"beanCell"];
return cell;
}你重写了dequeueReusableCellWithIdentifier方法吗?
CHKToBeTopUsrInfoBeanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"beanCell"];
return cell;
}你把cell用nslog打印出来看下,我看你的空的那些cell根本就是nil