2014-01-28 40 views
0

我知道這裏有很多類似的問題,但我沒有找到解決方案。我有一個複選框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); 

} 

請讓我知道我應該在我的代碼更改。任何幫助示例代碼將不勝感激。

謝謝。

+0

我已經在我的代碼中使用boolArray。請找到並提出一些好的解決方案。 – user3244372

+0

你正在初始化'boolArray'。 – Akhilrajtr

+0

我加了@property(nonatomic,strong)NSMutableArray * boolArray;在.h文件中,將其合成到.m文件中 – user3244372

回答

0
一個布爾值 'YES' 和 'NO'

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];` 

試試這個,

insted boolArraycellForRowAtIndexPath :使用

BOOL buttonPressed = [[boolDict objectForKey:[NSString stringWithFormat:@"%d", rowNumber]] boolValue]; 

使用NSMutableDictionary *boolDict

//init this in viewDidLoad 
NSMutableDictionary *boolDict = [[NSMutableDictionary alloc] init]; 

checkBoxClicked :使用

[boolDict setObject:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%d", [sender tag]]]; 

[boolDict setObject:[NSNumber numberWithBool:NO] forKey:[NSString stringWithFormat:@"%d", [sender tag]]]; 
1

爲此,您必須維護NSMutableArray和布爾值。讓我們假設其

NSMutableArray *selectedRows; BOOL isRowSelected;

然後在 'didSelectRowAtIndexPath方法' 你必須做出最後的 '的cellForRowAtIndexPath'

// Mentain state for UIButton. 
if([selectedRows containsObject:indexPath]) 
    // your button state : checked 
else 
    // your button state : unchecked 
0
static NSString *cellId = @"CheckBoxedCell"; 

變化CheckBoxedCell到您的類名。

我希望它的工作很好

相關問題