我是 iOS 新手,我创建了一个 Expandable-ListView,这里我的主要要求是当我展开单元格时,我想将 UILabel textcolor 更改为“红色”颜色并保留所有单元格UILabel textcolors 必须是“ORANGE”,如下图所示,当我折叠展开的单元格时,该单元格的 UILabel textcolor 必须是“ORANGE”。
为此,我尝试了下面的代码,但是当我折叠单元格时,文本颜色没有像“橙色”一样改变,请帮助我。
我的代码:
.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",
@"apaya",
@"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)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
if ([[arrayForBool objectAtIndex:section] boolValue]) {
return section+2;
}
else
return 0;
}
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)numberOfSectionsInTableViewUITableView *)tableView
{
return [sectionTitleArray count];
}
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{
if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
return 40;
}
return 0;
}
- (CGFloat)tableViewUITableView *)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 actionselector(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
Best Answer-推荐答案 strong>
只需要替换以下行:
if(collapsedSection == section)
与
if ([[arrayForBool objectAtIndex:section] boolValue])
关于ios - 当我在 ios 中展开和折叠 TableList 单元格时如何更改 UILabel 文本颜色,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/34262697/
|