ios 基础学习之零基础代码控制实现3

本文详细介绍如何使用 iOS 中的 UITableView 控件,并通过一个实战项目演示 UITableView 的基本配置与使用方法,包括数据源设置、单元格填充、点击反馈等功能。

这一节学习一下ios中的tableview控件的简单使用 

UiTableView使用必须实现连个协议 <UITableViewDataSource,UITableViewDelegate>来进行对tableview进行数据管理

1)创建一个新的项目learn3

2)添加一个定义模块view FragmentTableView 并实现连个接口协议 UITableViewDataSource,UITableViewDelegate 并定义一个局部的TableView变量

如FragmentTableView.h代码所示

#import <UIKit/UIKit.h>

@interface FragmentTableView : UIView<UITableViewDataSource,UITableViewDelegate>{
    UITableView *mUiTableView;
    NSArray *datalist;
}
@end


3) 在 FragmentTableView.m 中对tableview进行管理

1.在 initWithFrame 中进行对  FragmentTableView.h 中声明好的tableview变量进行初始化

如.m中代码:

#import "FragmentTableView.h"

@implementation FragmentTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        // 初始化局部变量
        mUiTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        [mUiTableView setDataSource:self]; //设置数据源为本身
        [mUiTableView setDelegate:self]; // 设置协议为本身
        [self addSubview:mUiTableView]; // 添加tableview控件
        [mUiTableView release];
        
        // 初始化一些测试数据
        datalist = [[NSArray alloc]initWithObjects:@"上海",@"北京",@"广州",@"深圳",@"香港",@"澳门",@"杭州",@"西湖",@"重庆",@"南京", nil];
    }
    return self;
}

-(void)dealloc{
    [datalist release];
    [super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end



2.接下来我们来实现必要的接口 

(1)在没有看api的 我其实也不知道接下来怎么做了 

所以还是了解一下UITableview :command+鼠标点击去可以看到他也有实现的 

搜 @protocol (command+f)可以看到也定义了几个协议 UITableViewDataSource,UITableViewDelegate 

我们关心的 是要做什么 接下来我们就要找 必须实现的 接口协议了继续搜 @required 可以看到下面有连个方法 如下面代码,这样我们就知道该怎么做了 必须实现的

@protocol UITableViewDataSource<NSObject>

@required

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

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
(2)在FragmentTableView.m中实现上面两个方法 并进行设置数据 

实现 UITableViewDataSource 第一个必须实现的方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 从本方法的名称定义 可以看到是所有行的number
    // 返回数组的总数
    return datalist.count;
}
实现 UITableViewDataSource 第二个必须实现的方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 返回 UITableViewCell 对象 根据方法名称意思是 indexPath cell 每行上 就是每行cell的填充
    // 先初始化一个 UITableViewCell 这次初始化就不和以前一样 规定他的坐标位置了 直接定义他的样式 并设置标识
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell"]];
    NSUInteger row = [indexPath row]; // 初始化一个无符号int
    NSLog(@"row:%i",row);
    cell.textLabel.text = [datalist objectAtIndex:row];
    return cell;
}

ok该实现的我们也都写好了  接下来在 Viewcontroller.m viewDidLoad 方法中添加FragmentUItableview进行显示

FragmentTableView *tableView = [[FragmentTableView alloc]initWithFrame:CGRectMake(0, 0, 320, 300)];
    [self.view addSubview:tableView];
    [tableView release]
command+R 进行运行 ok正常显示数据如下图 (这样不能说算完了 因为我们上面的代码还不好 因为我在来回点击并进行拉页面会报异常,接下来我们进行返回cell中的代码优化)



4)接下来对上面代码进行优化并设计的复杂一点

