UITableView自定义分区和自定义字母索引(包括自定义索引的字体和颜色)

今天讨论的主题是自定义UITableView,主要包括自定义分区(包括分区表头和相关属性),字母索引(index list的相关属性)。

要实现相关功能需要实现tableview delegate。

实现自定义title section的函数是:- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section;

实现自定义字母索引的函数是:-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

如下贴下示例代码(各位可参考相关函数,在自己的工程中实现自定义分区和字母索引)。


#pragmamark --

#pragmamark tableView delegate

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView

{

    int i =[m_characterIndexcount];  

    i++;

    returni;

}

 

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{

//返回分区头的高度

//数据类型必须为CGFloat

}

 

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{

     //返回分区尾的高度

    //数据类型必须为CGFloat

}

 

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

  //定义每个分区的行数

}

 

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath

{

   //每行的高度

  //数据类型必须为CGFloat

}

 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ///自定义index list/  、、字母索引

    for(UIView *indexViewin[tableView subviews])

    {

        if([[[indexViewclass]description]isEqualToString:@"UITableViewIndex"])

        {

            [indexView setFont:[UIFontsystemFontOfSize:14]];

            [indexView setBackgroundColor:[UIColorclearColor]];

        }       

    }

    /

   自定义UITableViewCell

}

 

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return m_characterIndex;

}

 

 

-(NSInteger)tableView:(UITableView*)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index{

    NSIntegercount = 1;

    for (NSString *characterinm_characterIndex) {

        if ([character isEqualToString:title] ) {

            returncount;

        }

        count++;

    }

    return 0;

}

 

-(NSString *)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{

     //分区头的名字

}

 

#pragmamark --

#pragmamark 自定义 titleof section

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

    NSString*sectionTitle=[selftableView:tableViewtitleForHeaderInSection:section];

    if(sectionTitle==nil) {

        returnnil;

    }

   

    // Create labelwith section title

    UILabel*label=[[[UILabelalloc]init]autorelease];

    label.frame=CGRectMake(13,0, 300, 31);

    label.backgroundColor=[UIColorclearColor];

    label.textColor=[UIColorwhiteColor];

    label.text=sectionTitle;

   

    // Createheader view and add label as a subview

    UIView*sectionView=[[[UIViewalloc] initWithFrame:CGRectMake(0,0, tableView.bounds.size.width,22)] autorelease];

    [sectionView setBackgroundColor:[UIColorcolorWithRed:55/255.0fgreen:159/255.0fblue:229/255.0falpha:1.0f]];

    [sectionView addSubview:label];

    returnsectionView;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值