【iOS开发-58】tableView初识:5个重要方法的使用和2种样式的差别

创建一个tableView,直接拖拽放在storyboard里面就可以。

(1)先创建一个数据模型类WSCarGroup,在WSCarGroup.h文件里:

#import <Foundation/Foundation.h>

@interface WSCarGroup : NSObject

@property(nonatomic,copy) NSString * title;
@property(nonatomic,copy) NSString * desc;
@property(nonatomic,strong) NSArray *cars;

@end

(2)在ViewController.m中:

——先把数据转成模型,这里没有plist。所以直接手动输入。

先声明一个装模型的数组变量

@property (nonatomic,strong) NSArray *carGroups;

-(NSArray *)carGroups{
    if (_carGroups==nil) {
        WSCarGroup *cg1=[[WSCarGroup alloc]init];
        cg1.title=@"德系汽车";
        cg1.desc=@"质量最精良";
        cg1.cars=@[@"宝马",@"奥迪",@"奔驰"];
        
        WSCarGroup *cg2=[[WSCarGroup alloc]init];
        cg2.title=@"日系汽车";
        cg2.desc=@"非常轻非常省油";
        cg2.cars=@[@"三菱",@"日产",@"本田",@"三菱",@"日产",@"本田",@"三菱",@"日产",@"本田",@"三菱",@"日产",@"本田"];
        
        _carGroups=@[cg1,cg2];
    }
    return _carGroups;
}


——设置tableView的数据源,和协议相似。充当数据源的要遵守“协议”。此处我们把ViewController当做数据源。所以先遵守:

@interface ViewController ()<UITableViewDataSource>


——然后设置成数据源

- (void)viewDidLoad {
    self.tableView.dataSource=self;
    [super viewDidLoad];
}

——tableView最重要的几个方法

111、设置多少组(假设省略这种方法,则默认是1组)

222、设置每组有多少行

333、设置每行的cell填充什么内容(最重要)

444、设置每组头部标题文字

555、设置妹婿尾部描写叙述文字

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carGroups.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    WSCarGroup *cg=self.carGroups[section];
    return cg.cars.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //由于要返回UITableViewCell。所以先创建一个,然后赋值,最后返回,就可以。
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    WSCarGroup *cg=self.carGroups[indexPath.section];
    cell.textLabel.text=cg.cars[indexPath.row];
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    WSCarGroup *cg=self.carGroups[section];
    return cg.title;
}

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    WSCarGroup *cg=self.carGroups[section];
    return cg.desc;
}

——当然。为了展示方便,隐藏顶部状态栏

-(BOOL)prefersStatusBarHidden{
    return YES;
}

tableView有两种展现形式:plain和grouped。

——plain就是每组之间间隔非常小,分组不明显。grouped每组间隔大分组明显。例如以下:


——plain有头部悬停效果。相似于QQ分组好友的那个组名称在最顶部悬停,直到被下一组顶替。


转载于:https://www.cnblogs.com/zhchoutai/p/8622982.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值