iOS开篇——UI之UITableView

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
  4 {
  5     //创建数据源
  6     NSMutableArray * _dataArray;
  7     
  8 }
  9 
 10 @end
 11 
 12 @implementation ViewController
 13 
 14 - (void)viewDidLoad {
 15     [super viewDidLoad];
 16     [self createTableView];
 17     
 18     //获得文件路径
 19     NSString * str = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
 20     //用文件路径初始化数组
 21     _dataArray = [[NSMutableArray  alloc]initWithContentsOfFile:str];
 22     
 23     self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
 24     
 25     
 26 }
 27 
 28 - (void)createTableView{
 29     //创建tableView  以及style
 30     UITableView * tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
 31     
 32     //设置代理
 33     tableView.dataSource = self;
 34     tableView.delegate = self;
 35     //设置行宽  也就是每行的高度  一般去代理方法里设置
 36 //    tableView.rowHeight = 80;
 37     
 38     UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
 39     //设置头视图
 40     tableView.tableHeaderView = view;
 41     
 42     
 43     //设置尾视图
 44     UIView * fview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
 45     tableView.tableFooterView = fview;
 46     
 47     [self.view addSubview:tableView];
 48 }
 49 
 50 
 51 
 52 //代理方法
 53 
 54 //行数
 55 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 56     return _dataArray.count;
 57     
 58 }
 59 
 60 //返回tableView的组数
 61 //如果不设置 默认返回1
 62 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
 63     return 2;
 64 }
 65 
 66 //创建cell 刷新cell
 67 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 68     
 69     //去复用队列里找 有没有空闲的cell cell的标示为cellID
 70     UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
 71     
 72     if (cell == nil) {
 73         //如果是空 初始化cell 标示设置为cellID
 74         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"];
 75         /*
 76          typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
 77          UITableViewCellStyleDefault,    无二级文字
 78          UITableViewCellStyleValue1,    二级文字位置和一级文字平行UITableViewCellStyleValue2, 无图         UITableViewCellStyleSubtitle
 79          };
 80          */
 81     }
 82     
 83     //设置附件样式
 84     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
 85     /*
 86      typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
 87      UITableViewCellAccessoryNone,
 88      UITableViewCellAccessoryDisclosureIndicator,
 89      UITableViewCellAccessoryDetailDisclosureButton
 90      UITableViewCellAccessoryCheckmark,
 91      UITableViewCellAccessoryDetailButton
 92      };
 93      */
 94     
 95     cell.backgroundColor = [UIColor yellowColor];
 96     UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
 97     view.backgroundColor = [UIColor greenColor];
 98     cell.selectedBackgroundView = view;
 99     
100     NSDictionary * dic = _dataArray[indexPath.row];
101     
102     cell.textLabel.text = [dic objectForKey:@"name"];
103     
104     cell.imageView.image = [UIImage imageNamed:[dic objectForKey:@"icon"]];
105     
106 //    if (indexPath.row%2==0) {
107 //        cell.backgroundColor = [UIColor greenColor];
108 //    }else{
109 //        cell.backgroundColor = [UIColor redColor];
110 //    }
111     
112     cell.detailTextLabel.text = [dic objectForKey:@"intro"];
113     cell.detailTextLabel.numberOfLines = 0;
114     
115     
116     return cell;
117 }
118 
119 
120 
121 ////设置组的尾视图
122 //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
123 //    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 5)];
124 //    view.backgroundColor = [UIColor clearColor];
125 //    
126 //    return view;
127 //}
128 //
129 ////设置组的头视图
130 //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
131 //    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 5)];
132 //    view.backgroundColor = [UIColor clearColor];
133 //    return view;
134 //}
135 //设置组尾视图高度
136 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
137     return 20;
138 }
139 //设置组头视图高度
140 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
141     return 20;
142 }
143 //设置组头视图标题 如果设置了组的头视图  则不会显示被遮盖
144 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
145     NSString *str = @"我是头";
146     return str;
147 }
148 //设置组尾视图标题 如果设置了组的尾视图  则不会显示被遮盖
149 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
150     NSString * str = @"我是尾巴";
151     return str;
152 }
153 
154 
155 
156 
157 //设置行高度
158 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
159     
160     return 90;
161 }
162 
163 //点击每个cell 触发此方法
164 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
165     //点击cell 出现提示框 提示框显示cell信息
166     //找到点击的cell
167     UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
168     NSLog(@"cell.text = %@",cell.textLabel.text);
169     
170     UIAlertView * alerView = [[UIAlertView alloc]initWithTitle:cell.textLabel.text message:cell.detailTextLabel.text delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
171     [alerView show];
172 }
173 
174 
175 
176 - (void)didReceiveMemoryWarning {
177     [super didReceiveMemoryWarning];
178     // Dispose of any resources that can be recreated.
179 }
180 
181 
182 
183 
184 
185 //
186 
187 @end

 

posted @ 2015-11-29 23:04  GXcoder  阅读(202)  评论(0编辑  收藏  举报