//界面佈局
UITableView *tableView_list = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, XSCREENWIDTH, XSCREENHEIGHT)];
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 alloc] initWithStyle: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 alloc] initWithFrame:CGRectMake(0, 0, tableView_list.width, 30)];
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];
}
}