1.在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 接口中进行修改 如下代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    // 返回 UITableViewCell 对象 根据方法名称意思是 indexPath cell 每行上 就是每行cell的填充
//    // 先初始化一个 UITableViewCell 这次初始化就不和以前一样 规定他的坐标位置了 直接定义他的样式 并设置标识
//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell"]];
//    NSUInteger row = [indexPath row]; // 初始化一个无符号int
//    NSLog(@"row:%i",row);
//    cell.textLabel.text = [datalist objectAtIndex:row];
//    return cell;
    
    // command+/ 进行注释上面的代码
    // 接下来进行优化上面的代码
    // 定义一个标识
    static NSString *stringIdentifier = @"cell";
    int row = [indexPath row];
    
    // 声明一个UITableViewCell 并接受 参数tableview中的一个标识cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:stringIdentifier];
    // 判断cell 是否为空 如果为空进行初始化 这样的好处是节省系统资源
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:stringIdentifier]autorelease];
    }
   
    // 接下来设置cell显示内容 可以多添加一点控件进去 我们对这个城市列表加上数字编号 并加一些背景等等
    
    // 和上面一样设置显示内容
    cell.textLabel.text = [datalist objectAtIndex:row];
    [cell.textLabel setTextAlignment:NSTextAlignmentCenter]; // 设置文字居中显示
    
    // 设置cell点击背景 这个只提供了 setSelectedBackgroundView方法 所以必须设置图片 我就随便截了个图用用吧 新建 New Group 文件夹命名为 images 鼠标右键添加截好的图片
    UIImageView *selectBgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 75/2)];
    [selectBgView setImage:[UIImage imageNamed:@"bg_item_red.png"]];
    [cell setSelectedBackgroundView:selectBgView];
    [selectBgView release];
    
    // 添加地区编号 背景图片
    UIImageView *idNumBgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, (75-45)/4, 45/2, 45/2)];
    [idNumBgView setImage:[UIImage imageNamed:@"bg_item_circle.png"]];
    [cell addSubview:idNumBgView];
    [idNumBgView release];
    // 添加编号
    UILabel *idNumLable = [[UILabel alloc]initWithFrame:CGRectMake(20, (75-45)/4, 45/2, 45/2)];
    [idNumLable setText:[NSString stringWithFormat:@"%i",row]];
    [idNumLable setTextAlignment:NSTextAlignmentCenter];
    [idNumLable setFont:[UIFont systemFontOfSize:12]];
    [idNumLable setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:idNumLable];
    
    return cell;
}
图片资源 


运行效果

2.接下来进行添加点击后获得item下标值 这样更符合我们需求

(1)接下来我们实现 UITableviewDelegate 中的 用户改变item选择方法

// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
实现代码如下:

// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    int row = [indexPath row];
    if (row != [datalist count]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                message:[NSString stringWithFormat:@"您选择的是:%@",[datalist objectAtIndex:row]]
                                delegate:self
                                cancelButtonTitle:@"确定"
                                otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

运行结果


5)总结

1.这节专门学习了UITableview的实现具体步骤 及 UITableview item 简单的布局



【评估多目标跟踪方法】9个高度敏捷目标在编队中的轨迹和测量研究(Matlab代码实现)内容概要:本文围绕“评估多目标跟踪方法”,重点研究9个高度敏捷目标在编队飞行中的轨迹生成与测量过程,并提供完整的Matlab代码实现。文中详细模拟了目标的动态行为、运动约束及编队结构,通过仿真获取目标的状态信息与观测数据,用于验证和比较不同多目标跟踪算法的性能。研究内容涵盖轨迹建模、噪声处理、传感器测量模拟以及数据可视化等关键技术环节,旨在为雷达、无人机编队、自动驾驶等领域的多目标跟踪系统提供可复现的测试基准。; 适合人群:具备一定Matlab编程基础,从事控制工程、自动化、航空航天、智能交通或人工智能等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于多目标跟踪算法(如卡尔曼滤波、粒子滤波、GM-CPHD等)的性能评估与对比实验;②作为无人机编队、空中交通监控等应用场景下的轨迹仿真与传感器数据分析的教学与研究平台;③支持对高度机动目标在复杂编队下的可观测性与跟踪精度进行深入分析。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注轨迹生成逻辑与测量模型构建部分,可通过修改目标数量、运动参数或噪声水平来拓展实验场景,进一步提升对多目标跟踪系统设计与评估的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值