gpt4 book ai didi

iphone - 在uitableviewcell中选中取消选中按钮

转载 作者:行者123 更新时间:2023-12-03 19:34:45 25 4
gpt4 key购买 nike

我之前看过一些帖子,但还没有得到答案,这就是为什么我尝试以更有效的方式再次发帖。我如何使用 UITableView 中的选中/取消选中功能,如下图所示。

这是我想要的表格,当我单击任何单元格的按钮时,该按钮图像将会更改,而不是在所有单元格上。

table

最佳答案

对于检查-取消检查功能,仅 buttonClicked: 方法是不够的。您还将在 cellForRowAtIndexPath: 方法中设置按钮被选择未选择的条件,因为 cellForRowAtIndexPath:每次滚动 UITableView 时都会调用 > 方法,并且单元格将刷新。我看到您之前的问题,您要添加两个具有两个操作的按钮,这不是一个好方法,只需更改检查-取消检查按钮的图像即可。

这就是我为此所做的 -

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *tblView;
NSMutableArray *arrayCheckUnchek; // Will handle which button is selected or which is unselected
NSMutableArray *cellDataArray; // this is your data array
}

@end

现在在 ViewController.m 类中 -

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

arrayCheckUnchek = [[NSMutableArray alloc]init];
//Assign your cell data array
cellDataArray = [[NSMutableArray alloc]initWithObjects:@"cell-1",@"cell-2",@"cell-3",@"cell-4",@"cell-5", nil];

// setting all unchecks initially
for(int i=0; i<[cellDataArray count]; i++)
{
[arrayCheckUnchek addObject:@"Uncheck"];
}

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [cellDataArray count];
}

-(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];
}

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

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];

if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
[button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];

return cell;
}

-(void)buttonClicked:(id)sender
{
//Getting the indexPath of cell of clicked button

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];

// No need to use tag sender will keep the reference of clicked button
UIButton *button = (UIButton *)sender;

//Checking the condition button is checked or unchecked.
//accordingly replace the array object and change the button image
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
[button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
}
else
{
[button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
}
}

最后它看起来像 -

enter image description here

关于iphone - 在uitableviewcell中选中取消选中按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12814060/

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