2016-05-28 9 views
0

Ich habe JSON-DatenPopulate UITableView von JSON-Daten

[{"id": 1,"Name": "Nodia","company": "WWW","position": "HR","email": "[email protected]","number": "33","photo": "image1.png"}, {"id": 2,"Name": "Nona ","company": "SSS","position": "Head of Department","email": "[email protected]","number": "5496","photo": "image2.png"}, {"id": 3,"Name": "David","company": "DDD","position": "Director","email": "[email protected]","number": "574","photo": "image3.png"},....]| 

ich eine Tabellenansicht mit Wert Namen erstellen.

Tableviewcontroller.h : 
    @interface CitiesViewController() 
     @property(nonatomic,strong)NSMutableArray *jsonArray 
     @property(nonatomic,strong)NSMutableArray *citiesArray; 
-(void)retrieveData; 

Tableviewcontroller.m : 
    @implementation CitiesViewController 
     @synthesize 

citiesArray,jsonArray; 

    #pragma mark - 

    #pragma mark Class Methods 

    -(void)retrieveData 
    { 

     NSURL *url = [NSURL URLWithString:getDataURL]; 
     NSData *data = [NSData dataWithContentsOfURL:url]; 

     jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

     citiesArray =[[NSMutableArray alloc]init]; 

     for (int i= 0; i<jsonArray.count; i++) 
     { 
      NSString *cID = [[jsonArray objectAtIndex:i] objectForKey:@"id"]; 
      NSString *cName = [[jsonArray objectAtIndex:i] objectForKey:@"Name"]; 
      NSString *cCompany = [[jsonArray objectAtIndex:i] objectForKey:@"company"]; 
      NSString *cPosition = [[jsonArray objectAtIndex:i] objectForKey:@"position"]; 
      NSString *cEmail = [[jsonArray objectAtIndex:i] objectForKey:@"email"]; 

      NSString *cNumber = [[jsonArray objectAtIndex:i] objectForKey:@"number"]; 
      NSString *cPhoto = [[jsonArray objectAtIndex:i] objectForKey:@"photo"]; 

      NSURL *urlOne = [NSURL URLWithString:cPhoto]; 
     //  NSLog(@"%@",urlOne); 
      NSData *photoData = [NSData dataWithContentsOfURL:urlOne]; 
      UIImage *imageOne = [[UIImage alloc] initWithData:photoData]; 

      [citiesArray addObject:[[City alloc]initWithCityName:cName andCityState:cCompany andCityCountry:cEmail andCityPopulation:cPosition andCityID:cID andCityNumber:cNumber andCityPhoto: (UIImage *) imageOne]]; 

     } 

     [self.tableView reloadData]; 

    } 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 

     [self retrieveData]; 

    } 

    #pragma mark - Table view data source 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
      return 1; 
     } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
      return citiesArray.count; 

    } 

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


     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

     // Configure the cell... 


     City *cityob; 
     cityob=[citiesArray objectAtIndex:indexPath.row]; 
     cell.textLabel.text=cityob.employeeName; 


     return cell; 

    } 

Alles funktioniert fine.Cell Daten zeigt. Aber das Problem ist, dass ich Titel Titel für Abschnitt und Abschnitt Index Titel in TableView nicht einstellen kann. Und ich möchte alphabetischen Header für Abschnitt Da die Daten dynamisch statische Lösung für das Problem ist nicht geeignet. like in this tutriole

Vielen Dank.Verzeih mein schlechtes Englisch!

+0

Obwohl dies die Swift-Version von Code ist, sollten Sie 'func tableView (tableView: UITableView, titleForHeaderInSection Abschnitt: Int) -> String verwenden? {} 'was eine Erweiterung von' UITableViewDelegate' ist. Sie können [this] (http://stackoverflow.com/a/15740781/3921490) als Referenz sehen. – amagain

+0

@amagain Detail wie möglich? –

+0

@amagain - (NSString *) tableview: (UITableView *) tableview titleForHeaderInSection: (NSInteger) section { }, aber ich weiß nicht, wie –

Antwort

0

Wie bereits erwähnt, müssen Sie TableView implementieren: titleForHeaderInSection :. Sie sind zur Zeit nur einen Abschnitt haben, und es ist unklar, was Sie zeigen wollen, aber eine beispielhafte Implementierung in etwa so aussehen würde:

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { 

NSString *sectionNameString = @"Current Jobs"; 
return sectionNameString;} 

Typing auf meinem iPhone, aber das ist etwa, wie es aussehen würde. Verwenden Sie eine Zeichenfolge Vergleichsfunktion außerhalb des Bereichs der Klasse wie so:

NSInteger sortCitiesByName(City *city1, City *city2, void *context) { 
NSString *name1 = [city1 name]; 
NSString *name2 = [city2 name]; 
return [name1 localizedCaseInsensitiveCompare: name2];} 

und dann sortieren Sie Ihre Array etwas wie folgt aus:

citiesArray = [citiesArray sortedArrayUsingFunction:sortCitiesByName context:nil]; 
+0

implementieren ich Array wollen Sortierung in alphabetischer Reihenfolge –

+0

Dafür Sie so etwas wie diese verwenden würde auf Ihre Städte Array: - (NSArray *) sortedArrayUsingSelector: (SEL) Komparator; oder - (NSArray *) sortedArrayUsingFunction: (NSInteger (*) (ObjectType, ObjectType, void * __nullable)) – caxix

+0

und das wird es mir geben? –

0
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    { 
     return [citiesArray objectAtIndex:section]; 
    } 

Bitte überprüfen Sie dies und geeignete Modifikationen machen .

+0

kann ein einzelnes Array für Alphabet erstellen? Ich möchte alphabetischer Header für Abschnitt –