2016-07-04 3 views
1

ich so versucht ... in cellForRowAtIndex()wie ausgewählte Zeilen mit Häkchen zeigen, während der zuvor ausgewählten Zeilen in UITableView in Ziel c Aktualisierung

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *[email protected]"SimpleTableViewIdentifier"; 

     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableViewIdentifier]; 

    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier]; 
    } 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.textLabel.text=[hardDependencyAlldataArray objectAtIndex:indexPath.row]; 

return cell; 
} 

und unter Codezeilen in DidSelectRowAtIndexPath hinzugefügt ..

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
cell.accessoryType = UITableViewCellAccessoryCheckmark; 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

aber, während die vorherigen ausgewählten Wert zu aktualisieren, überprüfen Marke nicht onpreviously ausgewählt zeigend rows..any ein in dieser issue..Thanks im Voraus helfen kann .. :)

+0

Was meinen Sie mit "Aktualisieren des zuvor ausgewählten Werts"? – Arun

+0

thq..for ur Antwort .. :) .. .. –

+0

zuvor habe ich Zeilen aus einer Tabellenansicht ausgewählt ... während der Auswahl erscheint ein Häkchen bei ausgewählten Zeilen ... jetzt möchte ich Agian Häkchen auf zuvor ausgewählten Zeilen zeigen, bevor Sie gehen zur Aktualisierung ... –

Antwort

2

Ich denke, das ist was du tun musst. In Ihrer cellForRowAtIndexPath Methode, statt cell.selectionStyle = UITableViewCellSelectionStyleNone; für alle Zellen zuweisen, weist es keine nur für jene Zellen, die

ändert die didSelectRowAtIndexPath Methode nicht gewählt werden, um das ausgewählte Zelle Detail zu einem Array speichern

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
cell.accessoryType = UITableViewCellAccessoryCheckmark; 
[self.selectedCellArray addObject:[hardDependencyAlldataArray objectAtIndex:indexPath.row]]; 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

Jetzt Ändern Sie die cellForRowAtIndexPath Methode wie folgt

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *[email protected]"SimpleTableViewIdentifier"; 

     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableViewIdentifier]; 

     if (cell == nil) 
     { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier]; 
     } 
     cell.textLabel.text=[hardDependencyAlldataArray objectAtIndex:indexPath.row]; 

     if (![self.selectedCellArray containsObject:[hardDependencyAlldataArray objectAtIndex:indexPath.row]]) 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 
     else 
     { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
     return cell; 
} 
+0

mein Anliegen ist, ich möchte lastSelectedRows verfolgen mit Häkchen für die Identifizierung von lastSelectedIndexed Werte, während die Tabelle Daten zu füllen .... –

+0

Ich habe meine Antwort mit Code aktualisiert. Bitte überprüfen Sie – Arun

+0

thq Herr Arun. :) ... aber ich habe noch nicht, zuvor ausgewählte Zeilen mit Häkchen im Bearbeitungsmodus ... –

1

Ich habe es Tamim. Überprüfen Sie die folgende Antwort. Ich probierte Beispielprojekt. Es funktioniert gut.

#import "ViewController.h" 

@interface ViewController() 
{ 
    NSMutableArray *arrProductSelection,*arrProductSelectDeSelectCheckMark; 
    NSArray *arrayFetchFromDefaults; 
    NSInteger lastSelectedIndex; 
} 

@end 

@implementation ViewController 

@synthesize tableViewCheckMarkSelectionUpdate; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    arrProductSelection = [[NSMutableArray alloc]initWithObjects:@"iPhone",@"iPad",@"iPod",@"iTV",@"iWatch",@"iMac",nil]; 
} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    [NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    arrayFetchFromDefaults = [userDefaults objectForKey:@"selectedcheckmark"]; 
    arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]initWithArray:arrayFetchFromDefaults]; 
    if(arrProductSelectDeSelectCheckMark.count == 0) 
    { 
     arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]init]; 
     for(int j=0;j<[arrProductSelection count];j++) 
     { 
     [arrProductSelectDeSelectCheckMark addObject:@"deselected"]; 
     } 
    } 
    [tableViewCheckMarkSelectionUpdate reloadData]; 

} 


#pragma mark - UITableViewDataSource Methods 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return arrProductSelection.count; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *strCell = @"cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell]; 
    if(cell==nil) 
    { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell]; 
    } 

    // lastSelectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedRow"]; - Getting Last selected index row 

    if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"]) 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    else 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    // if (indexPath.row == lastSelectedIndex) 
    //  cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    cell.textLabel.text = [arrProductSelection objectAtIndex:indexPath.row]; 
    return cell; 
} 


#pragma mark - UITableViewDelegate Methods 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // [[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:@"selectedRow"]; //This is for Last Selected IndexPath Row 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    @try 
    { 
    CGPoint touchPoint = [cell convertPoint:CGPointZero toView:tableViewCheckMarkSelectionUpdate]; 
    NSIndexPath *indexPath = [tableViewCheckMarkSelectionUpdate indexPathForRowAtPoint:touchPoint]; 
    NSLog(@"%@",arrProductSelectDeSelectCheckMark); 
    if([arrProductSelectDeSelectCheckMark count]==0) 
    { 
     for(int i=0; i<[arrProductSelection count]; i++) 
     { 
      [arrProductSelectDeSelectCheckMark addObject:@"deselected"]; 
     } 
    } 
    if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"selected"]; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"deselected"]; 
    } 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:arrProductSelectDeSelectCheckMark forKey:@"selectedcheckmark"]; 
    [defaults synchronize]; 
    } 
    @catch (NSException *exception) { 
    NSLog(@"The exception is-%@",exception); 
    } 
} 

- (IBAction)actionGoPrevious:(id)sender 
{ 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 
@end 
+0

zuvor gebuchte Code wurde für mich gearbeitet .. aber wie soll ich zuvor ausgewählte Werte abwählen ... –

+0

Ich habe alles getan Bruder . Überprüfen Sie meinen Code einmal.Sie bekommen alles.Ich implementierte Select, Abwahl und Speichern der zuletzt ausgewählten Zeile und Überprüfung der Bedingung, ob der zuletzt ausgewählte Zeilenindex gleich indexPath.row ist oder nicht.Alles in meinem Codierungsbruder getan. – user3182143

+0

ok..Thq ...:) ... –

Verwandte Themen