IOS-OC之高级控件TableView之一

本文介绍了一个简单的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~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

米悠悠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值