ios – 如何更改UITableView节颜色和文本颜色

ios – 如何更改UITableView节颜色和文本颜色,第1张

概述我有UITableView从plist填充日期和Even Section的标题已经从UITableView中的plist填充.它给我默认的蓝色部分颜色和白色文本颜色.像这样…… -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {NSString *key = 我有UItableVIEw从pList填充日期和Even Section的标题已经从UItableVIEw中的pList填充.它给我默认的蓝色部分颜色和白色文本颜色.像这样……

-(Nsstring *)tableVIEw:(UItableVIEw *)tableVIEw TitleForheaderInSection:(NSInteger)section {Nsstring *key = nil;if ([tableVIEw isEqual:self.searchdisplayController.searchResultstableVIEw]){    key = [self.searchResults objectAtIndex:section];}else{    key = [self.mySections objectAtIndex:section];}//    Nsstring *key = [self.mySections objectAtIndex:section];return [Nsstring stringWithFormat:@"%@",key]; }

.
现在我需要更改此默认文本颜色和Section的颜色,为此我实现下面显示的代码.但它给了我自己的UIVIEw.

-(UIVIEw *)tableVIEw:(UItableVIEw *)tableVIEw vIEwForheaderInSection:(NSInteger)section{    UIVIEw *tempVIEw=[[UIVIEw alloc]initWithFrame:CGRectMake(0,200,300,244)];    tempVIEw.backgroundcolor=[UIcolor clearcolor];    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,44)];    tempLabel.backgroundcolor=[UIcolor clearcolor];     tempLabel.shadowcolor = [UIcolor blackcolor];    tempLabel.shadowOffset = CGSizeMake(0,2);    tempLabel.textcolor = [UIcolor redcolor]; //here you can change the text color of header.    tempLabel.Font = [UIFont FontWithname:@"Helvetica" size:FontSizeforheaders];    tempLabel.Font = [UIFont boldSystemFontOfSize:FontSizeforheaders];        tempLabel.text=@"header Text";    [tempVIEw addSubvIEw:tempLabel];    [tempLabel release];    return tempVIEw;}
解决方法 为了最好地自定义表部分标题的外观,你真的需要实现两个方法:你已经拥有的第一个方法,它应该可以工作,尽管结果不是很有用.

第二种方法是tableVIEw:heightForheaderInSection:,它告诉UItableVIEw新部分的高度是什么,它可以像这样简单:

- (CGfloat)tableVIEw:(UItableVIEw *)tableVIEw heightForheaderInSection:(NSInteger)section {    return 50.0f; }

编辑:根据评论,这是您的代码和定义标题高度的结果:

编辑2:如果你想要带有黑色背景的红色文本,请更改tableVIEw的代码:vIEwForheaderInSection:像这样:

-(UIVIEw *)tableVIEw:(UItableVIEw *)tableVIEw vIEwForheaderInSection:(NSInteger)section{    UIVIEw *tempVIEw=[[UIVIEw alloc]initWithFrame:CGRectMake(0,244)];    tempVIEw.backgroundcolor=[UIcolor blackcolor];    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,44)];    tempLabel.backgroundcolor=[UIcolor clearcolor];     tempLabel.textcolor = [UIcolor redcolor]; //here you can change the text color of header.    tempLabel.Font = [UIFont FontWithname:@"Helvetica" size:FontSizeforheaders];    tempLabel.Font = [UIFont boldSystemFontOfSize:FontSizeforheaders];        tempLabel.text=@"header Text";    [tempVIEw addSubvIEw:tempLabel];    [tempLabel release];    return tempVIEw;}

编辑3:好的,所以我会尝试将第一种方法的代码与第二种方法合并.它看起来像这样:

-(UIVIEw *)tableVIEw:(UItableVIEw *)tableVIEw vIEwForheaderInSection:(NSInteger)section{    UIVIEw *tempVIEw=[[UIVIEw alloc]initWithFrame:CGRectMake(0,44)];    tempLabel.backgroundcolor=[UIcolor clearcolor];     tempLabel.textcolor = [UIcolor redcolor]; //here you can change the text color of header.    tempLabel.Font = [UIFont FontWithname:@"Helvetica" size:FontSizeforheaders];    tempLabel.Font = [UIFont boldSystemFontOfSize:FontSizeforheaders];    Nsstring *key = nil;    if ([tableVIEw isEqual:self.searchdisplayController.searchResultstableVIEw])    {        key = [self.searchResults objectAtIndex:section];    }    else{        key = [self.mySections objectAtIndex:section];    }    tempLabel.text=[Nsstring stringWithFormat:@"%@",key];    [tempVIEw addSubvIEw:tempLabel];    [tempLabel release];    return tempVIEw;}

这应该返回一个具有正确标签和正确外观的表视图节标题.

编辑4:只是关于所有这些如何工作的说明:如果你使用tableVIEw:vIEwForheaderInSection:你在tableVIEw中放置的代码:TitleForheaderInSection:将被忽略.因此,您需要对section header进行整个设置,包括tableVIEw:vIEwForheaderInSection方法中的正确文本.

@H_419_55@ 总结

以上是内存溢出为你收集整理的ios – 如何更改UITableView节颜色和文本颜色全部内容,希望文章能够帮你解决ios – 如何更改UITableView节颜色和文本颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1031500.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存