我知道這裏有很多類似的問題,但我沒有找到解決方案。我有一個複選框UITableViewCell
。在iOS中滾動時保留TableView單元格中的按鈕狀態
當我檢查和向下滾動和向上滾動再次,它的狀態是復位即unchecked
。
我知道單元格出現在cellForRowAtIndexPath
中,但是如何在檢查時保留其狀態?
這裏是我的嘗試:
@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);
}
請讓我知道我應該在我的代碼更改。任何幫助示例代碼將不勝感激。
謝謝。
我已經在我的代碼中使用boolArray。請找到並提出一些好的解決方案。 – user3244372
你正在初始化'boolArray'。 – Akhilrajtr
我加了@property(nonatomic,strong)NSMutableArray * boolArray;在.h文件中,將其合成到.m文件中 – user3244372