IOS-OC之高级控件TableView之一

简单的tableView哦,和列表一样,只有lable和image,很简单哦~~ 233~~~
最终效果图

上代码啦~~~
.h的代码

#import <UIKit/UIKit.h>
//加UITableViewDataSource,UITableViewDelegate委托代理,具体代码在.m中实现
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
//字符串数组
@property (nonatomic,strong) NSArray *listData;
//图片数组
@property (nonatomic , strong) NSArray *listImage;
//tableView声明
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end

.m文件的代码

@implementation ViewController
//返回数据数组的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_listData count];
}
//设置tableview的cell,就是没一行的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//这里要注意cell的复用
//相当于cell的tag,方便后面复用吧~~我是这么理解的
    static NSString *cellDentifier = @"cell";
    //声明cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellDentifier];
    if(cell == nil){
    //UITableViewCellStyleDefault:cell的样式,reuseIdentifier:cell的复用
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellDentifier];              
    }
    //设置cell的lable,indexPath.row获取cell的position
    cell.textLabel.text = _listData[indexPath.row];
    //设置cell的image
    cell.imageView.image = [UIImage imageNamed:_listImage[indexPath.row]];
    return  cell;    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //声明字符串数组
    NSArray *datas = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22", nil];
    //声明图片数组
    NSArray *images = [[NSArray alloc]initWithObjects:@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png",@"tab_nor.png", nil];
    //把当前数组赋值给.h中声明的数组
    self.listData = datas;
    self.listImage = images;

}
//这个事件是点击某一行数据时执行的操作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//获取到当前点击这一行的数据用initWithFormat拼接成字符串
    NSString *message = [[NSString alloc]initWithFormat:@"你选择了%@", _listData[indexPath.row]];
    //这个是显示一个alert
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选中" message: message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:ok];
    [self presentViewController:alert animated:YES completion:nil];
    //点击后会有按下效果,这个是执行操作后消除这一行的效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

nib文件

nib文件注意dataSource和delegate的拖拽,要拖到File’s Owner上哦~ 233~~~
还要注意tableView和.h文件的关联呢~~~

问题:
这次我遇到什么问题呢,布局的问题,没有加约束之前,列表没有靠近底部,留有一片空白,加了到顶部、左、右、底部都为0的约束之后就充满了屏幕~~
加约束的图片
大家会加约束吗?点完约束记得点下边的Add constraints哦~~
我经常忘~233~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
JavaFX 的 TableView 控件是一个用于展示表格数据的高级控件。它提供了灵活的列和行布局,支持对表格数据进行排序、筛选、编辑和选择等操作。 TableView 的主要特点和功能包括: 1. 列(Columns):TableView 可以包含多个列,每个列由 TableColumn 对象表示。你可以添加、移除和重新排序列,设置列的宽度、标题、对齐方式等属性。 2. 行(Rows):TableView 的每一行表示表格中的一条数据记录。可以通过添加到 TableView 的数据集合(ObservableList)来动态添加和删除行。 3. 单元格(Cells):每个单元格用于显示一个数据项。你可以自定义单元格的样式,包括字体、颜色、背景等。同时,TableView 也支持对单元格进行编辑。 4. 数据源(Items):TableView 使用一个 ObservableList 作为数据源,它可以是任何实现了 javafx.collections.ObservableList 接口的类。 5. 排序和筛选:TableView 支持对表格数据进行排序和筛选。你可以通过设置 TableColumn 的 sortable 属性来启用或禁用某列的排序功能。 6. 编辑(Editing):TableView 可以设置为可编辑模式,允许用户直接在表格中修改数据。你可以通过设置 TableColumn 的 editable 属性来控制某列是否可编辑。 7. 选择(Selection):可以通过 TableView 的 SelectionModel 获取用户选择的行或单元格信息。支持单选和多选模式。 8. 自定义单元格显示:你可以通过 setCellFactory 方法来自定义单元格的显示方式,实现更复杂的表格布局和样式。 使用 TableView 控件,你可以将表格数据以更直观、灵活和可交互的方式展示给用户,满足各种数据展示和操作的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米悠悠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值