ios - 当我在 ios 中展开和折叠 TableList 单元格时如何更改 UILabel 文本颜色
<p><p>我是 iOS 新手,我创建了一个 Expandable-ListView,这里我的主要要求是当我展开单元格时,我想将 <code>UILabel</code> textcolor 更改为“红色”颜色并保留所有单元格<code>UILabel</code> textcolors 必须是“ORANGE”,如下图所示,当我折叠展开的单元格时,该单元格的 <code>UILabel</code> textcolor 必须是“ORANGE”。 </p>
<p>为此,我尝试了下面的代码,但是当我折叠单元格时,文本颜色没有像“橙色”一样改变,请帮助我。</p>
<h2>我的代码:</h2>
<h2>.h 文件:-</h2>
<pre><code>#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray*arrayForBool;
NSArray *sectionTitleArray;
}
@property (weak, nonatomic) IBOutlet UITableView *expandableTableView;
@end
</code></pre>
<h2>.m 文件:-</h2>
<pre><code>#import "ViewController.h"
@interface ViewController ()
{
UILabel *viewLabel;
BOOL collapsed;
NSInteger collapsedSection;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
;
// Do any additional setup after loading the view, typically from a nib.
;
}
#pragmamark - Initialization
-(void)initialization
{
arrayForBool=[init];
sectionTitleArray=[initWithObjects:
@"Apple",
@"Strawberry",
@"Grapes",
@"Orange",
@"Banana",
@"Papaya",
@"Guava",
@"pineapple",
nil];
for (int i=0; i<; i++) {
];
}
}
#pragma mark -
#pragma mark TableView DataSource and Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([ boolValue]) {
return section+2;
}
else
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid=@"hello";
UITableViewCell *cell=;
if (cell==nil) {
cell=[initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
BOOL manyCells= [ boolValue];
/********** If the section supposed to be closed *******************/
if(!manyCells)
{
cell.backgroundColor=;
cell.textLabel.text=@"";
}
/********** If the section supposed to be Opened *******************/
else
{
cell.textLabel.text=,indexPath.row+1];
cell.textLabel.font=;
cell.backgroundColor=;
cell.imageView.image=;
cell.selectionStyle=UITableViewCellSelectionStyleNone ;
}
cell.textLabel.textColor=;
/********** Add a custom Separator with cell *******************/
UIView* separatorLineView = [ initWithFrame:CGRectMake(15, 40, _expandableTableView.frame.size.width-15, 1)];
separatorLineView.backgroundColor = ;
;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*************** Close the section, once the data is selected ***********************************/
];
withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([ 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=[initWithFrame:CGRectMake(0, 0, 280,40)];
sectionView.tag=section;
for (id subview in sectionView.subviews) {
if (]) {
;
} else if (]) {
;
}
}
viewLabel=[initWithFrame:CGRectMake(10, 0, _expandableTableView.frame.size.width-10, 40)];
viewLabel.font=;
viewLabel.text=];
;
if (collapsedSection ==section) {
viewLabel.textColor = ;
}else{
viewLabel.textColor = ;
}
/********** Add a custom Separator with Section view *******************/
UIView* separatorLineView = [ initWithFrame:CGRectMake(15, 40, _expandableTableView.frame.size.width-15, 1)];
separatorLineView.backgroundColor = ;
;
/********** Add UITapGestureRecognizer to SectionView **************/
UITapGestureRecognizer*headerTapped = [ initWithTarget:self action:@selector(sectionHeaderTapped:)];
;
returnsectionView;
}
#pragma mark - Table header gesture tapped
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = ;
collapsedSection = gestureRecognizer.view.tag;
if (indexPath.row == 0) {
collapsed= [ boolValue];
for (int i=0; i<; i++) {
if (indexPath.section==i) {
];
}
}
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
@end
</code></pre>
<p> <a href="/image/ytZQJ.png" rel="noreferrer noopener nofollow"><img src="/image/ytZQJ.png" alt="enter image description here"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>只需要替换以下行:</p>
<pre><code>if(collapsedSection ==section)
</code></pre>
<p>与</p>
<pre><code>if ([ boolValue])
</code></pre></p>
<p style="font-size: 20px;">关于ios - 当我在 ios 中展开和折叠 TableList 单元格时如何更改 UILabel 文本颜色,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34262697/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34262697/
</a>
</p>
页:
[1]