TableView基础篇

来源:互联网 发布:火蓝刀锋知乎 编辑:程序博客网 时间:2024/06/15 08:00
第一,初始化
       可以使用懒加载进行初始化;(懒加载就是利用get方法进行初始化)

//界面布局

    UITableView *tableView_list = [[UITableView allocinitWithFrame:CGRectMake(00XSCREENWIDTHXSCREENHEIGHT)];

    tableView_list.backgroundColor = [UIColor clearColor];

    tableView_list.dataSource = self;

    tableView_list.delegate = self;

    tableView_list.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.view addSubview:tableView_list];


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *identify = @"Identify";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
   if (cell == nil) {
       cell = [[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   return 50;
}

//列表头部
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
     return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *backView = [[UIView allocinitWithFrame:CGRectMake(00tableView_list.width30)];
    backView.backgroundColor = ColorForViewBg;
    return backView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


第二,tableView索引的设置

索引显示设置
tableView_list.sectionIndexColor = [UIColor blueColor]; //字体颜色
tableView_list.sectionIndexBackgroundColor = [UIColor clearColor];//索引背景颜色

以下两句是索引的关键
//返回索引栏数据
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return array_section;
}

//建立索引栏和section的关联
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [array_section indexOfObject:title];
}


第三,滑动删除

首先要设置cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

显示的字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

删除对应的操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   if (editingStyle == UITableViewCellEditingStyleDelete) {
       //[self deleteAction_myCar:indexPath.row];
   }
}
0 0
原创粉丝点击