tableView的总结

//
//  ViewController.m
//  TableViewController
//
//  Created by 王妍 on 16/3/23.
//  Copyright © 2016年 com.edong. All rights reserved.
//

#import "ViewController.h"
#import "MyTableViewCell.h"
#import "MyXIBTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //1.创建tableView
    [self creatTableViewController];
    

    

    //3.注册cell
//    [self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cell"];
    //3.1 Xib 注册的cell
    [self.tableView registerNib:[UINib nibWithNibName:@"MyXIBTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell_xib"];
}

-(void)creatTableViewController
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.tableView];
    
    //2.设置代理 及实现代理方法
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //3.tableView的属性
  
    /*
     UITableViewCellSeparatorStyleNone,  没有cell直接的那条线了
     UITableViewCellSeparatorStyleSingleLine, 先在中间 但是短一点
     UITableViewCellSeparatorStyleSingleLineEtched
     */
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    //分割线的颜色
    self.tableView.separatorColor = [UIColor redColor];
    //分割线的内边距(只能设置左右距离)    上 左 下 右 (设置上下没有用)
//    self.tableView.separatorInset = UIEdgeInsetsMake(110, 200, 0, 50);
    
    //行高 (但是用代理方法设置的话 这个就不管用了)
    self.tableView.rowHeight = 200;
}
#pragma mark----tableView的代理方法
//一个组有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return 3;
}
//一共几个组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
//    return [self.dataArray[section] count];
}
//cell的重用池
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MyXIBTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_xib" forIndexPath:indexPath];
    
    //给cell赋值
//    cell.headImv.image = [UIImage imageNamed:@"1111.png"];
//    cell.nameLable.text = @"aa";
   
    return cell;
}
//头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
//尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}

//头部试图
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *headView = [[UIView alloc] init];
//    headView.frame = CGRectMake(0, 0, 0, 0);
//    headView.backgroundColor = [UIColor redColor];
//    return headView;
//}



//尾部试图
//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    UIView *footView = [[UIView alloc] init];
//    footView.frame = CGRectMake(0, 0, 0, 0);
//    footView.backgroundColor = [UIColor yellowColor];
//    return footView;
//}



//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
    
    //不同cell的高度
//    return [MyTableViewCell cellHeight];
}
//点击cell的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


//分区头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"第%ld个头标题",section];
}

//分区尾标题
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"第%ld个尾标题",section];
}

//索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return @[@"a",@"b",@"c"];
}
//指定哪些行可以被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.row % 2 == 0) {
            return YES;
        }else{
            return NO;
        }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

//一些tableView的 属性

//旁边的滚动条没有了
 self.tableView.showsVerticalScrollIndicator = NO;

//分割线的颜色

  self.tableView.separatorColor = [UIColor redColor];

 

去掉cell的点击效果

  cell.selectionStyle = 0;

 

posted @ 2016-03-23 16:05  喵喵yy  阅读(210)  评论(0编辑  收藏  举报