ios - 当我们点击 UIbutton 时,如何更改 TableList UILabel backGround Color?

标签 ios objective-c uitableview

您好,我是 iOS 的新手,我在我的 Viewcontroller 上创建了一个表格列表,在我的表格列表行中,我以编程方式添加了一个 UIlabel,没关系。

而且我在我的“self.view”上以编程方式添加了一个 UIbutton。

这里我的主要要求是当我点击那个按钮时,我想更改添加到我的表格行的 UIlabel 背景颜色。

为此,我尝试了下面的代码,但 UIlabel backgroudcolor 没有改变。

请帮帮我。

我的代码:-

#import "ViewController.h"

@interface ViewController (){

UILabel *label;

}

@end

@implementation ViewController {

    UITableView *tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self tablelistCreation];
}

//UItableView createtion

-(void)tablelistCreation{

    tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];

    tableView.delegate = self;
    tableView.dataSource = self;

    [self.view addSubview:tableView];
}

//Delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];


    label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
    label.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:label];

    return cell;

}

//UIbutton createtion

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];
 [button setTitle:@"Show View" forState:UIControlStateNormal];
 button .backgroundColor = [UIColor orangecolor];
 button.frame = CGRectMake(100, 440, 30, 30);
 [self.view addSubview:button];

//button action

-(void)Method:(id)sender{

  label.backgroundColor = [UIColor redcolor];

}

最佳答案

如果您想更改所有单元格或部分单元格的标签颜色,您的问题不清楚。

无论如何,必须在 cellForRowAtIndexPath 中更改颜色,这是更新/创建单元格的地方。

您可以创建一个变量来通知您是否想要更改颜色,它会在 cellForRowAtIndexPath 上进行检查。

@implementation ViewController {
    UITableView *tableView;
    bool changeColor;
}

-(void)Method:(id)sender{
  changeColor != changeColor;
[tableview reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
        UILabel label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
        [cell.contentView addSubview:label];

    }


   if (changeColor){ 
      label.backgroundColor = [UIColor redcolor];
   }else{
      label.backgroundColor = [UIColor clearColor];
   }


return cell;
}

如果你想更改特定的标签,你可以创建一个 nsarray 来控制要更改的单元格

关于ios - 当我们点击 UIbutton 时,如何更改 TableList UILabel backGround Color?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192760/

相关文章:

iOS:将 UTC NSDate 转换为本地时区

objective-c - 是什么导致表格单元格以随机/未知模式显示?

objective-c - KVO - 如何检查对象是否是观察者?

ios - 加载保存到文档目录的图像只能工作一次,然后再也不会加载

ios - 如何填充作为 UIViewController subview 的 UITableView?

ios - 我怎样才能触发我的 iphone 应用程序从我的 mac 应用程序启动

ios - 使用 json 时从 TableView 中删除

objective-c - 这是在 iPhone 上生成随机字符串的好方法吗?

ios - 将 NSArray 保存到 NSUserDefaults 不起作用

ios - 如何从 Interface Builder 中设计的 UITableView 获取 UITableViewCell 引用