uitableview刷新在只有一个section情况下,怎么显示指定样式

我要如何改变侧字母的颜色在一个索引的UITableView?
我有一个字母索引和侧字母快速浏览列表以获得表视图。对于那些不熟悉,这样的:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
我的问题是,我的申请刚刚被骂得黑色。所以现在很难看到侧面的字母。
我无法弄清楚如何改变字母的颜色。我想它是“白色”,如果在所有可能的。
本文地址 :CodeGo.net/44612/
-------------------------------------------------------------------------------------------------------------------------
1. 从我可以告诉不幸的是它是不是可以自定义在索引中显示的文本的颜色,最接近我已经能够是能够修改背景颜色和索引的字体。
有代码在iPhone开发者的食谱由埃里卡丧盾它显示了如何访问UITableViewIndex视图(一类)。你可以找到对它的引用这本书的175页,如果你拥有了它。它可以访问的背景颜色和字体。你可以看到一个非官方的与此相关的类在这里。
警告这是一个使用类的所以你需要它。
下面是来自稍作修改的菜谱一个代码片段:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
for(UIView *view in [tv subviews])
if([[[view class] description] isEqualToString:@"UITableViewIndex"])
[view setBackgroundColor:[UIColor whiteColor]];
[view setFont:[UIFont systemFontOfSize:14]];
//rest of cellForRow handling...
这怎么可以访问和UITableViewIndex查看和修改它的学分。它看起来像视图没有任何子视图,所以它可能做的自定义与索引标题的数组绘制。
它并不完美,但希望它有助于一点点。
我有这个问题,只是发现了这样的一种方式,尽管这节课,所以认为一个额外的努力把它上传到App Store之前。
我应该说,我只试图与iOS5的,所以我不知道这是否会工作的版本。
我借和修改的埃里卡?桑德斯食谱例子,以便它现在更改文本颜色为红色:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
for(UIView *view in [tv subviews])
if([[[view class] description] isEqualToString:@"UITableViewIndex"])
[view performSelector:@selector(setIndexColor:) withObject:[UIColor redColor]];
//rest of cellForRow handling...
我们用下面的代码:
for(UIView *view in [tableView subviews]) {
if([view respondsToSelector:@selector(setIndexColor:)]) {
[view performSelector:@selector(setIndexColor:) withObject:[UIColor whiteColor]];
它工作得很好-这是非常相似的Mooglas答案-但事不闻不问单词“UITableViewIndex”。
如果您的最低的iOS版本更新比6.0,您sectionIndexColor物业UITableView。
颜色为表视图的索引文本。
@属性(非原子,保留)的UIColor * sectionIndexColor
表视图可以显示沿着图的边的索引,使得它
易于快速定位表中的内容。这
属性指定在这个区域显示的文本的颜色。
做一个可变数组包含备用标题标签
-(NSArray * )sectionIndexTitlesForTableView:(UITableView * )tableView
返回的数组@“”其中的引号之间的空格数决定了高燃点滚轮的宽度。
有“sectionIndexTitlesForTableView”调用update标签功能。
该函数删除您之前从他们的superviews创建然后创建并添加然而需要许多标签阵列中的所有标签。然后将它们添加到表的父视图。
这些都是将标签贴在正确的地方所需要的线路。
rect.origin.x = table.frame.origin.x+table.frame.size.width-rect.size.width-2;
rect.origin.y = 5+table.frame.origin.y+counter *(280-rect.size.height-([customIndexLabels count]-1))/([customIndexLabels count]-1);
if ([customIndexLabels lastObject]==thisLabel)
rect.origin.y-=10;
希望有所帮助。它并不完美我只是不关心不够,要自己解决
的主要问题是,最后一个标签的间距是不均匀的。
这在我的AppDelegate和它的工作在iOS 7:
[[UITableView的外观] setTintColor:[的UIColor purpleColor];
下面是我发现就在身边索引栏的背景色最好的解决方案。它工作在IOS7,可能iOS6的。
添加到您的viewDidLoad中
if ([_tableView respondsToSelector:@selector(setSectionIndexColor:)]) {
_tableView.sectionIndexBackgroundColor = [UIColor clearColor];
_tableView.sectionIndexTrackingBackgroundColor = [UIColor whiteColor];
第一个是默认的颜色,二是背景颜色,当你滚动。
本文标题 :我要如何改变侧字母的颜色在一个索引的UITableView?
本文地址 :CodeGo.net/44612/
继续浏览 :
您可以会感兴趣的文章:1.
Copyright (C) 2014 CodeGo.net 沪ICP备号 联系电邮: (#=@)【iOS7的一些总结】9、用列表显示内容(上):列表视图UITableView - 推酷
【iOS7的一些总结】9、用列表显示内容(上):列表视图UITableView
列表视图,顾名思义就是将数据的内容用列表的形式显示在屏幕上的视图。在ios中列表视图以UITableView实现,这个类在实际应用中非常的频繁,但是对于初学者来说不是非常容易理解。这里将UITableView的主要用法总结一下以备查。
UITableView定义在头文件UITableView.h中,具体的定义可以查看
从定义中可以看出,UITableView继承自UIScrollView类,因此在支持方便地显示列表数据的同时,还天生支持垂直滚动操作。组成列表的每一个元素称为UITableViewCell实例。一个
UITableViewCell也是应用非常广泛的类,定义可见
。在具体的使用过程中,可以创建一个独立的UITableView,也可以直接创建一个UITableViewController。这里主要记录创建UITableView的方法,下篇记录通过列表视图控制器使用UITableView。
UITableView类中定义了style属性:
@property(nonatomic, readonly) UITableViewStyle style
UITableView都可以选择两种style之一,即分组模式和平面模式,这两种模式定义在枚举变量UITableViewStyle中:
typedef enum {
UITableViewStylePlain,
UITableViewStyleGrouped
} UITableViewS
每一个列表视图的组成都是相似的,都是由一个表头视图+表体+表尾视图构成。其中表头和表尾两个视图默认为nil,需要时可以创建自定义视图添加到表头和表尾。定义如下:
@property(nonatomic, retain) UIView *tableHeaderV
@property(nonatomic, retain) UIView *tableFooterV
除表头和表尾之外,表体则由一串UITableViewCell(下面简称cell)构成。如果是分组表视图,则多个
UITableViewCell构成一个section,每个section也有头和尾视图。
下面简单新建一个demo展示一下如何创建一个
UITableView。这里假定大家都了解xcode的基本操作,所以就不再一步一步地截图了,简单叙述即可。不懂得可以去百度一下“xcode新建工程”。
新建一个single view application,在新生成的ViewController.m文件中重写loadView方法,新建一个UITableView视图。(别忘了把alloc的视图在dealloc函数中释放。)
- (void)loadView
CGFloat width = [UIScreen mainScreen].bounds.size.
CGFloat height = [UIScreen mainScreen].bounds.size.
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width,height)];
self.view = backgroundV
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain];
[self.view addSubview:_tableView];
[_tableView release];
编译运行,显示如下图:
表视图的协议方法——这是非常重要的部分,因为我们创建一个表视图,目的就是让视图可以显示数据,否则一个空空的表视图与废物无二。表视图所定义的协议方法由代理方法delegate和数据源方法data source方法组成。委托方法一般用于实现个性化处理表视图的基本样式(如单元格的高度等)以及捕捉单元格选中的响应;数据源方法用于完成表中的数据,如指定单元格数,以及创建每一个单元格。
要实现代理和数据源方法,首先需要让当前视图控制器支持UITableViewDelegate和UITableViewDataSource协议。做如下修改:
@interface ViewController : UIViewController&UITableViewDelegate,UITableViewDataSource&
并且在tableView创建完成后,将
tableView的delegate和dataSource设置为self,即委托给当前视图控制器来控制表视图的数据显示和响应。
_tableView.delegate =
_tableView.dataSource =
delegate和data source协议有两个方法是必须实现的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexP
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)
这两个方法分别用于生成每一个cell,以及指定当前section共有多少行。实现这两个方法是想要在表视图中显示数据必须实现的最低要求。
我们在视图控制器头文件中声明一个NSArray *model(retain属性),并在viewDidLoad中将[UIFont familyNames]赋给这个属性。
在视图控制器中实现这两个代理方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [_model count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *identify = @&TableViewCell&;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (nil == cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
cell.textLabel.text = self.model[indexPath.row];
在cellForRowAtIndexPath方法中,首先会检查是否有闲置的单元格,如果没有闲置的单元格,则会新建一个cell并将其返回。参数indexPath表示目前正在创建的单元格位于整个表视图的第几行。
编译,运行,显示结果:
如果希望实现对选中某个单元格的响应,只需要实现下面代理方法即可。在代理方法中可以实现创建新的视图控制器并控制其加载到屏幕上。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexP
已发表评论数()
&&登&&&陆&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见UITableView 系列三 :分类显示和改变外观 (实例) - Just Code - ITeye技术网站
博客分类:
1. 分类显示 sections
在之前的文章一文中,已经示范如何在 UITableView 中设定所要显示的资料,以及分别显示这些资料的细节,但是如果资料比数太多时该怎么办?你可以参考本篇文章的做法,将资料做分类的处理,并且建立快速索引,让使用者能以最短的时间找到所需要的资料。资料分类的概念动态表格的内容多半是存放在阵列当中方便资料的存取,如果你有好几类不同比的资料,你可以将这些资料分别存放在不同的阵列里,最后再使用一个 NSMutableArray 将这些存放不同资料的阵列包起来,之后我们只要针对这个 NSMutableArray 做操作即可。(以下是沿用之前文章的程式码做修改)
//资料初始化
roleArray = [[NSArray alloc] initWithObjects:@"野蛮人", @"法师", @"弓箭手", @"盗贼", @"德鲁伊", @"骑士", nil];
monsterArray = [[NSArray alloc] initWithObjects:@"哥布林战士", @"哥布林护卫", @"哥布林军官", @"哥布林王", @"黑暗德鲁伊", @"狼人", @"傀儡护卫", @"傀儡领主", @"蜘蛛", @"蝙蝠", nil];
heroicaArray = [[NSMutableArray alloc] initWithObjects:roleArray, monsterArray, nil];
UITableView Sections 的设定如果要将资料作分类显示,可以使用以下的内建方法函式,并回传一个 NSInteger,告诉 UITableViewController 你想将资料分成几类。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [heroicaArray count];
程式码到此就已经算是完成资料的分类,后续的动作就是要显示这些分类好的资料,大致的程式码都和之前的文章差不多,只是操作的物件不同,可以透过方法函式所得到的 section 参数,决定于目前是要处理那一类的资料。
//设定每一类的资料笔数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[heroicaArray objectAtIndex:section] count];
//设定每一类的资料内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//制作可重復利用的表格栏位Cell
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//设定栏位的内容与类型
cell.textLabel.text = [[heroicaArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureB
分类标题与快速索引分类的标题可以家在分类的开头或是结尾,同样是透过方法函式所得到的 section 参数,来确认目前所在的分类。
//设定分类开头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
return @"英雄角色";
return @"怪物角色";
return @"";
//设定分类结尾标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
另外,建立类似电话簿的快速索引,则可以透过下列内建方法函式,回传一个快速索引的阵列,阵列内容的顺序,就是你分类的顺序。
//建立快速索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSArray *index = [[NSArray alloc] initWithObjects:@"英雄", @"怪物", @"武器", @"道具", @"战利品", @"其他", nil];
比较好的做法在上述分类标题与快速索引的部分,使用 switch 与静态的数值来做设定,其实这不是很恰当的做法,尤其当你的资料笔数非常庞大的时候,比较好的建议是将这些资讯同样放入阵列里面,且动态存取它们,来完成设定标题与建立索引阵列。另外要注意的是,虽然是好几类不同的资料,但是他们最好还是能拥有相同的属性,即使该属性为 nil。例如 A 类型的资料有颜色属性,但是 B 类型没有或是不需要,但是仍需为 B 类型的资料保留颜色属性,即使它们的值都是 nil,这样的观念有点类似于多型 Polymorphism,这样不但可以减少程式码的撰写,对于表格内的资料也能保持一致性。
2. 改变外观
UITableView 所制作出来的应用程式在使用上多半大同小异,它们之间最大的不同还是在表格的呈现方面,如何设计出具有独特外观的 UITableView,才是令人头痛的问题,通常是选择制作一个全新的 UITableViewCell 来使用,但是你也可以採用比较简单的做法,使用内建的方法函式来做些微的改变,方式如下。Table View整个 Table View 能改变的东西实在不多,多半都是更改背景,但是当你更改背景颜色之后就会发现 Cell 与 Cell 之间会多出一条白线 Separator,你可以参考下列程式码改变它的颜色或是移除不显示。
//改变Separator颜色
[self.tableView setSeparatorColor:[UIColor orangeColor]];
//移除Separator
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Table View CellTable View Cell 本身就提供一些不同的类型可供选择,如下图,你可以藉由选择不同的类型来改变文字在 Cell 中编排的方式。
Attributes 中的 Style 属性另外如果想要在 Cell 中增加其它元件时,可以使用 addSubview 的方法函式来添加新的元件,例如在下列程式码中,除了设定左右的图像之外,还自行新增了一个 UILabel 放在其中。
//设定文字背景为透明
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
//设定Cell中左边的图片
cell.imageView.image = [UIImage imageNamed:[[heroicaArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row + 1]];
//设定Cell中右边的连结图片
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dice.png"]];
//增加UILabel
UILabel *subtitle = [[UILabel alloc] initWithFrame:CGRectMake(95.0, 45.0, 200.0, 20.0)];
[subtitle setTextColor:[UIColor colorWithHue:1.0 saturation:1.0 brightness:1.0 alpha:0.5]];
[subtitle setBackgroundColor:[UIColor clearColor]];
[subtitle setFont:[UIFont systemFontOfSize:12.0]];
[subtitle setText:@"还可以放注解唷"];
[cell addSubview:subtitle];
//设定背景
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellBG.png"]]];
Section透过下列内建的方法函式,可以自行更改 Section 的标题内容。
//设定开头的分类样式
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[sectionView setBackgroundColor:[UIColor brownColor]];
//增加UILabel
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 18)];
[text setTextColor:[UIColor blackColor]];
[text setBackgroundColor:[UIColor clearColor]];
[text setText:[[heroicaArray objectAtIndex:section] objectAtIndex:0]];
[text setFont:[UIFont boldSystemFontOfSize:16.0]];
[sectionView addSubview:text];
return sectionV
//设定结尾的分类样式
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
Section Index表格分类的快速索引虽然没有内建的方法函式可供设定外观使用,但是你仍然可以透过自制的索引介面并配合下列程式码,将表格切换到所想要的分类上。
CGRect sectionRect = [self.tableView rectForSection:1];
[self.tableView scrollRectToVisible:sectionRect animated:YES];
备註如果表格个对应的资料结构有任何问题,可以在「索引式搜索」中的「元件设定」分类里找到所有有关 UITableView 的,查阅其他有关表格的设定方式。
浏览: 3077644 次
来自: 洛杉矶
这个跟参数怎么设置没有任何关系,如果你设置了文件夹保护,那么当 ...
怎么通过 php post/get访问这个网址啊,http:/ ...
图片还加密 ,你还是中国人么,连个马赛克都不如
wang 写道删除时间的时候怎么获得对应时间的 ...
.transparent_class {
backgr ...温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
阅读(698)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'UITableView',
blogAbstract:'一、介绍1,UITableView一个Scrollable(可滑动的)列表,它展示的内容项可以被分到不同的sections中。&&&&& UITableView只有一列,只允许垂直滚动。&&&&& UITableView由sections组成。每个section可以有一个header和一个footer(显示文本或图像),然后包含不定数量的row。很多表视图只有一个section和不可见的header和footer。UIKit 通过indexPath识别section和row的位置。Sections为0 to n-1;rows为 0 to n-1。 &&&&& UITableView可以有它自己的header和f',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:4,
publishTime:6,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'无',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

我要回帖

更多关于 uitableview分组 的文章

 

随机推荐