IOS-OC之高级控件TableView之一
来源:互联网 发布:java中的除法运算 编辑:程序博客网 时间:2024/05/17 16:46
简单的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文件注意dataSource和delegate的拖拽,要拖到File’s Owner上哦~ 233~~~
还要注意tableView和.h文件的关联呢~~~
问题:
这次我遇到什么问题呢,布局的问题,没有加约束之前,列表没有靠近底部,留有一片空白,加了到顶部、左、右、底部都为0的约束之后就充满了屏幕~~
大家会加约束吗?点完约束记得点下边的Add constraints哦~~
我经常忘~233~
0 0
- IOS-OC之高级控件TableView之一
- IOS-OC之高级控件TableView之二
- IOS-OC之高级组件PickerView之一
- 《iOS 高级编程》之Tableview进阶指南
- 《iOS 高级编程》之Tableview进阶指南
- oc ios ,json,TableView(listView)
- IOS控件-tableview
- iOS-OC-基本控件之UIPageControl
- iOS-OC-基本控件之UIPageControl
- iOS-OC-基本控件之UIPageControl
- iOS之UI控件之TableView详细解释
- IOS-OC之高级组件PickerView之二
- iOS tableView 控件用法 [转]
- IOS控件TableView下拉刷新
- iOS tableView~尾部添加控件
- iOS之tableView优化
- iOS笔记之tableView
- iOS基础之OC简单控件知识了解
- Android 动画 ValueAnimator(三)
- signal exception
- 9-axis sensor计算角位移 调研资料
- C++ Primer学习笔记——顺序容器操作
- json 转数组
- IOS-OC之高级控件TableView之一
- Maven 配置
- PHPexcel导出_带合并单元格/边框背景/_有效果图_thinkphp
- 系统学习机器学习之模型诊断与调试
- 使用AndResGuard混淆资源文件
- 全国省市二级对照表 数组形式
- 为非列表元素设置列表样式
- 设计模式-原型模式
- 序列号生成算法分析