iOS开发UI篇UITableview控件基本使用.docx

上传人:b****7 文档编号:10548599 上传时间:2023-02-21 格式:DOCX 页数:11 大小:16.88KB
下载 相关 举报
iOS开发UI篇UITableview控件基本使用.docx_第1页
第1页 / 共11页
iOS开发UI篇UITableview控件基本使用.docx_第2页
第2页 / 共11页
iOS开发UI篇UITableview控件基本使用.docx_第3页
第3页 / 共11页
iOS开发UI篇UITableview控件基本使用.docx_第4页
第4页 / 共11页
iOS开发UI篇UITableview控件基本使用.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

iOS开发UI篇UITableview控件基本使用.docx

《iOS开发UI篇UITableview控件基本使用.docx》由会员分享,可在线阅读,更多相关《iOS开发UI篇UITableview控件基本使用.docx(11页珍藏版)》请在冰豆网上搜索。

iOS开发UI篇UITableview控件基本使用.docx

iOS开发UI篇UITableview控件基本使用

iOS开发UI篇—UITableview控件基本使用

iOS开发UI篇UITableview控件基本使用一、一个简单的英雄展示程序NJHero.h文件代码(字典转模型)复制代码1#importFoundation/Foundation.h23@interfaceNJHero:

NSObject4/**5*头像6*/7@property(nonatomic,copy)NSString*icon;8/**

iOS开发UI篇—UITableview控件基本使用

 

一、一个简单的英雄展示程序

 

NJHero.h文件代码(字典转模型)

 

复制代码

 1#import

 2 

 3@interfaceNJHero:

NSObject

 4/**

 5 * 头像

 6 */

 7@property(nonatomic,copy)NSString*icon;

 8/**

 9 * 名称

10 */

11@property(nonatomic,copy)NSString*name;

12/**

13 * 描述

14 */

15@property(nonatomic,copy)NSString*intro;

16 

17-(instancetype)initWithDict:

(NSDictionary*)dict;

18+(instancetype)heroWithDict:

(NSDictionary*)dict;

19@end

复制代码

NJViewController.m文件代码

 

复制代码

 1#import"NJViewController.h"

 2#import"NJHero.h"

 3 

 4@interfaceNJViewController()

 5/**

 6 * 保存所有的英雄数据

 7 */

 8@property(nonatomic,strong)NSArray*heros;

 9@property(weak,nonatomic)IBOutletUITableView*tableView;

10 

11@end

12 

13@implementationNJViewController

14 

15#pragmamark-懒加载

16-(NSArray*)heros

17{

18  if(_heros==nil){

19    //1.获得全路径

20    NSString*fullPath= [[NSBundlemainBundle]pathForResource:

@"heros"ofType:

@"plist"];

21    //2.更具全路径加载数据

22    NSArray*dictArray=[NSArrayarrayWithContentsOfFile:

fullPath];

23    //3.字典转模型

24    NSMutableArray*models=[NSMutableArrayarrayWithCapacity:

dictArray.count];

25    for(NSDictionary*dictindictArray){

26      NJHero*hero=[NJHeroheroWithDict:

dict];

27      [modelsaddObject:

hero];

28    }

29    //4.赋值数据

30    _heros=[modelscopy];

31  }

32  //4.返回数据

33  return_heros;

34}

35 

36-(void)viewDidLoad

37{

38  [superviewDidLoad];

39  //设置Cell的高度

40  //当每一行的cell高度一致的时候使用属性设置cell的高度

41  self.tableView.rowHeight=60;

42  self.tableView.delegate=self;

43}

44 

45#pragmamark-UITableViewDataSource

46//返回多少组

47-(NSInteger)numberOfSectionsInTableView:

(UITableView*)tableView

48{

49  return1;

50}

51//返回每一组有多少行

52-(NSInteger)tableView:

(UITableView*)tableViewnumberOfRowsInSection:

(NSInteger)section

53{

54  returnself.heros.count;

55}

56//返回哪一组的哪一行显示什么内容

57-(UITableViewCell*)tableView:

(UITableView*)tableViewcellForRowAtIndexPath:

(NSIndexPath*)indexPath

58{

59  //1.创建CELL

60  UITableViewCell*cell=[[UITableViewCellalloc]initWithStyle:

UITableViewCellStyleSubtitlereuseIdentifier:

nil];

61  //2.设置数据

62  //2.1取出对应行的模型

63  NJHero*hero=self.heros[indexPath.row];

64  //2.2赋值对应的数据

65  cell.textLabel.text=hero.name;

66  cell.detailTextLabel.text=hero.intro;

67  cell.imageView.image=[UIImageimageNamed:

hero.icon];

68  //3.返回cell

69  returncell;

70}

71#pragmamark-UITableViewDelegate

72/*

73//当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度

74-(CGFloat)tableView:

(UITableView*)tableViewheightForRowAtIndexPath:

(NSIndexPath*)indexPath

75{

76  if(1==indexPath.row){

77    return180;

78  }

79  return44;

80}

81 */

82 

83#pragmamark-控制状态栏是否显示

84/**

85 * 返回YES代表隐藏状态栏,NO相反

86 */

87-(BOOL)prefersStatusBarHidden

