gpt4 book ai didi

ios - 当我们点击它时,如何更改添加到表格 View 单元格上的按钮背景颜色?

转载 作者:行者123 更新时间:2023-11-28 08:58:07 25 4
gpt4 key购买 nike

我在 tableview 单元格上添加了一个自定义按钮,一切正常。

但这里我的主要要求是,当我“第一次”单击添加到表格 View 单元格上的那个按钮时,按钮背景颜色需要更改(变为红色),当我第二次单击该按钮时,需要显示之前的颜色(变回白色)

我的代码:

#import "ViewController2.h"

@interface ViewController2 ()
{
NSArray * Mainarray;
UITableView * MaintableView;
UIImageView * accessoryView;
UIButton *button;
BOOL tapped;
}

@end

@implementation ViewController2

- (void)viewDidLoad
{
[super viewDidLoad];

tapped = NO;

Mainarray = [[NSArray alloc]initWithObjects:@"Ram",@"Rama",@"Rakesh",@"Ranjith", nil];

self.view.backgroundColor = [UIColor darkGrayColor];
MaintableView = [[UITableView alloc]init];
MaintableView.translatesAutoresizingMaskIntoConstraints = NO;
MaintableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
MaintableView.dataSource=self;
MaintableView.delegate=self;
MaintableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[MaintableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[self.view addSubview:MaintableView];

NSDictionary * views = NSDictionaryOfVariableBindings(MaintableView);

NSArray * horizentalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[MaintableView]-0-|" options:0 metrics:nil views:views];

NSArray * verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[MaintableView]-0-|"options:0 metrics:nil views:views];

[self.view addConstraints:horizentalConstraint];
[self.view addConstraints:verticalConstraint];
}

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

return 1;
}

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

return Mainarray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *Cell = [MaintableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (Cell == nil)
{
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Cell.textLabel.text = [Mainarray objectAtIndex:indexPath.row];

button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=indexPath.row;
[button addTarget:self
action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"" forState:UIControlStateNormal];
button.frame = CGRectMake(Cell.frame.size.width - 50, 10, 30, 30);

button.layer.cornerRadius = 60/2.0f;
button.layer.borderColor=[UIColor blackColor].CGColor;
button.backgroundColor = [UIColor whiteColor];
button.layer.borderWidth=2.0f;


//setting tag to button
button.tag=indexPath.row;

[Cell.contentView addSubview:button];

return Cell;
}

-(void)aMethod :(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:MaintableView];
NSIndexPath *indexPath1 = [MaintableView indexPathForRowAtPoint:buttonPosition];
NSInteger variable = indexPath1.row;
NSLog(@"ok it is %ld",(long)variable);

if(button.tag == variable){

button.backgroundColor= tapped ? [UIColor whiteColor]:[UIColor redColor];
tapped = YES;
}
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
@end

enter image description here

最佳答案

您可以创建一个数组,您可以在其中存储选定的按钮。示例:

var selectedButtons: [Bool] = [Bool](count: N, repeatedValue: false)

在您的 cellForRowAtIndexPath 中:

if selectedButtons[indexPath.row] {
yourButton.backgroundColor = UIColor.redColor()
} else {
yourButton.backgroundColor = UIColor.whiteColor()
}

然后在你的 tableView didSelectRowAtIndexPath 中你可以说:

selectedButtons[indexPath.row] = true
tableView.reloadData()

抱歉,我的代码是用 Swift 编写的,但将它转换为 Obj-C 并不难。

关于ios - 当我们点击它时,如何更改添加到表格 View 单元格上的按钮背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32550355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com