TableView基礎篇

第一,初始化
       可以使用懶加載進行初始化;(懶加載就是利用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];
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章