ios uitableview如何显示某个部分?
其实还有一个类似于系统自带通讯录的版块样式,是以UITableViewStylePlain的模式实现的;
例子如下:
//
// HomeViewController.h
// TestTableview
//
//由海峰于13-9-12创建。
//版权所有(c) 2065438+2003海峰。版权所有
//
#导入
@ interface home view controller:uiview controller & lt;& lt/span>。UITableViewDelegate,UITableViewDataSource & gt{
UITableView * listTableView
NSArray * sectionTitleArray
}
@end
//
// HomeViewController.m
// TestTableview
//
//由海峰于13-9-12创建。
//版权所有(c) 2065438+2003海峰。版权所有
//
#导入“HomeViewController.h”
@interface HomeViewController()
@end
@实现HomeViewController
-(id)initWithNibName:(ns string *)nibNameOrNil bundle:(ns bundle *)nibBundleOrNil
{
self =[super initwith nibname:nibNameOrNil bundle:nibBundleOrNil];
如果(自己){
//自定义初始化
}
回归自我;
}
- (void)viewDidLoad
{
【超级viewDidLoad】;
//在加载视图后进行任何附加设置。
section title array =[NSArray arrayWithObjects:@ " 1-10 ",@"11-20 ",@"21-30 ",@"31-40 ",@"41-50 ",@"51-60 ",@"61-70
UITableView * TV =[[UITableView alloc]initWithFrame:self . view . bounds];
tv.dataSource = self
tv.delegate = self
listTableView = tv
【self . view addSubview:TV】;
ui view * h view =[[ui view alloc]initWithFrame:CGRectMake(0,0,320.f,200 . f)];
h view . background color =[ui color orange color];
list table view . table header view = hview;
}
//右索引的字节数(如果未实现,则不显示右索引)
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)table view {
返回sectionTitleArray
}
//部分(标签)标题显示
-(ns string *)table view:(UITableView *)table view titleForHeaderInSection:(NSInteger)section {
return[sectionTitleArray objectAtIndex:section];
}
//标签的数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)table view {
返回10;
}
//设置节的高度。
-(CG float)table view:(UITableView *)table view heightForHeaderInSection:(NSInteger)section {
if (section == 0) {
返回80;
}
返回20;
}
//单击右索引表项时调用。
-(NSInteger)table view:(UITableView *)table view sectionForSectionIndexTitle:(ns string *)title at index:(NSInteger)index {
ns string * key =[section title array objectAtIndex:index];
NSLog(@ " sectionForSectionIndexTitle key = % @ ",key);
if(key = = UITableViewIndexSearch){
[list table view setcontent offset:CGPointZero animated:NO];
返回NSNotFound
}
回报指数;
}
-(ui view *)table view:(UITableView *)table view viewForHeaderInSection:(NSInteger)section {
UIView * v = nil
if (section == 0) {
v =[[ui view alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,80)];
[v set background color:[ui color gray color]];
UILabel * label title =[[UILabel alloc]initWithFrame:CGRectMake(50.0 f,10.0f,200.0f,30.0 f)];
[label title setbackground color:[ui color clear color]];
label title . text alignment = NSTextAlignmentCenter;
LabelTitle.text = @ "第一节自定义页面";
[v addSubview:label title];
}
回归v;
}
//设置单元格的高度。
-(CG float)table view:(UITableView *)table view height for rowatdinexpath:(NSIndexPath *)indexPath
{
返回44;
}
-(NSInteger)table view:(ui table view *)table view number of rowsin section:(NSInteger)section {
返回10;
}
-(UITableViewCell *)table view:(UITableView *)table view cell for rowatindexpath:(NSIndexPath *)index path {
static ns string * detail indicated = @ " table cell ";
UITableViewCell * cell =[table view dequeuereusablecellwith identifier:detail indicated];
if (cell == nil) {
cell =[[UITableViewCell alloc]init with style:UITableViewCell style default reuse identifier:detail indicated];
cell . tag = index path . row;
}
cell . text label . text =[ns string string with format:@ " % d ",10 * index path . section+index path . row+1];
返回单元格;
}
-(void)direceivememorywarning
{
【超级didReceiveMemoryWarning】;
//释放任何可以重新创建的资源。
}
@end