iOS UITableView相关

本文详细介绍了 iOS 开发中 UITableView 的使用技巧,包括初始化、设置行为、注册自定义 cell、复用 cell、实现右滑删除等功能,并提供了滚动时 cell 动画效果的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 初始化

#pragma mark - Getter

- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView = [UITableView newAutoLayoutView];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        
        [self.view addSubview:_myTableView];
    }
    
    return _myTableView;
}

2. 设置UITableView的行为

- (void)initRootView {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = kColor(0xeeeeee);

    //去掉多余的分割线
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.backgroundColor = kColor(0xeeeeee);
    self.myTableView.tableFooterView = [UIView new];
    
    //设置cell高度自适应 不用计算cell高度了
    self.myTableView.rowHeight = UITableViewAutomaticDimension;
    self.myTableView.estimatedRowHeight = 44;
    
    //添加下拉刷新 上拉加载 用到 MJRefresh
    [self addHeaderFooterView];
}

// 添加下拉刷新头部控件
- (void)addHeaderFooterView {
    __unsafe_unretained typeof(self) vc = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [vc refreshRequest];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.textColor = kColor(0x888888);
    self.myTableView.mj_header = header;

    //自动刷新(一进入程序就下拉刷新)
    [self.myTableView.mj_header beginRefreshing];
    
    // 添加上拉刷新尾部控件
    MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [vc loadMoreRequest];
    }];
    footer.stateLabel.textColor = kColor(0x888888);
    self.myTableView.mj_footer = footer;
    self.myTableView.mj_footer.hidden = YES;
}

//继承vc 重写上下拉方法调用接口
- (void)refreshRequest {}
- (void)loadMoreRequest {}

//某些页面不需要上下拉 重新new个UITableView 覆盖initRootView方法
- (void)rootTableViewWithoutRefresh {
    self.myTableView = [UITableView newAutoLayoutView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;

    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.backgroundColor = kColor(0xeeeeee);
    
    self.myTableView.tableHeaderView = [UIView new];
    self.myTableView.tableFooterView = [UIView new];

    self.myTableView.rowHeight = UITableViewAutomaticDimension;
    self.myTableView.estimatedRowHeight = 44;
    
    [self.view addSubview:self.myTableView];
}

3. 注册自定义cell

[self.myTableView registerClass:[AuctionNoticeCellAll class] forCellReuseIdentifier:@"AuctionNoticeCellAll"];

4. 复用cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AuctionNoticeCellAll *cell = [self.myTableView dequeueReusableCellWithIdentifier:@"AuctionNoticeCellAll" forIndexPath:indexPath];
    
    //item是mode         
    [cell setupItem:item];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;    
}

5. 右滑删除cell

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

//自定义删除view
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *delAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //删除   
            [self.noticeAlarms removeObjectAtIndex:indexPath.section];
            [self.myTableView beginUpdates];
            [self.myTableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                            withRowAnimation:UITableViewRowAnimationTop];
            [self.myTableView endUpdates];
    }];
    delAction.backgroundColor = kColorRed;
    
    return @[delAction];
}
/**
 *  删除显示的view
 */
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"normal" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    //删除   
        [self.items removeObjectAtIndex:indexPath.section];
        [self.myTableView beginUpdates];
        [self.myTableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                            withRowAnimation:UITableViewRowAnimationTop];
        [self.myTableView endUpdates];
    }];
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"default" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }];
    UITableViewRowAction *action3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"destructive" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }];
    
    action1.backgroundColor = [UIColor lightGrayColor];
    action2.backgroundColor = [UIColor greenColor];
    action3.backgroundColor = [UIColor redColor];
    
    action1.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
//    action2.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//    action3.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];

    if (indexPath.row%3 == 0) {
        return @[action1, action2];
    }
    
    return @[action1, action2, action3];
}

6. tip

//tableview 删除某行 先删除数据
在调用deleteRowsAtIndexPaths:方法前,要确保数据为最新。也就是说,先将要删除的数据从数据源中删除
    [self.rootItems removeObjectAtIndex:indexPath.row];

    [self.myTableView beginUpdates];
    [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]  withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.myTableView endUpdates];

删除section
[self.noticeAlarms removeObjectAtIndex:indexPath.section];
            [self.myTableView beginUpdates];
            [self.myTableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];

            [self.myTableView endUpdates];

7. 滚动时显示的cell由小变大动画

在tableView:cellForRowAtIndexPath:设置初始值 在willDisplayCell恢复

[cell.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        obj.transform = CGAffineTransformMakeScale(.8, .8);

    }];



/**

 *  cellForRowAtIndexPath初始化cell后准备显示cell调用. 不需要修改cell.transform

 */

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    [UIView animateWithDuration:.5 animations:^{

        [cell.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            obj.transform = CGAffineTransformIdentity;

        }];

        NSLog(@"willDisplayCell %@", cell);

    }];

}

8.  

9. 

10. 

11. 

 

 

转载于:https://my.oschina.net/dkdsj/blog/607130

内容概要:该论文研究增程式电动汽车(REEV)的能量管理策略,针对现有优化策略实时性差的问题,提出基于工况识别的自适应等效燃油消耗最小策略(A-ECMS)。首先建立整车Simulink模型和基于规则的策略;然后研究动态规划(DP)算法和等效燃油最小策略;接着通过聚类分析将道路工况分为四类,并设计工况识别算法;最后开发基于工况识别的A-ECMS,通过高德地图预判工况类型并自适应调整SOC分配。仿真显示该策略比规则策略节油8%,比简单SOC规划策略节油2%,并通过硬件在环实验验证了实时可行性。 适合人群:具备一定编程基础,特别是对电动汽车能量管理策略有兴趣的研发人员和技术爱好者。 使用场景及目标:①理解增程式电动汽车能量管理策略的基本原理;②掌握动态规划算法和等效燃油消耗最小策略的应用;③学习工况识别算法的设计和实现;④了解基于工况识别的A-ECMS策略的具体实现及其优化效果。 其他说明:此资源不仅提供了详细的MATLAB/Simulink代码实现,还深入分析了各算法的原理和应用场景,适合用于学术研究和工业实践。在学习过程中,建议结合代码调试和实际数据进行实践,以便更好地理解策略的优化效果。此外,论文还探讨了未来的研究方向,如深度学习替代聚类、多目标优化以及V2X集成等,为后续研究提供了思路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值