88{

89  returnYES;

90}

91@end

复制代码

实现效果:

 

 

 

代码注意点:

 

(1)在字典转模型的代码处用下面的代码,为可变数组分配dictArray.count个存储空间,可以提高程序的性能

 

NSMutableArray*models=[NSMutableArrayarrayWithCapacity:

dictArray.count];

 

(2)设置cell的高度

 

有三种办法可以设置cell的高度

 

1)可以在初始加载方法中设置,self.tableView.rowHeight=60;这适用于当每一行的cell高度一致的时候,使用属性设置cell的高度。

 

2)在storyboard中设置,适用于高度一致

 

3)当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度

 

  -(CGFloat)tableView:

(UITableView*)tableViewheightForRowAtIndexPath:

(NSIndexPath*)indexPath

 

{

 

  if(1==indexPath.row){

 

    return180;

 

  }

 

  return44;

 

}

 

二、cell的一些属性

 

代码示例:

 

复制代码

 1#import"NJViewController.h"

 2#import"NJHero.h"

 3 

 4@interfaceNJViewController()

 5/**

 6 * 保存所有的英雄数据

 7 */

 8@property(nonatomic,strong)NSArray*heros;

 9@property(weak,nonatomic)IBOutletUITableView*tableView;

 10 

 11@end

 12 

 13@implementationNJViewController

 14 

 15#pragmamark-懒加载

 16-(NSArray*)heros

 17{

 18  if(_heros==nil){

 19    //1.获得全路径

 20    NSString*fullPath= [[NSBundlemainBundle]pathForResource:

@"heros"ofType:

@"plist"];

 21    //2.更具全路径加载数据

 22    NSArray*dictArray=[NSArrayarrayWithContentsOfFile:

fullPath];

 23    //3.字典转模型

 24    NSMutableArray*models=[NSMutableArrayarrayWithCapacity:

dictArray.count];

 25    for(NSDictionary*dictindictArray){

 26      NJHero*hero=[NJHeroheroWithDict:

dict];

 27      [modelsaddObject:

hero];

 28    }

 29    //4.赋值数据

 30    _heros=[modelscopy];

 31  }

 32  //4.返回数据

 33  return_heros;

 34}

 35 

 36-(void)viewDidLoad

 37{

 38  [superviewDidLoad];

 39  //设置Cell的高度

 40  //当每一行的cell高度一致的时候使用属性设置cell的高度

 41  self.tableView.rowHeight=60;

 42  self.tableView.delegate=self;

 43 

 44}

 45 

 46#pragmamark-UITableViewDataSource

 47//返回多少组

 48-(NSInteger)numberOfSectionsInTableView:

(UITableView*)tableView

 49{

 50  return1;

 51}

 52//返回每一组有多少行

 53-(NSInteger)tableView:

(UITableView*)tableViewnumberOfRowsInSection:

(NSInteger)section

 54{

 55  returnself.heros.count;

 56}

 57//返回哪一组的哪一行显示什么内容

 58-(UITableViewCell*)tableView:

(UITableView*)tableViewcellForRowAtIndexPath:

(NSIndexPath*)indexPath

 59{

 60  //1.创建CELL

 61  UITableViewCell*cell=[[UITableViewCellalloc]initWithStyle:

UITableViewCellStyleSubtitlereuseIdentifier:

nil];

 62  //2.设置数据

 63  //2.1取出对应行的模型

 64  NJHero*hero=self.heros[indexPath.row];

 65  //2.2赋值对应的数据

 66  cell.textLabel.text=hero.name;

 67  cell.detailTextLabel.text=hero.intro;

 68  cell.imageView.image=[UIImageimageNamed:

hero.icon];

 69   

 70  //2.3设置cell的辅助视图

 71  //cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;

 72  if(0==indexPath.row){

 73    cell.accessoryView=[UIButtonbuttonWithType:

UIButtonTypeContactAdd];

 74  }else

 75  {

 76    cell.accessoryView=[[UISwitchalloc]init];

 77  }

 78//  UIButton*btn=[[UIButtonalloc]init];

 79//  btn.backgroundColor=[UIColorredColor];

 80//  cell.accessoryView=btn;

 81   

 82   

 83  //2.4设置cell的背景颜色

 84  cell.backgroundColor=[UIColorblueColor];

 85   

 86  //设置默认状态的背景

 87//  UIView*view=[[UIViewalloc]init];

 88//  view.backgroundColor=[UIColorblueColor];

 89//  cell.backgroundView=view;

 90   

 91  UIImageView*iv=[[UIImageViewalloc]initWithImage:

[UIImageimageNamed:

@"buttondelete"]];

 92  cell.backgroundView=iv;

 93   

 94  //设置选中状态的背景

 95  UIView*view2=[[UIViewalloc]init];

 96  view2.backgroundColor=[UIColorpurpleColor];

 97  cell.selectedBackgroundView=view2;

 98  //3.返回cell

 99  returncell;

100}

101 

102 

103#pragmamark-控制状态栏是否显示

104/**

105 * 返回YES代表隐藏状态栏,NO相反

106 */

107-(BOOL)prefersStatusBarHidden

108{

109  returnYES;

110}

111@end

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1