UITableView 总结 2023

@property (nonatomic, strong) UITableView *jk_tableView;

- (UITableView *)jk_tableView {
    if (!_jk_tableView) {
        _jk_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _jk_tableView.delegate = self;
        _jk_tableView.dataSource = self;
        _jk_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kAdaptedFloat(20))];
        _jk_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _jk_tableView.backgroundColor = [UIColor clearColor];
        _jk_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _jk_tableView.showsVerticalScrollIndicator = NO;
        _jk_tableView.showsHorizontalScrollIndicator = NO;
        if(@available(iOS 15.0,*)){_jk_tableView.sectionHeaderTopPadding=0;}
    }
    return _jk_tableView;
}

UITableViewAutomaticDimension

@interface <#class name#> () 

@end
#pragma mark ------- UITableViewDelegate -------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kAdaptedFloat(50);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UITableViewCell cellForTableView:tableView];
}
//根据cell, 刷新cell
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.tableView reloadRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationNone];

// 用storyboard创建UITableViewController
+ (instancetype)gh_sweepermanagement {
    return [[UIStoryboard storyboardWithName:NSStringFromClass(self) bundle:nil] instantiateViewControllerWithIdentifier:NSStringFromClass(self)];
}

//tableview既要有左滑删除又要有全选删除 didSelectRowAtIndexPath不能有 [tableView deselectRowAtIndexPath:indexPath animated:YES]
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.collectTableView.editing) {
        return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    }else{
        return UITableViewCellEditingStyleDelete;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *GHHomePageDVcellID = @"GHHomePageDVcellID";
    GHHomePageDVcell *cell = [tableView dequeueReusableCellWithIdentifier:GHHomePageDVcellID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"GHHomePageDVcell" owner:nil options:nil] firstObject];
    }
    
    return cell;
}

######tableViewCell

//cell选中没有效果
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[tableView deselectRowAtIndexPath:indexPath animated:NO];
//改变全选cell,左边的选中颜色
cell.tintColor = [UIColor orangeColor];

//改变cell的背景颜色(选择cell的颜色)
UIView *backgroundViews = [[UIView alloc]initWithFrame:cell.frame];
backgroundViews.backgroundColor = [UIColor whiteColor];
[cell setSelectedBackgroundView:backgroundViews];
- (void)layoutSubviews
{
    [super layoutSubviews];

    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIControl * _Nonnull control, NSUInteger idx, BOOL * _Nonnull stop) {

        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]) {

            for (UIView *view in control.subviews) {

                if ([view isKindOfClass:[UIImageView class]]) {

                    UIImageView *imageView = (UIImageView *)view;
                    imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, 12, 12);

                    if (self.selected) {
                        //设置选中时的照片
                        imageView.image = [UIImage imageNamed:@"selcect_share_icon"];
                    } else {
                        //设置未选中时的照片
                        imageView.image = [UIImage imageNamed:@"noselcect_share_icon"];
                    }
                }
            }
        }
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIControl * _Nonnull control, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]) {
            
            for (UIView *view in control.subviews) {
                
                if ([view isKindOfClass:[UIImageView class]]) {
                    
                    UIImageView *imageView = (UIImageView *)view;
                    imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, 12, 12);
                    
                    if (self.selected) {
                        //设置选中时的照片
                        imageView.image = [UIImage imageNamed:@"selcect_share_icon"];
                    } else {
                        //设置未选中时的照片
                        imageView.image = [UIImage imageNamed:@"noselcect_share_icon"];
                    }
                }
            }
        }
    }];
}
@property (nonatomic, strong) NSMutableArray *deleteArray;
self.deleteArray = [NSMutableArray array];

#pragma mark ------- Click Methods -------

- (void)guanliBtnClick {
    self.tableView.editing = !self.tableView.editing;
    
    if (self.tableView.isEditing == YES) {
        [self.deleteArray removeAllObjects];
        [self.guanliBtn setTitle:@"取消" forState:0];
        [self.quanXuanMainView.quanxuanButton setTitle:@"全选" forState:0];
        [self.quanXuanMainView.deleteButton setTitle:[NSString stringWithFormat:@"删除(%ld)", self.deleteArray.count];
        
        self.quanXuanMainView.hidden = NO;
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.view);
            make.bottom.equalTo(self.quanXuanMainView.mas_top);
        }];
        
    } else {
        [self.deleteArray removeAllObjects];
        [self.guanliBtn setTitle:@"管理" forState:0];
        self.quanXuanMainView.hidden = YES;
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.equalTo(self.view);
        }];
    }
}

// 全选
- (void)quanxuanButtonClick {
    if ([self.quanXuanMainView.quanxuanButton.titleLabel.text isEqual:@"全选"]) {
        [self.quanXuanMainView.quanxuanButton setTitle:@"取消全选" forState:0];
        self.deleteArray = [NSMutableArray arrayWithArray:self.tableData];
        [self.quanXuanMainView.deleteButton setTitle:[NSString stringWithFormat:@"删除(%ld)", self.deleteArray.count] forState:0];
        for (int i = 0; i

你可能感兴趣的:(OC,ios,xcode,objective-c)