UITableview iPhone中的多个复选框
我是iOS新手 我正在tableview中动态创建按钮 我在按钮上设置了一个图像,用于选中和取消选中 我要做的是,当我点击(或检查)按钮时,indexpath行上的数据将添加到我的数组中。 如果我取消选择它,数据将从数组中删除。请帮帮我UITableview iPhone中的多个复选框,iphone,uitableview,Iphone,Uitableview,我是iOS新手 我正在tableview中动态创建按钮 我在按钮上设置了一个图像,用于选中和取消选中 我要做的是,当我点击(或检查)按钮时,indexpath行上的数据将添加到我的数组中。 如果我取消选择它,数据将从数组中删除。请帮帮我 -(void)btnForCheckBoxClicked:(id)sender { UIButton *tappedButton = (UIButton*)sender; indexForCheckBox= [sender tag]; if([tappe
-(void)btnForCheckBoxClicked:(id)sender
{
UIButton *tappedButton = (UIButton*)sender;
indexForCheckBox= [sender tag];
if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkbox_unchecked.png"]])
{
[sender setImage:[UIImage imageNamed: @"checkbox_checked.png"] forState:UIControlStateNormal];
strinForCheckBox= [ApplicationDelegate.ArrayForSearchResults objectAtIndex:indexForCheckBox];
[arrForCheckBox addObject:strinForCheckBox];
NSLog(@"Sender Tag When Add %d", indexForCheckBox);
NSLog(@"Array Count Check Box %d",[arrForCheckBox count]);
}
else
{
[sender setImage:[UIImage imageNamed:@"checkbox_unchecked.png"]forState:UIControlStateNormal];
[arrForCheckBox removeObjectAtIndex:indexForCheckBox];
NSLog(@"Sender Tag After Remove %d", indexForCheckBox);
NSLog(@"Array Count Uncheck Box %d",[arrForCheckBox count]);
}
}
您可以在按钮单击方法中添加此项 第一个设置布尔值,该值在一次单击时设置为“是”,在第二次单击时设置为“否”,在第一次单击时,使用array addobject属性和其他click removeobject属性在数组中添加索引值
-(void)list
{
if(isCheck==YES)
{
//add array object here
isCheck=NO;
}
else if(isCheck==NO)
{
//remove array object here
isCheck=YES;
}
}
您可以在按钮单击方法中添加此项 第一个设置布尔值,该值在一次单击时设置为“是”,在第二次单击时设置为“否”,在第一次单击时,使用array addobject属性和其他click removeobject属性在数组中添加索引值
-(void)list
{
if(isCheck==YES)
{
//add array object here
isCheck=NO;
}
else if(isCheck==NO)
{
//remove array object here
isCheck=YES;
}
}
我想这个问题早就有答案了!请先查看,并告诉我们这是否是您正在寻找的
我想这个问题早就有答案了!请先查看,并告诉我们这是否是您正在寻找的
我找到了另一种方法。从和中提取的代码,我刚刚用键/值对修改为有用的代码,如下面的代码-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if ([[selectedRowsArray objectAtIndex:indexPath.row] containsObject:@"YES"])
cell.imageView.image = [UIImage imageNamed:@"checked.png"];
else
cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
UITapGestureRecogniser *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleChecking:)
[cell.imageView addGestureRecognizer:tap];
[tap release];
cell.textLabel.text = [contentArray objectAtIndex];
return cell;
}
- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer {
CGPoint tapLocation = [tapRecognizer locationInView:self.tableView];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
if ([[selectedRowsArray objectAtIndex:tappedIndexPath.row] containsObject:@"YES"])
[selectedRowsArray replaceObjectAtIndex:tappedIndexPath.row withObject:[NSSet setWithObject:@"NO"]];
else
[selectedRowsArray replaceObjectAtIndex:tappedIndexPath.row withObject:[NSSet setWithObject:@"YES"]];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}
添加selectedRowsArray
的对象,如下所示,以及self.tableView
的行数。因此,您需要添加bool值,如下所示
for ( int i = 0; i < tableViewRowCount; i++ ) { // tableViewRowCount would be your tableView's row count
....
....
....
[selectedRowsArray addObject:[NSSet setWithObject:@"NO"]];
....
....
}
for(int i=0;i
希望这有帮助!干杯
更新
通过使用更新的代码,您不需要保留
NSMutableDictionary
的键/值对来过度使用它NSSet
为您提供了更好的方法。我找到了另一种方法。从和中提取的代码,我刚刚用键/值对修改为有用的代码,如下面的代码-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if ([[selectedRowsArray objectAtIndex:indexPath.row] containsObject:@"YES"])
cell.imageView.image = [UIImage imageNamed:@"checked.png"];
else
cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
UITapGestureRecogniser *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleChecking:)
[cell.imageView addGestureRecognizer:tap];
[tap release];
cell.textLabel.text = [contentArray objectAtIndex];
return cell;
}
- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer {
CGPoint tapLocation = [tapRecognizer locationInView:self.tableView];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
if ([[selectedRowsArray objectAtIndex:tappedIndexPath.row] containsObject:@"YES"])
[selectedRowsArray replaceObjectAtIndex:tappedIndexPath.row withObject:[NSSet setWithObject:@"NO"]];
else
[selectedRowsArray replaceObjectAtIndex:tappedIndexPath.row withObject:[NSSet setWithObject:@"YES"]];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}
添加selectedRowsArray
的对象,如下所示,以及self.tableView
的行数。因此,您需要添加bool值,如下所示
for ( int i = 0; i < tableViewRowCount; i++ ) { // tableViewRowCount would be your tableView's row count
....
....
....
[selectedRowsArray addObject:[NSSet setWithObject:@"NO"]];
....
....
}
for(int i=0;i
希望这有帮助!干杯
更新
通过使用更新的代码,您不需要保留
NSMutableDictionary
的键/值对来过度使用它NSSet
为您提供了更好的解决方法。我真的不明白您需要什么帮助!如果可能,在您的问题中提供一些代码@Haris我理解了他的问题。多亏了@Haris,我发布了按钮点击事件的代码,在indexpath方法行的tablev单元格中,我正在动态创建按钮。我真的不明白您需要什么帮助!如果可能,在您的问题中提供一些代码@Haris我理解他的问题。感谢@Haris,我发布了按钮点击事件的代码,在indexpath方法行的tablev单元格中,我正在动态创建类似于此类型的按钮,但我的问题是在选择后将值存储在数组中,取消选择后将其从数组中删除希望您理解类似于此类型的按钮,但我的问题是关于在数组中存储值在选择和取消选择后将其从数组中删除希望您理解仅传递在didselectrow tableview方法中创建的对象并在此处传递该对象。仅传递在didselectrow tableview方法中创建的对象并在此处传递该对象。