UITableView

本文深入探讨了UITableView的数据管理和自定义Cell的实现技术。包括如何使用UITableViewDataSource和UITableViewDelegate协议进行数据填充、增删改查、编辑与移动操作。同时详细介绍了如何自定义UITableViewCell的初始化、布局与高度适配,确保在不同设备上的良好展示效果。

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

#UITableView 继承 UIScrollView

##内容

###有两个协议

1. 内容

@property (nonatomic, assign)   id <UITableViewDataSource> dataSource;

2. 操作

@property (nonatomic, assign)   id <UITableViewDelegate>   delegate;


###区数 行数

- (NSInteger)numberOfSections;

- (NSInteger)numberOfRowsInSection:(NSInteger)section;

头 尾 2选1 or 0

- (CGRect)rectForHeaderInSection:(NSInteger)section;

- (CGRect)rectForFooterInSection:(NSInteger)section;


###增删读移动

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);


UITableViewDataSource 协议方法

@required

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; // 建立

@optional

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;    // 区

- (NSString *)tableView:(UITableView *)tableView titleForHeader(/Footer)InSection:(NSInteger)section; 


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; // 可编辑

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; // 可移动

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;  // 目录索引


// 编辑按钮的方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

// 删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.arr removeObjectAtIndex:indexPath.row];
//      [tableView reloadData];
      // 通过tableView来删除上面的cell
      // 第一个参数:制定删除哪个分区的哪行作为他的第一个元素放在数组中
      // 删除动画
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}


// 移动

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

// 移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString *str = [self.arr[sourceIndexPath.row] retain];
    [self.arr removeObjectAtIndex:sourceIndexPath.row];
    [self.arr insertObject:str atIndex:destinationIndexPath.row];
    [str release];
}


********************************************************************************************


#pragma iOS8.0之后出现的方法,可以编辑多个按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *deletAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [self.arr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }];
    UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        if (indexPath.row) {
            NSString *name = self.arr[indexPath.row];
            [self.arr removeObjectAtIndex:indexPath.row];
            [self.arr insertObject:name atIndex:0];
            NSIndexPath *indexPathZero = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
            [tableView moveRowAtIndexPath:indexPath toIndexPath:indexPathZero];
        }
    }];
    
    
    return @[deletAction, topAction];
}

// 三种样式;

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleDelete;

}

#pragma 重写系统的编辑按钮点击方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    UITableView *tabelView = (UITableView *)[self.view viewWithTag:1000];
    [super setEditing:editing animated:YES];
    [tabelView setEditing:editing animated:YES];
}

##自定义tableViewCell

#pragma 重写cell的初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 完成的属性视图的创建,但是一般创建之后不给属性视图fram
        [self createView];
    }
    return self;
}
- (void)createView{
    self.leftImageView = [[UIImageView alloc] init];
    self.rightImageView = [[UIImageView alloc] init];
    self.topLabel = [[UILabel alloc] init];
    self.bottomlabel = [[UILabel alloc] init];
    self.bottomlabel.backgroundColor = [UIColor yellowColor];
    self.topLabel.backgroundColor = [UIColor redColor];
    self.rightImageView.backgroundColor = [UIColor blueColor];
    self.leftImageView.backgroundColor = [UIColor cyanColor];
    
    // 添加
    
    [self.contentView addSubview:self.leftImageView];
    [self.contentView addSubview:self.rightImageView];
    [self.contentView addSubview:self.topLabel];
    [self.contentView addSubview:self.bottomlabel];
    
    [_rightImageView release];
    [_leftImageView release];
    [_topLabel release];
    [_bottomlabel release];

    
}
#pragma mark 这个方法是cell显示之前走得最后一个方法,一般会在方法里设置所有属性视图的大小和尺寸,这个方法会用在图片文字自适应的位置上

- (void)layoutSubviews{
    // 重写了父类的方法, 别忘了[supper layoutSubviews];
    [super layoutSubviews];
    self.leftImageView.frame = CGRectMake(0, 0, WIDTH / 3, HEIGHT);
    self.topLabel.frame = CGRectMake(WIDTH / 3, 0, WIDTH / 3, HEIGHT / 2);
    self.bottomlabel.frame = CGRectMake(WIDTH / 3, HEIGHT / 2, WIDTH / 3, HEIGHT / 2);
    self.rightImageView.frame = CGRectMake(WIDTH / 3 * 2, 0 , WIDTH / 3, HEIGHT);
    
}

- (void)dealloc{
    [_rightImageView release];
    [_leftImageView release];
    [_bottomlabel release];
    [_topLabel release];
    [super dealloc];
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

##cell 自适应高度


###tableView

#pragma mark 这个方法是tableview的delegate所提供的协议方法,主要是设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 根据图片的尺寸设置行高
    UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]];
    // 计算labl的高度
    // 根据对应的文字求出cell上label显示的高度
    
    NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
   // 根据文字的大小,计算出文本的尺寸 执行一个尺寸(375, 0);
    CGRect rect = [self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    return image.size.height / image.size.width * self.view.frame.size.width + rect.size.height;
    
}
###自定义cell
- (void)layoutSubviews{

    CGSize picSize = self.myImageView.image.size;
    self.myImageView.frame = CGRectMake(0, 0, self.contentView.frame.size.width , self.contentView.frame.size.width * picSize.height / picSize.width);
    

    
    // 默认字体17号
    NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    // 根据文字的大小,计算出文本的尺寸 执行一个尺寸(375, 0);
    CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    self.myLabel.frame = rect;
    self.myLabel.center = CGPointMake(self.myLabel.center.x, self.myLabel.center.y + self.myImageView.frame.size.height);
    [super layoutSubviews];
}



内容概要:该论文研究增程式电动汽车(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、付费专栏及课程。

余额充值