Iphone 选中uitableview中的取消选中按钮';s细胞
我以前看过一些帖子,但还没有得到答案,这就是为什么我试图以更有效的方式再次发布。如何使用Iphone 选中uitableview中的取消选中按钮';s细胞,iphone,uitableview,Iphone,Uitableview,我以前看过一些帖子,但还没有得到答案,这就是为什么我试图以更有效的方式再次发布。如何使用UITableView中的check uncheck功能,如下图所示 这是我想要的表格,当我点击任何单元格的按钮时,按钮图像将改变,而不是在所有单元格上 标签对于在IB中设计的自定义uitableviewcell非常有用。如果以编程方式创建单元格,则不需要标签。您使用它们只是为了找到正确的uiview来设置属性。是,在UITableViewCell中声明类并使用标记识别它们时,设置标记很简单。我已经发布了一些
UITableView
中的check uncheck功能,如下图所示
这是我想要的表格,当我点击任何单元格的按钮时,按钮图像将改变,而不是在所有单元格上
标签对于在IB中设计的自定义uitableviewcell非常有用。如果以编程方式创建单元格,则不需要标签。您使用它们只是为了找到正确的uiview来设置属性。是,在UITableViewCell中声明类并使用标记识别它们时,设置标记很简单。我已经发布了一些示例代码供您参考。我不确定这会解决你的问题。您询问了如何设置标记并使用标记标识类。所以它会让你对你的问题有一些基本的了解
-(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.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
label1 = [[UIView alloc] initWithFrame:CGRectMake(2, 20, 15, 15)];
label1.tag = 100;
label1.backgroundColor = [UIColor whiteColor];
label1.layer.cornerRadius = 5;
[cell.contentView addSubview: label1];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(25, 2, 165, 23)];
label2.tag = 101;
label2.font = [UIFont boldSystemFontOfSize:15];
label2.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: label2];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(190, 2, 90, 23)];
label3.tag = 102;
label3.font = [UIFont systemFontOfSize:14];
label3.textColor = [UIColor colorWithRed:0.14117670588235 green:0.435294117647059 blue:0.847058823529412 alpha:1];
label3.backgroundColor = [UIColor clearColor];
label3.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview: label3];
}
label1 = (UILabel *) [cell.contentView viewWithTag:100];
NSString *string1 = [array1 objectAtIndex:indexPath.row];
label2 = (UILabel *) [cell.contentView viewWithTag:101];
NSString * string2 = [array2 objectAtIndex:indexPath.row];
label3 = (UILabel *) [cell.contentView viewWithTag:102];
NSString * string3 = [array3 objectAtIndex:indexPath.row];
return cell;
}
请让我知道这是否有用。否则我们会走另一条路
快乐编码。谢谢。我同意@VakulSaini的观点,因为你可以这样做来处理触摸或刷卡或其他任何事情,这是一个例子:
-(void)handleSwipLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint Location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:Location];
cell = [tableView cellForRowAtIndexPath:indexPath];
}
使用
UITableView
的cellforrowatinexpath
方法,并在此方法中通过使用[cell.contentView addSubview:YourComponentName]在UITableView单元格中添加任何组件,如(UITextField
,UILabel
…)编码>并通过indexPath.row
将每个组件赋予其标记
比如说
YourComponentName.tag = indexPath.row;
对于只选中取消选中功能的按钮选中:
方法是不够的。您还将把条件放在cellforrowatinexpath:
方法中,该方法选择了按钮或未选择按钮,因为cellforrowatinexpath:
方法将在每次滚动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
最后它看起来会像——
你想做什么请问这个问题,因为我认为可以有更好的方法来解决你的问题,而不是标记。请看这个。在UITableViewCell
上设置标记是不好的方法。每个单元格都可以从它的行ID中检测,你可以将标记设置为用于创建自定义单元格的UI控件,例如UIButton。在UIButton的选择器中,您可以根据需要获取并使用其标记。@VakulSaini即使我现在这样认为,标记也不是解决方案。让我检查一下,我稍后会通知您。这不是我需要的,请查看我修改后的问题。我是以编程方式进行的,我需要做的正是。不要为此使用标记。只需在tableView:didselectrowatinexpath:in按钮操作中执行所有必需的操作,只需添加[[self tableView]reloadData];基兰:实际上,当我改变按钮的图像和阵列时,不需要添加这一行。如果不在此处更改图像,则需要重新加载表视图。
- (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"];
}
}