UITableView 帶右側索引以及Section
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return sectionName;
}
//section (標籤)標題顯示
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionName objectAtIndex:section];
}
//標籤數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionName.count;
}
// 設定section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40;
}
//點選右側索引表項時呼叫
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSString *key = [sectionName objectAtIndex:index];
NSLog(@"sectionForSectionIndexTitle key=%@",key);
if (key == UITableViewIndexSearch) {
[tableViewProj setContentOffset:CGPointZero animated:NO];
return NSNotFound;
}
return index;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,320, 400)];
view.backgroundColor = [UIColor whiteColor];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(8, 13, 70, 20)];
title.text = [sectionName objectAtIndex:section];
title.textColor=[UIColor whiteColor];
title.font = [UIFont systemFontOfSize:15];
title.textAlignment= NSTextAlignmentCenter;
title.backgroundColor = Rgb2UIColor(233,82,62);
[view addSubview:title];
return view;
}
// 設定cell的高度
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
CGSize size = CGSizeMake(215,2000);
CGSize labelsize = [[detailArr objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
CGFloat height = labelsize.height+10;
if (height>30) {
return height;
}
}
return 30;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0)
{
return detailArr.count;
}else
{
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailViewCell" forIndexPath:indexPath];
if (indexPath.section==0) {
cell.title.text = [nameArr objectAtIndex:indexPath.row];
cell.detaillab.text = [detailArr objectAtIndex:indexPath.row];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}