您的位置:首页 > 产品设计 > UI/UE

UITableView的一些方法

2015-11-11 20:59 405 查看
#import "RootViewController.h"

//签订tableView的协议

//两个协议 UITableViewDataSource, UITableViewDelegate

//DataSource(数据源)

@interface
RootViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorgrayColor];

    

//    UITableView

    UITableView *tableView = [[UITableViewalloc]
initWithFrame:self.view.boundsstyle:UITableViewStylePlain];

//    UITableViewStyleGrouped  分组 -默认在头部和尾部添加视图

//    UITableViewStylePlain    默认

//*********************************

//    设置代理

    

//    协议方法
    tableView.delegate =self;

//    数据源
    tableView.dataSource =self;

//*********************************

    

//***********************************************************************

//   注册重用池

//   参数1
: 注册的是什么类型的cell

//   参数2
: 重用池的标识

    [tableView registerClass:[UITableViewCellclass]
forCellReuseIdentifier:@"UITableCellIdentifier"];

//***********************************************************************    
    [self.viewaddSubview:tableView];
    [tableViewrelease];
}

//返回多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return

4;
}

//分区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return

10;
}

//如果不想显示每个section的footer ze高度返回0
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

//    当直接返回0的时候系统会认为它是无效值
所以不会做任何改变所以返回float类型的最小值

d7f9
    returnCGFLOAT_MIN;
}

//返回每个section头部的自定义视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *vv = [[UIViewalloc]
initWithFrame:CGRectMake(0,0,30,10)];

    vv.backgroundColor = [UIColorredColor];
   return vv;
}

//返回每个section的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   return
@"A";
}

#pragma mark -- 返回右侧的索引

//与section相互对应
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    return@[@"1",@"2",
@"3",@"4"];
}

#pragma mark -- cell的点击事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath {

//    取消选中状态

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];

//    打印当前的indexPath.row
   NSLog(@"%ld", indexPath.row);
}

#pragma mark -- 先执行行数
再每取一个cell都走一遍高

#pragma mark -- 返回每行高度的协议方法(默认高度为44.0)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath {

//    indexPath -- 包含row(行)和section(区)用来定位是哪个cell

    

//    当行数为第一个的时候返回200的高度
   if (indexPath.row ==0)
{
       return

200;
    }
   return

80;
}

#pragma mark -- UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

//    tableView 以区(section)划分每个区包含若干行

//    tableView 没有设置区个数的时候默认为1个区

    

//    返回10个row
   if (section ==

0) {
       return

5;
    }else
if (section ==1) {
       return

6;
    }else
if (section ==2) {
       return

7;
    }else {
       return

8;
    }

    
   return

20;
}

//每个row显示内容(cell)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {

//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"a"];

//   从重用池中获取cell,如果重用池中没有cell,系统会根据注册的cell自动创建并返回

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"UITableCellIdentifier"];

    

//设置文字
    cell.textLabel.text =@"迪丽热巴";

    cell.detailTextLabel.text =@"ABCD";
    cell.imageView.image = [UIImageimageNamed:@"Image"];
   return cell;
}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.
}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uitableview