【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];
        [email protected]"德系汽车";
        [email protected]"质量最精良";
        [email protected][@"宝马",@"奥迪",@"奔驰"];

        WSCarGroup *cg2=[[WSCarGroup alloc]init];
        [email protected]"日系汽车";
        [email protected]"很轻很省油";
        [email protected][@"三菱",@"日产",@"本田",@"三菱",@"日产",@"本田",@"三菱",@"日产",@"本田",@"三菱",@"日产",@"本田"];

        [email protected][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分组好友的那个组名称在最顶部悬停,直到被下一组顶替。

时间: 2024-08-01 14:36:32

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

iOS开发中单例对象的标准创建方法

//标准的单例写法 //以创建歌曲的管理者为例进行创建.+(instancetype) sharedQYSongManager { static QYSongsManager *songManager =nil; //采用GDC标准单例实现方法 static dispatch_once_t onceToken; //Executes a block object once and only once for the lifetime of an application. dispatch_onc

iOS开发-No matching provisioning profiles found解决方法

今天真机调试的时候莫名其妙遇到了这样的一个问题: This product type must be built using a provisioning profile, however no provisioning profile matching both the identity "iPhone Developer" and the bundle identifier..... 具体如下图所示: 十分蛋疼, 发现不管是从网上下的demo, 还是自己的过程.凡事真机测试的时候都

IOS开发系列--TableView、多个TableViewCell、自定义Cell、Cell上画画(故事板+代码方式),ios7tableview索引

在此之前,我们已经创建了一个通过简单的表视图应用程序并显示预定义的图像.在本教程中,我们将继续努力,使应用程序变得更好,: >不同的行显示不同的图像 - 上个教程,我们的所有行显示相同的缩略图.那么不同的食物显示不同的图片不是更好么? >自定义视图单元-我们将展示我们自己的视图来替代默认表单元格样式 显示不同缩略图 在我们更改代码之前,让我们回顾显示缩略图的代码. 最后,我们增加了一个行代码指示UITableView每一行显示"creme_brelee.jpg"这张图片.显

ios开发runtime学习二:runtime交换方法

#import "ViewController.h" /* Runtime(交换方法):主要想修改系统的方法实现 需求: 比如说有一个项目,已经开发了2年,忽然项目负责人添加一个功能,每次UIImage加载图片,告诉我是否加载成功 当系统提供的控件不能满足我们的需求的时候,我们可以 1:通过继承系统控件,重写系统的方法,来扩充子类的行为(super的调用三种情况) 2:当需要为系统类扩充别的属性或是方法的时候,与哪个类有关系,就为哪个类创建分类.3:利用runtime修改系统的类,增加

IOS开发中tableView显示列表内容数据(storyboard版)

这是第一次写博客这类东西,且同为菜鸟级自学IOS,若有哪些不正确的希望您指正,谢谢... 先写一个大家自学时都会用到的东西——列表展示,或许您不认为这是问题,那是因为您聪慧,刚学时倒是困扰到我了,特意写一下: 第一步:创建工程IOS-->single view application      ——> Product Name:tableViewDemo                    Language:Objective—C                    Devices:iPh

iOS开发设置tableview的分割线

在开发ios8中大家会发现系统自带的分割线前面会有15个像素的空余,那么怎么才能像以前一样的,我看到别人的博客有提到 首先在viewdidload中设置好你的系统分割线,然后加上如下代码 listView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width,self.contentView.frame.size.height) style:UITableViewStylePla

ios开发之--tableview刷新某一个区和某一行

在开发中,有时候,我们不需要刷新整个表,只需要刷新局部数据即可,具体代码如下: //section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic]; //cell刷新 NSIndexPath *indexPath=[NSIndexPath indexPat

IOS 开发中 TableView的文本Cell高度的自适应,UILabel自动换行适应

最后的运行效果: 需求: 1.表格里的UILable要求自动换行 2.创建的tableViewCell的高度会自动适应内容的高度 一.用xcode构建项目,创建一个有tableView的视图,用纯代码的形式实现: 1.创建一个UIViewController类,定义一个UITableView,实现TableView的委托和数据源协议 // //  TableViewController.h //  AdaptiveCell // //  Created by swinglife on 14-1-

[IOS 开发]TableView如何刷新指定的cell 或section

//一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic]; //一个cell刷新 NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0]; [tableView r