2016-04-28 24 views
0

Ich habe eine Tabellenansicht, die dynamische Daten von einem Dienst erhält. Ich möchte mehrere Zeilen Limit ist 3 .. Ich habe alle möglichen Möglichkeiten versucht, aber kann es nicht bekommen .. folgt ist mein Code. Bitte helfen Sie mir, dieses Problem zu lösen. Danke im vorausEingeschränkte Auswahl in UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [self.countriesArr count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSUInteger row = [indexPath row]; 

    static NSString *MyIdentifier = @"tableCell"; 

    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 


    if (cell == nil) { 

     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (CustomCell *) currentObject; 

      } 
     } 

    } 

    NSDictionary *thisRow = [self.countriesArr objectAtIndex:row]; 


    if(_WSConstCountryID !=nil && ![_WSConstCountryID isEqual:@"0"] && ![_WSConstCountryID isEqual:@""] && _WSConstCountrySelectedIndex ==row ) 
    { 
     cell .accessoryType=UITableViewCellAccessoryCheckmark; 
    } 
    else { 
     cell .accessoryType=UITableViewCellAccessoryNone; 
    } 

    cell.lblTitle.text = [thisRow objectForKey:_WSColumnCountryName]; 
    NSString *str=[thisRow objectForKey:_WSColumnCountryID]; 
    NSString *stra=_WSConstCountryID; 
    if ([str isEqualToString:stra]) { 
     cell .accessoryType=UITableViewCellAccessoryCheckmark; 
     cell.highlighted=YES; 
    }else 
    { 
     cell .accessoryType=UITableViewCellAccessoryNone; 

    } 


    return cell; 


} 
#pragma mark - 
#pragma mark Table view delegate 

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


    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    NSUInteger row = [indexPath row]; 
    NSDictionary *thisRow=[self.countriesArr objectAtIndex:row]; 
    NSLog(@"%@",[thisRow description]); 


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 



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

    if(_WSConstCountryID!=nil && ![_WSConstCountryID isEqual:@"0"] && ![_WSConstCountryID isEqual:@""] &&_WSConstCountrySelectedIndex!=row) 
    { 

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstCountrySelectedIndex inSection:0]]; 


     if (cell != nil) 
     { 
      cell .accessoryType=UITableViewCellAccessoryNone; 
     } 

    } 
    if(cell.accessoryType == UITableViewCellAccessoryCheckmark) 
    { 
     _WSConstCountryID=[thisRow objectForKey:_WSColumnCountryID]; 
     _WSConstCountryName=[thisRow objectForKey:_WSColumnCountryName]; 
     _WSConstCountrySelectedIndex=row ; 
    } 
    else 
    { 
     [email protected]"0"; 
     [email protected]"Select"; 
     _WSConstCountrySelectedIndex=-1 ; 
    } 

    [self.navigationController popViewControllerAnimated:YES]; 

} 

Antwort

0

Es gibt zwei Dinge, die Sie dafür folgen müssen. 1. Da Sie maximal nur drei Zeilen gleichzeitig auswählen möchten, behalten Sie ein Array zum Verfolgen dieser Zeilennummern bei und überprüfen Sie, ob dieses Array die Zeilennummer in CellForRowAtIndexPath hat. Aber _WSConstCountrySelectedIndex hat nur die zuletzt ausgewählte Zeilennummer.

2.In didSelectRowAtIndex aktualisieren Sie Ihr Array mit der aktuell ausgewählten Zeilennummer. Und stellen Sie sicher, dass das Array die Anzahl von 3 nicht überschreitet. Ähnlich wie in didDeSelectRowAtIndex, wenn die ausgewählte Zeile abgewählt ist, entfernen Sie diese Zeilennummer im Array.

Es muss Ihnen einen Start geben.

0

In willSelectRowAtIndexPath überprüfen Sie die Anzahl der Anzahl der Zeilen ausgewählt. Wenn es kleiner als 3 ist, dann gebe den Index zurück oder gebe nil zurück.

- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ([tableView indexPathsForSelectedRows].count > 3) { 
     return nil; 
    } 
    return indexPath; 
} 

Hoffe, das wird Ihnen helfen.