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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章