zoukankan      html  css  js  c++  java
  • UITableView的使用总结

    直接贴代码了,很好理解,注释很全,一看就懂......

    //
    //  ViewController.m
    //  TableViewSectionTitleDemo
    //
    //  Created by 思 彭 on 2017/4/13.
    //  Copyright © 2017年 思 彭. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
    
    @property (nonatomic, strong) UITableView *tableView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setUI];
    }
    
    #pragma mark - 设置界面
    
    - (void)setUI {
        
        // 注意: 如果为group类型的tableView,会造成第一section的header的title高度有问题
        /*
         1.UITableViewStylePlain有多段时 段头停留(自带效果)
         没有中间的间距和头部间距(要想有的重写UITableViewCell UITableViewHeaderFooterView里面的setFrame方法)
         2.UITableViewStyleGrouped
         去掉头部和中间间隔
         设置标头的高度为特小值 (不能为零 为零的话苹果会取默认值就无法消除头部间距了)
         UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
         view.backgroundColor = [UIColor redColor];
         self.tableView.tableHeaderView = view;
         */
        
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.tableView.tableFooterView = [[UIView alloc]init];
        self.tableView.sectionIndexColor = [UIColor grayColor];
        self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
        self.tableView.sectionHeaderHeight = 30.0f;
        self.tableView.sectionFooterHeight = FLT_EPSILON;
        
        // 方法一:修改sectionHeader的背景颜色
    //    [[UITableViewHeaderFooterView appearance] setTintColor:[UIColor lightGrayColor]];
        // 注册cell
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        [self.view addSubview: self.tableView];
    }
    
    #pragma mark - UITableViewDataSource
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        
        return 5;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        
        return 10;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.text = @"思思";
        return cell;
    }
    
    #pragma mark - UITableViewDelegate
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        
        return @"A";
    }
    
    
    - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        
        return @[@"A", @"B", @"C", @"D", @"E"];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
        
        return 5;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
    }
    
    // 6.0以上使用
    -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
    {
        // 方法二:修改sectionHeader的背景颜色 Background color
    //    view.tintColor = [UIColor blackColor];
        
        // Text Color
        UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
        [header.textLabel setTextColor:[UIColor whiteColor]];
        header.backgroundColor = [UIColor redColor];
        
        // 方法三:修改sectionHeader的背景颜色
        header.contentView.backgroundColor = [UIColor lightGrayColor];
        // Another way to set the background color
        // Note: does not preserve gradient effect of original header
        // header.contentView.backgroundColor = [UIColor blackColor];
    }
    
    /* 方法四:修改sectionHeader的背景颜色
    -(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
            UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
            UIView* content = castView.contentView;
            UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
            content.backgroundColor = color;
        }
    }
    */
    
    // 让段头不停留(取消粘性效果)
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        
        CGFloat sectionHeaderHeight = 30;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
    
    @end
  • 相关阅读:
    面对祖传屎山代码应该采用的5个正确姿势
    一行代码卖出570美元, 天价代码的内幕
    漫画 | 悲催的中国式软件开发
    看看我每天的工作,你们这些程序员都是“辣鸡”!
    漫画 | 浏览器一个比一个“无耻”
    程序员应该造的五大轮子
    我所尊敬的三位女程序员
    重磅!七国首脑会议决定制裁Go语言!
    漫画 | C语言哭了,过年回家,只有我还没对象
    漫画 | CPU战争40年,真正的王者终于现身!
  • 原文地址:https://www.cnblogs.com/pengsi/p/6704328.html
Copyright © 2011-2022 走看看