当我在 ios 中展开和折叠 TableList 单元格时如何更改 UILabel 文本颜色
Posted
技术标签:
【中文标题】当我在 ios 中展开和折叠 TableList 单元格时如何更改 UILabel 文本颜色【英文标题】:How to change UILabel text-color when i expand and Collapse the TableList cells in ios 【发布时间】:2015-12-14 08:42:57 【问题描述】:我是 ios 新手,我创建了一个 Expandable-ListView,这里我的主要要求是当我展开单元格时,我想将 UILabel
textcolor 更改为“红色”颜色,其余所有单元格 UILabel
textcolors 必须像下面的图片一样是“橙色”,当我折叠展开的单元格时,该单元格的UILabel
textcolor 必须是“橙色”。
为此,我尝试了下面的代码,但是当我折叠单元格时,文本颜色没有像“橙色”那样改变,请帮助我。
我的代码:
.h 文件:-
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
NSMutableArray *arrayForBool;
NSArray *sectionTitleArray;
@property (weak, nonatomic) IBOutlet UITableView *expandableTableView;
@end
.m 文件:-
#import "ViewController.h"
@interface ViewController ()
UILabel *viewLabel;
BOOL collapsed;
NSInteger collapsedSection;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initialization];
#pragma mark - Initialization
-(void)initialization
arrayForBool=[[NSMutableArray alloc]init];
sectionTitleArray=[[NSArray alloc]initWithObjects:
@"Apple",
@"Strawberry",
@"Grapes",
@"Orange",
@"Banana",
@"Papaya",
@"Guava",
@"pineapple",
nil];
for (int i=0; i<[sectionTitleArray count]; i++)
[arrayForBool addObject:[NSNumber numberWithBool:NO]];
#pragma mark -
#pragma mark TableView DataSource and Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if ([[arrayForBool objectAtIndex:section] boolValue])
return section+2;
else
return 0;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellid=@"hello";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if (cell==nil)
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
/********** If the section supposed to be closed *******************/
if(!manyCells)
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=@"";
/********** If the section supposed to be Opened *******************/
else
cell.textLabel.text=[NSString stringWithFormat:@"%@ %d",[sectionTitleArray objectAtIndex:indexPath.section],indexPath.row+1];
cell.textLabel.font=[UIFont systemFontOfSize:15.0f];
cell.backgroundColor=[UIColor whiteColor];
cell.imageView.image=[UIImage imageNamed:@"point.png"];
cell.selectionStyle=UITableViewCellSelectionStyleNone ;
cell.textLabel.textColor=[UIColor blackColor];
/********** Add a custom Separator with cell *******************/
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(15, 40, _expandableTableView.frame.size.width-15, 1)];
separatorLineView.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:separatorLineView];
return cell;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return [sectionTitleArray count];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
/*************** Close the section, once the data is selected ***********************************/
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];
[_expandableTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
if ([[arrayForBool objectAtIndex:indexPath.section] boolValue])
return 40;
return 0;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 40;
#pragma mark - Creating View for TableView Section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 280,40)];
sectionView.tag=section;
for (id subview in sectionView.subviews)
if ([subview isKindOfClass:[UIImageView class]])
[subview removeFromSuperview];
else if ([subview isKindOfClass:[UILabel class]])
[subview removeFromSuperview];
viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, _expandableTableView.frame.size.width-10, 40)];
viewLabel.font=[UIFont systemFontOfSize:15];
viewLabel.text=[NSString stringWithFormat:@"List of %@",[sectionTitleArray objectAtIndex:section]];
[sectionView addSubview:viewLabel];
if (collapsedSection == section)
viewLabel.textColor = [UIColor redColor];
else
viewLabel.textColor = [UIColor orangeColor];
/********** Add a custom Separator with Section view *******************/
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(15, 40, _expandableTableView.frame.size.width-15, 1)];
separatorLineView.backgroundColor = [UIColor blackColor];
[sectionView addSubview:separatorLineView];
/********** Add UITapGestureRecognizer to SectionView **************/
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[sectionView addGestureRecognizer:headerTapped];
return sectionView;
#pragma mark - Table header gesture tapped
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
collapsedSection = gestureRecognizer.view.tag;
if (indexPath.row == 0)
collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
for (int i=0; i<[sectionTitleArray count]; i++)
if (indexPath.section==i)
[arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
[_expandableTableView reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
@end
【问题讨论】:
你想改变 Apple 1 文字的颜色吗? @Krish no @jay Bhalani 标题颜色(如上面的 Apple 列表,草莓列表)我想在单元格展开时将颜色更改为“红色”,当单元格折叠时颜色必须为“橙色” 希望您能理解我的要求 它完全取决于你的实现方式。 所以当你折叠单元格时你的问题是什么? expand 与红色文本完美搭配?我是对还是错。 【参考方案1】:只需要替换以下行:
if(collapsedSection == section)
与
if ([[arrayForBool objectAtIndex:section] boolValue])
【讨论】:
以上是关于当我在 ios 中展开和折叠 TableList 单元格时如何更改 UILabel 文本颜色的主要内容,如果未能解决你的问题,请参考以下文章