在iOS中滚动时在TableView单元格中保留按钮状态
我知道这里有许多类似的问题,但我没有找到解决办法。我在在iOS中滚动时在TableView单元格中保留按钮状态,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我知道这里有许多类似的问题,但我没有找到解决办法。我在UITableViewCell中有一个复选框 当我选中并向下滚动和再次向上滚动时,其状态为重置,即未选中 我知道该单元格已在cellforrowatinexpath中被取消命名,但选中时如何保持其状态 以下是我尝试过的: @interface ContactCheckedViewController () { UIButton *checkBox; } - (UITableViewCell *)tableView:(UITableV
UITableViewCell
中有一个复选框
当我选中并向下滚动和再次向上滚动时,其状态为重置,即未选中
我知道该单元格已在cellforrowatinexpath
中被取消命名,但选中时如何保持其状态
以下是我尝试过的:
@interface ContactCheckedViewController ()
{
UIButton *checkBox;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"CheckBoxedCell";
// NSString *cellId = [NSString stringWithFormat:@"Section:%d Row:%d",indexPath.section,indexPath.row];
CheckBoxedCellClass *cell = (CheckBoxedCellClass *)[self.tableViewContact dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
NSArray *nib;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
nib = [[NSBundle mainBundle] loadNibNamed:@"CheckBoxedCellClass" owner:self options:nil];
}
else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
nib = [[NSBundle mainBundle] loadNibNamed:@"CheckBoxedCellClass_iPad" owner:self options:nil];
}
for (id object in nib)
{
if([object isKindOfClass:[CheckBoxedCellClass class]])
{
cell = (CheckBoxedCellClass *)object;
break;
}
}
cell = [nib objectAtIndex:0];
}
SaveCheckBoxedView *saveContact;
if(isFiltered == YES) //Handling UISearchbar
{
saveContact = [filterdArray objectAtIndex:indexPath.row];
cell.nameLabel.text = saveContact.nameString;
}
else
{
saveContact = [mutableArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
}
cell.companyLabel.text = saveContact.companyString;
cell.invIdLabel.text = [NSString stringWithFormat:@"%@", saveContact.invitId];
//handling check box
NSInteger rowNumber = 0;
for(NSInteger i = 0; i < indexPath.section ; i++)
{
rowNumber += [self tableView:self.tableViewContact numberOfRowsInSection:i];
}
rowNumber += indexPath.row;
//UIButton *checkBox;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(7, 8, 30, 30)];
}
else
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(15, 13, 30, 30)];
}
[checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:@selector(checkBoxClicked:event:) forControlEvents:UIControlEventTouchUpInside];
// handle check box view reset when scrolled
BOOL buttonPressed = [[self.boolArray objectAtIndex:rowNumber] boolValue];
[checkBox setSelected:buttonPressed];
if(buttonPressed)
{
[checkBox setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
}
else
{
[checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
}
checkBox.tag = rowNumber;
[cell.contentView addSubview:checkBox];
return cell;
}
-(void)checkBoxClicked:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableViewContact];
NSIndexPath *indexPath = [self.tableViewContact indexPathForRowAtPoint: currentTouchPosition];
NSLog(@"value of indexPath.section %d ,indexPath.row %d",indexPath.section,indexPath.row);
UIButton *tappedButton = (UIButton*)sender;
NSLog(@"Tag number = %d", [sender tag]);
if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkBox.png"]])
{
[sender setImage:[UIImage imageNamed: @"checkBoxMarked.png"] forState:UIControlStateNormal];
NSUserDefaults *buttonDefault = [NSUserDefaults standardUserDefaults];
[buttonDefault setBool:YES forKey:@"CHECKMARKEDKEY"];
//add values in boolArray to retain button state
[boolArray replaceObjectAtIndex:[sender tag] withObject:[NSNumber numberWithBool:YES]];
if(isFiltered == YES)
{
NSString *addId = [filteredArrayOfIds objectAtIndex:indexPath.row];
NSLog(@"filterd id = %@", addId); //get filtered array here
[arrayOfIds addObject:addId];
}
else
{
NSString *finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag];
NSLog(@"Tagged checked button id = %@", finalIntId);
[arrayOfIds addObject:finalIntId];
}
}
else
{
[sender setImage:[UIImage imageNamed:@"checkBox.png"]forState:UIControlStateNormal];
NSLog(@"UnChecked");
NSUserDefaults *buttonDefault = [NSUserDefaults standardUserDefaults];
[buttonDefault setBool:NO forKey:@"CHECKMARKEDKEY"];
[boolArray replaceObjectAtIndex:[sender tag] withObject:[NSNumber numberWithBool:NO]];
if(isFiltered == YES)
{
[arrayOfIds removeObjectIdenticalTo:[filteredArrayOfIds objectAtIndex:tappedButton.tag]];
}
else
{
[arrayOfIds removeObjectIdenticalTo:[mutableArrayOfIds objectAtIndex:tappedButton.tag]];
}
}
NSLog(@"bool array = %@", boolArray);
}
@接口ContactCheckedViewController()
{
UIButton*复选框;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellId=@“CheckBoxedCell”;
//NSString*cellId=[NSString stringWithFormat:@“节:%d行:%d”,indexPath.Section,indexPath.Row];
CheckBoxedCellClass*单元格=(CheckBoxedCellClass*)[self.tableViewContact dequeueReusableCellWithIdentifier:cellId];
如果(!单元格)
{
NSArray*nib;
如果(用户界面习惯用法()==UIUserInterfaceIdiomPhone)
{
nib=[[NSBundle mainBundle]loadNibNamed:@“CheckBoxedCellClass”所有者:自选项:nil];
}
else if(用户界面习惯用法()==UIUserInterfaceIdiomPad)
{
nib=[[NSBundle mainBundle]loadNibNamed:@“CheckBoxedCellClass_iPad”所有者:自我选项:nil];
}
for(nib中的id对象)
{
if([object isKindOfClass:[CheckBoxedCellClass]])
{
cell=(CheckBoxedCellClass*)对象;
打破
}
}
单元格=[nib objectAtIndex:0];
}
SaveCheckBoxedView*saveContact;
if(isFiltered==YES)//处理UISearchbar
{
saveContact=[FilterArray objectAtIndex:indexPath.row];
cell.namelab.text=saveContact.nameString;
}
其他的
{
saveContact=[mutableArray objectAtIndex:indexath.row];
cell.namelab.text=[[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];
}
cell.companyLabel.text=saveContact.companyString;
cell.invIdLabel.text=[NSString stringWithFormat:@“%@”,saveContact.invitId];
//处理复选框
NSInteger rowNumber=0;
对于(NSInteger i=0;i
请让我知道我应该在代码中更改什么。任何关于示例代码的帮助都将不胜感激
谢谢。为此,您必须维护NSMutableArray和boolean。让我们假设它是
NSMutableArray*selectedRows;
布尔当选代码>
UIButton *btn = (UIButton *)[cell viewWithTag://your button tag]; //your button instance
if(isRowSelected){
// set button checked
isRowSelected = NO;
[selectedRows addObject:indexPath];
}
else{
// set button checked
isRowSelected = YES;
[selectedRows removeObject:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];`
// Mentain state for UIButton.
if([selectedRows containsObject:indexPath])
// your button state : checked
else
// your button state : unchecked
//init this in viewDidLoad
NSMutableDictionary *boolDict = [[NSMutableDictionary alloc] init];
BOOL buttonPressed = [[boolDict objectForKey:[NSString stringWithFormat:@"%d", rowNumber]] boolValue];
[boolDict setObject:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%d", [sender tag]]];
[boolDict setObject:[NSNumber numberWithBool:NO] forKey:[NSString stringWithFormat:@"%d", [sender tag]]];
static NSString *cellId = @"CheckBoxedCell";