UITableView

UITableView

- (void)viewDidLoad {

    [super viewDidLoad];

    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStyleGrouped];分组形式的

    self.myTableView.delegate = self;

    self.myTableView.dataSource = self;     <UITableViewDelegate,UITableViewDataSource>两个代理都要遵守

    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;     分割线

    [self.myTableView setEditing:YES];     编辑状态

    [self.view addSubview:_myTableView];

}

1.有多少段 默认为0

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 26;

}

2.有多少行,调用多次

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if ((section+1)%2 == 0) {

        return 3;

    }else{

        return 2;

    }

}

3.某段某行显示什么样子  NSIndexPath封装的两个属性sectionrow

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //重复利用机制

    NSString *cellID = @"cellID";

    //先判断是否有可以重复利用的cell

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell==nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }

    //cell显示的样式

    cell.textLabel.text = [NSString stringWithFormat:@"%ld,%ld",indexPath.section,indexPath.row];

    return cell;

}

4.配置cell高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80;

}

5.段落头部尾部

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return @"头标题";

}

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return @"尾标题";

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 30;

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 30;//不能没有,如果想没有就把值变小

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView * v =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];

    v.backgroundColor = [UIColor redColor];

    return v;

}

6.cell被点了要做什么

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];取消点击事件

}

7.设置cell是否可以编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row== 0) {

        return NO;

    }else{

        return YES;

    }

}

8.更改编辑状态的按钮

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return  UITableViewCellEditingStyleDelete;

}

9.更改删除状态下右边显示的标题

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    return @"删除";

}

10.删除状态下按钮被点

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

}

11.cell可以上下移动

-(bool)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{  

}

12.索引标题

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    NSMutableArray * titleArray = [NSMutableArray array];

    for (int i = 0; i<26; i++) {

        [titleArray addObject:[NSString stringWithFormat:@"%c",'A'+i]];

    }

    return titleArray;

}

posted @ 2016-02-22 16:37  浪味小仙女  阅读(160)  评论(0编辑  收藏  举报