请教tableview设置section中section index title的背景如何修改

博客分类:
今天逛devdiv发现这个了这个东西然后自己就写了个demo看看
以前看iphone app经常在table右边有一个列表。可以用于快速选择。
这个就是sectionIndexTitle了
设置sectionIndex
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
此时是默认对应 顺序对应 sectionIndex根据顺序对应 到section
修改sectionIndex对应
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
通过传入的传入每个sectionIndex的title,index 来设置这个sectionIndex 对应的section。
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//用于设置sectionIndexTitle
//返回要为一个内容为NSString 的NSArray 里面存放
//默认情况下 section Title根据顺序对应 section 【如果不写tableView: sectionForSectionIndexTitle: atIndex:的话】
NSMutableArray* a=[NSMutableArray array];
for(CountryVO *c in self.countryDy){
[a addObject: [c.countryName substringToIndex:1]];
return b=@[@"1",@"2"];
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
//传入 section title 和index 返回其应该对应的session序号。
//一般不需要写 默认section index 顺序与section对应。除非 你的section index数量或者序列与section不同才用修改
浏览 11941
浏览: 299567 次
来自: 深圳/湛江
有源码么,想学习
借我一双翅膀 写道大哥,求指教啊?
大哥,求指教啊
是不是要加个权限?
=-=表示 如果你上次做那个。。。我就用 ...iOS学习之UITableView(三):进阶篇索引,标记和自定义的table - 为程序员服务
iOS学习之UITableView(三):进阶篇索引,标记和自定义的table
一、带索引目录的表视图
图1 带索引的列表
&img alt="" height="569" src="/imgr?src=http%3A%2F%2Fstatic.oschina.net%2Fuploads%2Fspace%2F7%2F094514_wdMi_735123.png&r=http%3A%2F%2Fmy.oschina.net%2Fjoanfen%2Fblog%2F204503" width="321"/&
本想获取通讯录中得名字,但为了用模拟器调试方便,就写死了数据,所以也只写了部分字母,总之有那么点意思就成
&pre&&code&@interface ViewController ()&UITableViewDataSource,UITableViewDelegate&
NSArray sectionT // 每个分区的标题
NSArray contentsA // 每行的内容
}&/code&&/pre&
&pre&&code&/* @brief 准备数据源 在viewDidLoad方法中调用/
- (void)readySource
sectionTitles
= [[NSArray alloc] initWithObjects:
@"A",@"C",@"F",@"G",@"H",@"M",@"S",@"T",@"X",@"Z", nil];
contentsArray
= [[NSArray alloc] initWithObjects:
@[@"阿伟",@"阿姨",@"阿三"],
@[@"蔡芯",@"成龙",@"陈鑫",@"陈丹",@"成名"],
@[@"芳仔",@"房祖名",@"方大同",@"芳芳",@"范伟"],
@[@"郭靖",@"郭美美",@"过儿",@"过山车"],
@[@"何仙姑",@"和珅",@"郝歌",@"好人"],
@[@"妈妈",@"毛主席"],
@[@"孙中山",@"沈冰",@"婶婶"],
@[@"涛涛",@"淘宝",@"套娃"],
@[@"小二",@"夏紫薇",@"许巍",@"许晴"],
@[@"周恩来",@"周杰伦",@"张柏芝",@"张大仙"],nil];
}&/code&&/pre&
③显示索引
&pre&&code&// 每个分区的页眉
-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
return [sectionTitles objectAtIndex:section];
// 索引目录
-(NSArray )sectionIndexTitlesForTableView:(UITableView )tableView
return sectionT
&/code&&/pre&
④点击索引,跳转到点击的分区
&pre&&code&// 点击目录
-(NSInteger)tableView:(UITableView )tableView sectionForSectionIndexTitle:(NSString )title atIndex:(NSInteger)index
// 获取所点目录对应的indexPath值
NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
// 让table滚动到对应的indexPath位置
[tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}&/code&&/pre&
二、可以进行行标记的表视图
图2 可标记的列表
&img alt="" src="/imgr?src=http%3A%2F%2Fstatic.oschina.net%2Fuploads%2Fspace%2F8%2F154543_uOTu_735123.png&r=http%3A%2F%2Fmy.oschina.net%2Fjoanfen%2Fblog%2F204503"/&
②在cellForRow方法中,将Cell的accessoryType设置为None
&pre&&code& // 定义其辅助样式
cell.accessoryType
= UITableViewCellAccessoryN&/code&&/pre&
③在didSelectRow方法中
&pre&&code&// 点击行事件
-(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
// 获取点击行的cell
UITableViewCell *cell
= [tableView cellForRowAtIndexPath:indexPath];
// 如果cell已经被标记
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
// 取消标记
cell.accessoryType
= UITableViewCellAccessoryN
// 如果cell未标记
// 标记cell
cell.accessoryType
= UITableViewCellAccessoryC
// 取消选中效果
[tableView deselectRowAtIndexPath:indexPath animated:YES];
&/code&&/pre&
此时,点击行即可选中,取消选中,但是滚动一下视图吧,你会发现下面某些未被点击的行也已经被标记了,这是因为cell的重用机制造成的,在第一篇文章中就这个问题有提到过
④解决cell重用问题,在cellForRow方法中,定义cellIdetifier时,将其每一行都定义为不同的值,就不会出现覆盖,重复等现象了,但是这个方法太过粗暴,并不是最好的解决办法,情急之下可以先用,然后再慢慢调试Table上的数据
&pre&&code&NSString cellIdentifier = [NSString stringWithFormat:@"cellIdentifier%d%d",indexPath.row,indexPath.section];&/code&&/pre&
三、定制表视图的每一行内容
①我们做一个类似网易新闻客户端的新闻列表的table,如图3;简易效果图,如图4
网易新闻效果
图4 demo效果
&img alt="" src="/imgr?src=http%3A%2F%2Fstatic.oschina.net%2Fuploads%2Fspace%2F8%2F161135_SMSs_735123.png&r=http%3A%2F%2Fmy.oschina.net%2Fjoanfen%2Fblog%2F204503"/&
&img alt="" src="/imgr?src=http%3A%2F%2Fstatic.oschina.net%2Fuploads%2Fspace%2F3%2FmIb_735123.png&r=http%3A%2F%2Fmy.oschina.net%2Fjoanfen%2Fblog%2F204503"/&
②数据源,在interface中声明
&pre&&code&NSMutableArray news_MA// 新闻内容数据源&/code&&/pre&
新建一个model类,命名为"newsModel",存放每一项数据
newsModel.h如下,.m中没有添加其他代码,如果需要拷贝,可以重载copyWithZone方法,参考
&a href="http://my.oschina.net/joanfen/blog/135053" rel="nofollow" target="_blank"&
http://my.oschina.net/joanfen/blog/135053
&pre&&code&#import &Foundation/Foundation.h&
typedef NS_ENUM(NSInteger, NEWSReportType){
NEWSReportOrdinary, // 普通新闻
NEWSReportExclusive,// 独家新闻
NEWSReportSpecial,
// 专题新闻
@interface newsModel : NSObject
@property (nonatomic, copy)NSString *
@property (nonatomic, copy)NSString *
@property (nonatomic, copy)NSString *
@property (nonatomic, assign)NSInteger
news_replyNo;
//跟帖数量
@property (nonatomic, assign)NEWSReportType reportT
//报道类型
@end&/code&&/pre&
在viewDidLoad方法中
&pre&&code&
news_MArray = [[NSMutableArray alloc] init];
for(NSInteger index =0; index&10; index++){
newsModel *model
= [[newsModel alloc] init];
model.news_image
= [NSString stringWithFormat:@"%d.jpg",index+1];
model.news_title
= @"曾在月光之下望烟花";
model.news_summary
= @"曾共看夕阳渐降下 我怎么舍得去放下 要怎么舍得去放下";
model.news_replyNo
= index+196;
model.reportType
= index%3;
[news_MArray addObject:model];
&/code&&/pre&
&pre&&code&// 每个分区行数
-(NSInteger)tableView:(UITableView )tableView numberOfRowsInSection:(NSInteger)section
return [news_MArray count];
}&/code&&/pre&
④自定义cell上控件
在cellForRow方法中if(cell==nil)前
&pre&&code&
/**自定义cell*/
newsModel model
= [news_MArray objectAtIndex:indexPath.row];
UIImageView *
//1.添加imageView
//2.添加标题Label
//3.添加摘要Label
//4.添加跟帖数量Label
//5.属于专题或者独家报道,进行标记
/********************/&/code&&/pre&
在if(cell==nil)内
&pre&&code&/**自定义cell***/
//1.添加imageView
CGRect imageViewF
= CGRectMake(5, 5, 85, 65);
image_view
= [[UIImageView alloc] initWithFrame:imageViewF];
[cell addSubview:image_view];
//2.添加标题Label
CGRect titleLabelF
= CGRectMake(95, 5, 230, 24);
title_label
= [[UILabel alloc] initWithFrame:titleLabelF];
title_label.font
= [UIFont systemFontOfSize:16];//字体大小
[cell addSubview:title_label];
//3.添加摘要Label
CGRect summaryLabelF
= CGRectMake(97, 27, 210, 40);
summary_label
= [[UILabel alloc] initWithFrame:summaryLabelF];
summary_label.font
= [UIFont systemFontOfSize:12];
// 字体大小
summary_label.textColor
= [UIColor darkGrayColor];
// 文字颜色
summary_label.numberOfLines = 2;
[cell addSubview:summary_label];
//4.跟帖数量Label
CGRect replyNoLabelF
= CGRectMake(210, 45, 95, 24);
replyNo_label
= [[UILabel alloc] initWithFrame:replyNoLabelF];
replyNo_label.font
= [UIFont systemFontOfSize:12];
// 字体大小
replyNo_label.textColor
= [UIColor darkGrayColor];
// 文字颜色
replyNo_label.textAlignment = NSTextAlignmentR
// 文字右对齐
//5.专题extraView
CGRect extraViewF
= CGRectMake(270, 50, 28, 14);
extra_view
= [[UIButton alloc] initWithFrame:extraViewF];
extra_view.titleLabel.font = [UIFont boldSystemFontOfSize:10];
[extra_view setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// 普通新闻,只添加跟帖数量
if (model.reportType==NEWSReportOrdinary) {
[cell addSubview:replyNo_label];
// 专题新闻,添加专题标志,并添加跟帖数量
else if(model.reportType == NEWSReportSpecial){
// 设置背景色
extra_view.backgroundColor = [UIColor colorWithRed:120.0/255.0 green:170.0/255.0 blue:245.0/255.0 alpha:1.0];
[extra_view setTitle:@"独家" forState:UIControlStateNormal];// 设置标题
[cell addSubview:extra_view];
replyNo_label.frame = CGRectMake(170, 45, 95, 24);
// 改变跟帖数量Label的坐标
[cell addSubview:replyNo_label];
// 添加跟帖数量Label
// 独家新闻,只添加独家标志
else if(model.reportType == NEWSReportExclusive){
extra_view.backgroundColor = [UIColor redColor];
// 设置背景颜色
[extra_view setTitle:@"专题" forState:UIControlStateNormal]; // 设置标题
[cell addSubview:extra_view];
// 添加到cell
/*****/&/code&&/pre&
在if(cell==nil)后
&pre&&code&/自定义cell***/
[image_view setImage:[UIImage imageNamed:model.news_image]];// 设置图片
title_label.text
= model.news_
// 设置标题
summary_label.text
= model.news_
// 设置小标题
replyNo_label.text
= [NSString stringWithFormat:@"%d 跟帖",model.news_replyNo];// 设置跟帖数量
/**/&/code&&/pre&
⑤设置行高
&pre&&code&-(CGFloat) tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 75;
}&/code&&/pre&
&a href="http://www.oschina.net/action/code/download?code=33723&id=48636" rel="nofollow" target="_blank"&
http://www.oschina.net/action/code/download?code=33723&id=48636
芳仔小脚印的博客
原文地址:, 感谢原作者分享。
您可能感兴趣的代码[ios]如何更改字体样式和背景颜色在 titleForHeaderInSection 方法中
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
经过长时间阅读和检查代码,我很自豪有一个自定义的表节与节标题,查看所有填充从核心数据对象。现在我会需要自定义的节标题和背景颜色。我已经亲眼看过但在 viewForHeaderInSection 方法。不是是可能里面我 titleForHeaderInSection 方法吗?在这里你有我的方法:
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
id &NSFetchedResultsSectionInfo& theSection = [[self.fetchedResultsController sections]objectAtIndex:section];
NSString *sectionname = [theSection name];
if ([sectionname isEqualToString:@"text 1"]){
return @"Today";
else if ([sectionname isEqualToString:@"text 2"]){
return @"Tomorrow";
if ([[self.fetchedResultsController sections]count]&0){
id&NSFetchedResultsSectionInfo& sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo name];
解决方法 1:
在这里使用的示例,您现有的代码设置标题文本,但您可以使用 UITableViewHeaderFooterView 来调整外观:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
static NSString *header = @"customHeader";
UITableViewHeaderFooterView *vH
vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
if (!vHeader) {
vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header];
vHeader.textLabel.backgroundColor = [UIColor redColor];
vHeader.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];
如果你想,你可以甚至子类 UITableViewHeaderFooterView 就像你会子类 UITableViewCell 自定义外观,更进一步。}

我要回帖

更多关于 tableview indexpath 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信