UITableView之Group

作者: 酒红色T恤 | 来源:发表于2016-05-31 23:39 被阅读408次
    • 爱你一万年
      #import "ViewController.h"
      #import "UIView+JKPicker.h"
      @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
      @property (weak, nonatomic) IBOutlet UITableView *myGroupTableView;

       @end
      
       @implementation ViewController
        -(void)viewDidLoad 
       {
       [super viewDidLoad];
       self.myGroupTableView.delegate = self;
       self.myGroupTableView.dataSource =self;
       }
      

    pragma mark - 设置组脚视图高度

        -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
       {
       return 20;
       }
    

    pragma mark - 设置组头视图高度

        -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
       if (section == 0) {
       return 40;
         }else
       return 40;
      }
    

    pragma mark - 设置每一组几行

       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
       if (section == 0) 
        {
        return 1; 
        }
       else
        return 2;
    
    
       }
    

    pragma mark - 设置几组

       - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
         return 2;
       }
    

    pragma mark - 设置组脚视图

       - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
       if (section == 0) {
        UIView * v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.myGroupTableView.frame.size.width, 20)];
        v.backgroundColor = [ UIColor redColor];
        return v;
       }else{
         return nil;
       }
       }
    

    pragma mark - 设置组头视图

      -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
          UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.myGroupTableView.frame.size.width, 60)];
             view.backgroundColor = [UIColor whiteColor];
                 UIView * height1View = [[ UIView alloc]initWithFrame:CGRectMake(15, 36, self.myGroupTableView.frame.size.width, 1)];
               height1View.backgroundColor = [UIColor lightGrayColor];  
                 UILabel * la = [[ UILabel alloc]init];                                                                                                      
     
          if (section == 0) {
        
        la.text = @"    基本信息";
        [la setFont:[UIFont systemFontOfSize:16]];
        la.width = 200;
        la.height = 50;
        [view addSubview:la];
        [view addSubview:height1View];
        }else{
        la.text = @"    我的荣誉";
        [la setFont:[UIFont systemFontOfSize:16]];
        la.width = 200;
        la.height = 50;
        [view addSubview:la];
        [view addSubview:height1View];
       }
        return  view;
       }
    

    pragma mark - 设置cell内容

       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
           UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
           if (cell == nil) {
        cell = [[ UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
        cell.backgroundColor = [ UIColor blueColor];
           }
           if (indexPath.section == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"第一组:%zd",indexPath.row];
           }else{
        cell.textLabel.text = [NSString stringWithFormat:@"第二组%zd",indexPath.row];}
           return cell;
       }

    相关文章

      网友评论

        本文标题:UITableView之Group

        本文链接:https://www.haomeiwen.com/subject/kpqhdttx.html