标签:
-(UITableView *)newsTableView{ if(!_newsTableView){ //初始化 // UITableViewStylePlain和UITableViewStyleGrouped是UItableview的两种风格,使用UITableViewStyleGrouped的时候,他会自己帮你分开每一个分区,而UITableViewStylePlain就需要自己实现下面的代理方法设置每个分区的头和脚的视图和高度(下面会细讲) _newsTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; //设置是否显示滚动(进度)条 _newsTableView.showsVerticalScrollIndicator = NO; //设置分割线的颜色 _newsTableView.separatorColor = [UIColor colorWithRed:244/255.0 green:245/255.0 blue:246/255.0 alpha:1.0]; //设置分隔线的样式 _newsTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;// //设置背景色 _newsTableView.backgroundColor = [UIColor colorWithRed:244/255.0 green:245/255.0 blue:246/255.0 alpha:1.0]; //设置代理,(代理需要实现 <UITableViewDataSource,UITableViewDelegate>两个代理) _newsTableView.dataSource = self; _newsTableView.delegate = self; } return _newsTableView; }
二.代理实现
默认是返回1,如果自己有改动,就必须重新实现 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 1; }
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //使用静态变量定义一个唯一的标识符 static NSString * userDetailInfoTableViewCell = @"userDetailInfoTableViewCell"; //根据标识符,在重用队列里面找同类的cell,直接取出来 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:userDetailInfoTableViewCell]; if(!cell){ //如果不存在(说明重用队列里面没有闲置的cell),就重新初始化一个cell,当然要指明其标识符,这样之后才能取。 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:userDetailInfoTableViewCell]; } //设置点击cell时的风格,这里是没有变化 cell.selectionStyle = UITableViewCellSelectionStyleNone; //设置cell显示右边的箭头符号 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //每次得到cell后,重新设定里面的内容 cell.textLabel.text = @"news"; return cell; }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.section==1 && indexPath.row == 2){//第2区的第3行 return 100; } return 120;//可能存在不同行有不同的高度,就像扣扣空间的动态一样,那就需要计算每一行的高度再返回;如果所有的行的高度都一样,就可以不用这个方法,而是直接在初始化UITableView设定UITableView.rowHeight=120;; }
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 10.0;//同返回行高度一样,可以根据参数section来判断哪一个分区头,就可以设定不同分区头的高度。 //有头就有脚,和此函数对应的就有-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ }设定当前分区的分区脚视图的高度。 }
标签:
原文地址:http://www.cnblogs.com/Yongersblog/p/5825325.html