ios uitableview怎么显示部分section

 我来答
育知同创教育
2016-04-23 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
通常大家都是用UITableView的两种形式,UITableViewStylePlain,UITableViewStyleGrouped;
其实还有一种类似系统 自带通讯录的那种 Section 样式 在UITableViewStylePlain这种模式下实现;

例子如下:
//
// HomeViewController.h
// TestTableview
//
// Created by haifeng on 13-9-12.
// Copyright (c) 2013年 haifeng. All rights reserved.
//

#import

@interface HomeViewController : UIViewController<</span>UITableViewDelegate,UITableViewDataSource>{

UITableView *listTableView;
NSArray *sectionTitleArray;
}

@end

//
// HomeViewController.m
// TestTableview
//
// Created by haifeng on 13-9-12.
// Copyright (c) 2013年 haifeng. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

sectionTitleArray = [NSArray arrayWithObjects:@"1-10",@"11-20",@"21-30",@"31-40",@"41-50",@"51-60",@"61-70",@"71-80",@"81-90",@"91-100", nil];

UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds];
tv.dataSource = self;
tv.delegate = self;
listTableView = tv;
[self.view addSubview:tv];

UIView *hview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320.f, 200.f)];
hview.backgroundColor = [UIColor orangeColor];
listTableView.tableHeaderView = hview;
}

//右边索引 字节数(如果不实现 就不显示右侧索引)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

return sectionTitleArray;
}

//section (标签)标题显示
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

return [sectionTitleArray objectAtIndex:section];
}

//标签数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 10;
}

// 设置section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) {
return 80;
}
return 20;
}

//点击右侧索引表项时调用
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

NSString *key = [sectionTitleArray objectAtIndex:index];
NSLog(@"sectionForSectionIndexTitle key=%@",key);
if (key == UITableViewIndexSearch) {
[listTableView setContentOffset:CGPointZero animated:NO];
return NSNotFound;
}

return index;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

UIView *v = nil;
if (section == 0) {
v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
[v setBackgroundColor:[UIColor grayColor]];

UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 10.0f, 200.0f,30.0f)];
[labelTitle setBackgroundColor:[UIColor clearColor]];
labelTitle.textAlignment = NSTextAlignmentCenter;
labelTitle.text = @"第一个section 定制页面";
[v addSubview:labelTitle];
}

return v;
}

// 设置cell的高度
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString *detailIndicated = @"tableCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detailIndicated];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:detailIndicated];
cell.tag = indexPath.row;
}
cell.textLabel.text = [NSString stringWithFormat:@"%d",10*indexPath.section + indexPath.row +1];

return cell;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式