찾다

 >  Q&A  >  본문

objective-c - 自定义UITableView dataSource的cellForRowAtIndexPath方法没被执行

自定义一个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;
}
黄舟黄舟2757일 전785

모든 응답(2)나는 대답할 것이다

  • phpcn_u1582

    phpcn_u15822017-05-02 09:40:00

    tableview를 커스터마이징할 때,layoutsubviews가 다시 작성되어서 dataSource 메소드가 실행되지 않는 것을 발견했습니다.layoutsubviews 메소드를 주석 처리하면 실행될 수 있는데 이유를 모르겠습니다. 댓글 외에 해결 방법은 없을까요?

    회신하다
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-02 09:40:00

    layoutsubview를 다시 작성할 때 상위 클래스의layoutsubviews[슈퍼 레이아웃subviews]를 호출하지 않았습니다.

    회신하다
    0
  • 취소회신하다