2012-11-15 72 views
0

我正在使用tableview和顯示數據。在表格中查看所有checkMark項目發送的郵件。但是當滾動表格時,複選標記未被選中。滾動表格時,我不想取消選中。請幫幫我。我正在開發下面的代碼滾動表格視圖時未選中複選標記

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier "; 
    CaptureCell *cell = (CaptureCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CaptureCell" 
                owner:self options:nil]; 
     for (id oneObject in nib) if ([oneObject isKindOfClass:[CaptureCell class]]) 
      cell = (CaptureCell *)oneObject; 
    } 

    if ([[[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"]isEqualToString:[dict objectForKey:[[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"]]]) 
    { 
     cell.confirmMail.image = [UIImage imageNamed:@"mailConfirm.png"]; 
    } 
    NSString *input; 
    input = [[dataArray objectAtIndex:indexPath.row]objectForKey:@"amount"]; 
    [input stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
    //NSLog(@"after removing white spaces %@",input); 
    double price; 
    NSString *numberAsString = nil; 
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; 
    NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 
    [numberFormatter setLocale:gbLocale]; 
    [gbLocale release]; 
    [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle]; 
    if ([[input stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]length]!=0) 
    { 
     NSLog(@"the input is***** %@",input);   
     price=[[input stringByReplacingOccurrencesOfString:@"$" withString:@""] doubleValue]; 
     NSLog(@"the price is %f",price); 
     numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:price]]; 
     NSLog(@"the numberAsString...... %@",numberAsString); 
    } 
    [numberFormatter release]; 


    cell.eventNameLbl.text = [[dataArray objectAtIndex:indexPath.row]objectForKey:@"expenseType"]; 
    cell.expenTypeLbl.text = [[dataArray objectAtIndex:indexPath.row]objectForKey:@"empName"]; 
    cell.dataLbl.text = [[dataArray objectAtIndex:indexPath.row]objectForKey:@"Date"]; 
    cell.amountLbl.text = numberAsString; 

    cell.billImage.image = [UIImage imageWithData:[[dataArray objectAtIndex:indexPath.row]objectForKey:@"image"]]; 

    //[cell.textLabel setText:[NSString stringWithFormat:@"Row %d", indexPath.row]]; 
    cell.selectionStyle=UITableViewCellSelectionStyleNone; 
    NSIndexPath* selection = [tableView indexPathForSelectedRow]; 
    if (selection && selection.row == indexPath.row) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 



    // if([[[captureArray objectAtIndex:indexPath.row]objectForKey:@"eventName"]isEqualToString:[[selectedData objectAtIndex:indexPath.row]objectForKey:@"eventName"]]) 
    // NSLog(@"Equal"); 

    //[cell setEditing:YES animated:YES]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *selected = [dataArray objectAtIndex:indexPath.row]; 


    if([tableView isEditing] == YES) 
    { 
     AppDelegate *sharedDelegate = [[UIApplication sharedApplication]delegate]; 
     sharedDelegate.isEditCapture = YES; 
     sharedDelegate.findDate = YES; 
     NSString *imgDataStr = [[NSString alloc]initWithData:[[dataArray objectAtIndex:indexPath.row]objectForKey:@"image"] encoding:NSUTF8StringEncoding]; 
     NSLog(@"imageStr%@",imgDataStr); 
     ExpensesCaptureViewController_iPhone *nextController = [[ExpensesCaptureViewController_iPhone alloc] initWithNibName:@"ExpensesCaptureViewController_iPhone" bundle:nil]; 

     nextController.captureId = [[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"]; 
     [self.navigationController pushViewController:nextController animated:YES]; 
     [imgDataStr release]; 
     [nextController release]; 
     if([selectedData count]>0) 
     { 
      [selectedData removeObject:selected]; 
     } 

    } 


    else { 
     if ([[[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"]isEqualToString:[dict objectForKey:[[dataArray objectAtIndex:indexPath.row] objectForKey:@"CaptureId"]]]) 
     { 
      NSLog(@"sent items"); 
     } 

     else { 
      if (cell.accessoryType == UITableViewCellAccessoryNone) 
      { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 

       [selectedData addObject:selected]; 

      } 

      else 
      { 

       cell.accessoryType = UITableViewCellAccessoryNone;    

       [selectedData removeObject:selected]; 

      } 

     } 


    } 

    NSLog(@"DataObjects %d %@",[selectedData count] ,[[dataArray objectAtIndex:indexPath.row] objectForKey:@"eventName"]); 

    //[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

} 
+0

我不是很追隨一行應該檢查,當它不應該;也許你可以澄清。然而,作爲一個普遍的原則,當我滾動'_'時,出現單元格發生形式_'[意外事件X]的問題時,解決方案通常與單元重用有關,並且未明確設置單元格上的某些屬性。 – FluffulousChimp

回答

2

你滾動您的tableView當- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法被調用和設置

NSIndexPath* selection = [tableView indexPathForSelectedRow]; 
    if (selection && selection.row == indexPath.row) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

這意味着當你將滾動indexPath將成爲當前行只有這樣的問題當前行的複選標記仍將被檢查,但所有其他行都將進入else條件,並且選中標記將被刪除。

要解決它,你應該做一個做到以下幾點:

定義以下屬性:

@property (nonatomic)BOOL isViewIsLoadedForTheFirstTime; 

-(void)viewDidLoad 
{ 
    self.isViewIsLoadedForTheFirstTime = YES; 
} 

然後替換(cellForRow),這些線路的方法

if (selection && selection.row == indexPath.row) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

到以下幾行:

if (self.isViewIsLoadedForTheFirstTime) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     self.isViewIsLoadedForTheFirstTime = NO; 
    } 

這會讓你設置複選標記,只有當視圖第一次加載的時候你會滾動它不會改變。

我希望這會幫助你

相關問題