DZNEmptyDataSet的几个坑

DZNEmptyDataSet是一个常用的代码库,用来处理列表为空时的显示,例如UITableView或UICollectionView为空时可以显示一个自定义的view,下面总结3个需要注意规避的的坑
1、自定义view的高度控制问题:需要给自定义view添加高度约束条件
2、自定义view的背景色问题:
在- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView中指定背景色
3、列表从有内容变成内容为空,界面滚动偏移的问题:
需要在- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView 中重新设置下偏移值

#pragma mark - DZNEmptySetDataSource
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {

    UIView *emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 260)];
    emptyView.backgroundColor = [UIColor clearColor];
     [emptyView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[emptyView(260)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(emptyView)]];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-205)/2, 0, 205, 154)];
    imageView.image = [UIImage imageNamed:@"search_no_image"];
    [emptyView addSubview:imageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth-205)/2, CGRectGetMaxY(imageView.frame)+20, 205, 22)];
    label.text = @"找不到符合条件的照片";
    label.textColor = HexRGB(0x999999);
    label.font = SXFont(16);
    label.textAlignment = NSTextAlignmentCenter;
    [emptyView addSubview:label];
    
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake((kScreenWidth-100)/2, CGRectGetMaxY(label.frame)+30, 100, 34)];
    [searchBtn setTitle:@"重新筛选" forState:UIControlStateNormal];
    [searchBtn setTitleColor:HexRGB(0x6D41CC) forState:UIControlStateNormal];
    [searchBtn.titleLabel setFont:SXBoldFont(14)];
    searchBtn.layer.cornerRadius = 17;
    searchBtn.layer.borderWidth = SXBorderWidth;
    searchBtn.layer.borderColor = HexRGB(0x6D41CC).CGColor;
    searchBtn.layer.masksToBounds = YES;
    [searchBtn addTarget:self action:@selector(onClickSearch:) forControlEvents:UIControlEventTouchUpInside];
    [emptyView addSubview:searchBtn];
    
    return emptyView;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView*)scrollView {
    return 0;
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIColor whiteColor];
}

- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView {
    [self.collectionView setContentOffset:CGPointMake(0, -self.collectionView.contentInset.top)];
}

你可能感兴趣的:(DZNEmptyDataSet的几个坑)