自定义一个tableview,但是主控制器中实现之后,没有加载cell,打断点,发现cellForRowAtIndexPath:(NSIndexPath *)indexPath方法没有进入,就是没有被调用。下面是自定义UITableView的代码:
.h文件
#import <UIKit/UIKit.h>
#import "GCProductModel.h"
@interface RecommendComView : UITableView
@property(nonatomic,strong) NSMutableArray *productsArr;
@property (nonatomic,strong) UIButton *unfoldBtn;
-(void) setButtonUnFoldStatus;
-(void)setButtonFoldStatus;
@end #import "RecommendComView.h"
.m文件
#import "UIColor+DecColor.h"
#define DEVICE_WIDTH [UIScreen mainScreen].bounds.size.width
#define DEVICE_HEIGHT [UIScreen mainScreen].bounds.size.height
static const CGFloat viewHeight = 44.0f;
static const CGFloat navigationHeight = 64.0f;
@interface RecommendComView()
@property (nonatomic,strong) UILabel *desLbl;
@property (nonatomic,strong) UIView *headerView;
@end
@implementation RecommendComView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
if(self=[super initWithFrame:frame style:style])
{
_unfoldBtn = [[UIButton alloc]init];
_desLbl = [[UILabel alloc] init];
//设置btn
[self setButtonUnFoldStatus];
//设置label
_desLbl.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13.0f];
_desLbl.textColor = [UIColor decColorWithRed:46 green:46 blue:46 alpha:1];
[self setFrame:CGRectMake(0, navigationHeight, DEVICE_WIDTH, viewHeight*4)];
//设置headerview
_headerView = [[UIView alloc]initWithFrame:CGRectMake(0,0, DEVICE_WIDTH, viewHeight)];
UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
// 毛玻璃视图
UIVisualEffectView * effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
effectView.backgroundColor = [UIColor decColorWithRed:252 green:252 blue:252 alpha:0.08f];
//添加到要有毛玻璃特效的控件中
effectView.frame = self.bounds;
[self addSubview:effectView];
//设置底层透明度
self.backgroundColor = [UIColor decColorWithRed:255 green:255 blue:255 alpha:0.85];
self.scrollEnabled = NO;
self.tableHeaderView = _headerView;
}
return self;
}
-(void) layoutSubviews
{
//设置button的Frame
[_unfoldBtn setFrame:CGRectMake(DEVICE_WIDTH-15-26, 15.5, 26, 13)];
[self.headerView addSubview:_unfoldBtn];
[self.headerView addSubview:_desLbl];
}
#pragma mark - setButtonStatus
-(void) setButtonUnFoldStatus
{
[_unfoldBtn setTitle:@"展开" forState:UIControlStateNormal];
_unfoldBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13.0f];
[_unfoldBtn setTitleColor:[UIColor decColorWithRed:255 green:100 blue:100 alpha:1] forState:UIControlStateNormal];
}
-(void)setButtonFoldStatus
{
[_unfoldBtn setTitle:@"收起" forState:UIControlStateNormal];
_unfoldBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13.0f];
[_unfoldBtn setTitleColor:[UIColor decColorWithRed:255 green:100 blue:100 alpha:1] forState:UIControlStateNormal];
}
-(void) setProductsArr:(NSMutableArray *)productsArr
{
_productsArr = productsArr;
_desLbl.text = [NSString stringWithFormat:@"共有%lu件商品" ,(unsigned long)_productsArr.count];
[_desLbl sizeToFit];
//设置商品图标
if(_productsArr.count<3)
{
for(int i = 0;i<_productsArr.count;i++)
{
GCProductModel *product = _productsArr[i];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(15+36*i, 6, 32, 32)];
imgView.image = [UIImage imageNamed:product.coverImg];
imgView.backgroundColor = [UIColor redColor];
[_headerView addSubview:imgView];
}
[_desLbl setFrame:CGRectMake(15+36*_productsArr.count+4, 15.5, _desLbl.frame.size.width, _desLbl.frame.size.height)];
}else
{
for(int i = 0;i<3;i++)
{
GCProductModel *product = _productsArr[i];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(15+36*i, 6, 32, 32)];
imgView.image = [UIImage imageNamed:product.coverImg];
imgView.backgroundColor = [UIColor redColor];
[_headerView addSubview:imgView];
}
[_desLbl setFrame:CGRectMake(15+36*3+4, 15.5, _desLbl.frame.size.width, _desLbl.frame.size.height)];
}
}
@end
自定义cell
#import "CombProductCell.h"
#import "UIColor+DecColor.h"
#define DEVICE_WIDTH [UIScreen mainScreen].bounds.size.width
#define DEVICE_HEIGHT [UIScreen mainScreen].bounds.size.height
static const CGFloat ImgLength = 32.0f;
@interface CombProductCell()
@property (nonatomic,strong) UIImageView *imgView;
@property (nonatomic,strong) UILabel *lbl;
@end
@implementation CombProductCell
-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
_imgView = [[UIImageView alloc]init];
_lbl = [[UILabel alloc]init];
_lbl.textColor = [UIColor decColorWithRed:46 green:464 blue:46 alpha:1];
_lbl.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16.0f];
_lbl.textAlignment = NSTextAlignmentLeft;
[self addSubview:_imgView];
[self addSubview:_lbl];
//[self setFrame:CGRectMake(0, 0, DEVICE_WIDTH, 44)];
}
return self;
}
- (void) layoutSubviews
{
[_imgView setFrame:CGRectMake(15, 6, ImgLength, ImgLength)];
[_lbl setFrame:CGRectMake(55,13,DEVICE_WIDTH-55-19.5,16)];
}
主控制器
-(void) createRecommendComView
{
/*
商品组合View组合直接放在ViewController中,位置就在NavigationBar下面,但是在mainScrollView中流出了空白位置
*/
//_recommendComView = [[RecommendComView alloc]initWithFrame:CGRectMake(0, NAVIBAR_HEIGHT, DEVICE_WIDTH, TOPBAR_HEIGHT)];
_recommendComView = [[RecommendComView alloc] initWithFrame:CGRectMake(0, NAVIBAR_HEIGHT, DEVICE_WIDTH, TOPBAR_HEIGHT+CELL_HEIGHT*3) style:UITableViewStylePlain];
[self setCombsProductData];
//设置组合_recommendComView页面的tableview的代理
_recommendComView.delegate = self;
_recommendComView.dataSource= self;
[_recommendComView reloadData];
//添加点击事件
[_recommendComView.unfoldBtn addTarget:self action:@selector(clickedUnFoldCombProductViewBtn) forControlEvents:UIControlEventTouchUpInside];
_foldFlag = TRUE;
[self.view addSubview:_recommendComView];
[_recommendComView registerClass:[CombProductCell class] forCellReuseIdentifier:KEY_RECOMMEND_CELL_TABLEVIEW_IDENTIFIER];
}
#pragma mark:tableView代理和DataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CombProductCell *cell = [tableView dequeueReusableCellWithIdentifier:KEY_RECOMMEND_CELL_TABLEVIEW_IDENTIFIER];
if(cell==nil)
{
cell = [[CombProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KEY_RECOMMEND_CELL_TABLEVIEW_IDENTIFIER];
}
cell.product = _combProuctsArr[indexPath.row];
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return CELL_HEIGHT;
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _combProuctsArr.count;
//return 3;
}
J'ai découvert que lors de la personnalisation de la tableview, layoutsubviews était réécrit, donc la méthode dataSource n'était pas exécutée si je commentais la méthode layoutsubviews, elle pouvait être exécutée, mais je ne sais pas pourquoi ? Y a-t-il une solution à part les commentaires ?
Lorsque vous avez réécrit layoutsubviews, vous n'avez pas appelé les layoutsubviews de la classe parent [super layoutsubviews]