2016-07-21 38 views
1
NSArray *sectionArray; 
int sectionCount=0; 
NSDictionary *orderedData; 
NSString *checkInStr, *checkOutStr; 
NSString *govtTaxes, *enhancementTotal, *grandTotal; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self setupTable]; 

    [self.bookingsTableView reloadData]; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

-(void)viewDidDisappear:(BOOL)animated { 
    if(doesSendNotification){ 
     NSLog(@"summary view disappeared"); 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"SummaryViewDismissedNotification" object:self]; 
    } 
} 

-(void)viewWillAppear:(BOOL)animated { 
    [self.bookingsTableView reloadData]; 
} 

-(void)setupTable { 

    self.bookingsTableView.rowHeight = UITableViewAutomaticDimension; 
    self.bookingsTableView.estimatedRowHeight = 50.0; 
    sectionArray = [[SummaryModel sharedInstance] getTableSections:self.s_sendEnhancementServerDict]; 
    orderedData = [[SummaryModel sharedInstance] getOrderedData:self.s_sendEnhancementServerDict]; 

    [self.bookingsTableView reloadData]; 
} 





#pragma mark- UITableview delegate and datasource methods 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    if(section==0){ 

     return 3; 

    } else if (section>0 && section<(sectionCount-1)){ 
     int rows=(int)[[orderedData objectForKey:(NSString*)[sectionArray objectAtIndex:section]] count]; 
     return rows; 

    } else { 

     return 4; 

    } 
} 

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

    return (NSString*)[sectionArray objectAtIndex:section]; 
} 

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


    NSString *cellIdentifier; 
    UITableViewCell *cell; 

// UITableView *table = (UITableView*)[self.view viewWithTag:11]; 
    if (indexPath.section==0 && indexPath.row>=0 && indexPath.row<=2) { 

     cellIdentifier [email protected]"SplitCell"; 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     } 
     UILabel *l1 = (UILabel*)[cell viewWithTag:1]; 
     UILabel *l2 = (UILabel*)[cell viewWithTag:2]; 

     if(indexPath.row==0){ 
      l1.attributedText = [self getStyledString1:@"Hotel Name"]; 
      l2.attributedText = [self getStyledString:self.s_propertyName]; 
     } else if(indexPath.row==1){ 
      l1.attributedText = [self getStyledString1:@"Arrival Date:"]; 
      l2.attributedText = [self getStyledString:checkInStr]; 
     } else if(indexPath.row==2){ 
      l1.attributedText = [self getStyledString1:@"Departure Date:"]; 
      l2.attributedText = [self getStyledString:checkOutStr]; 
     } 


    } else if (indexPath.section>0 && indexPath.section<(sectionCount-1)) { 

     //  for(int i=0;i<5;i++){ 

     [email protected]"VerticalLabelCell"; 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     } 
     UILabel *l3 = (UILabel*)[cell viewWithTag:3]; 
     UILabel *l4 = (UILabel*)[cell viewWithTag:4]; 
     l3.layer.backgroundColor = GOLDEN_COLOR.CGColor; 

     NSArray *roomTypeArray = [orderedData objectForKey:(NSString*)[sectionArray objectAtIndex:indexPath.section]]; 
     NSDictionary *roomD = [roomTypeArray objectAtIndex:indexPath.row]; 

     NSString *header = [roomD objectForKey:@"room_type_name"]; 
     NSAttributedString *sH = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",header] attributes:@{NSFontAttributeName:ARIAL_FONT_BOLD}]; 
     l3.attributedText = sH; 

     int roomCount = [(NSNumber*)[roomD objectForKey:@"room_units"] intValue]; 
     NSMutableAttributedString *labelText = [[NSMutableAttributedString alloc] init]; 
     for(int i=0;i<roomCount;i++){ 

      NSString *roomNo = [NSString stringWithFormat:@"\n Room # %d\n",i+1]; 
      NSAttributedString *s = [[NSAttributedString alloc] initWithString:roomNo attributes:@{NSFontAttributeName:ARIAL_FONT_BOLD, NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)}]; 
      [labelText appendAttributedString:s]; 

      NSString *adults = [NSString stringWithFormat:@" Adults: %@ \t\t Max. Adults: %@ \n",[roomD objectForKey:@"max_adults"],[roomD objectForKey:@"max_adults"]]; 

      NSAttributedString *s1 = [[NSAttributedString alloc] initWithString:adults attributes:@{NSFontAttributeName:ARIAL_FONT_BOLD}]; 
      [labelText appendAttributedString:s1]; 

      NSArray *enhanc = [(NSArray*)[roomD objectForKey:@"room_features"] objectAtIndex:i]; 

      for(int i=0;i<[enhanc count];i++){ 
       [labelText appendAttributedString:[self getStyledString2:[NSString stringWithFormat:@" %@\n", [enhanc objectAtIndex:i]]]]; 
      } 

      l4.attributedText = labelText; 
     } 


    } else if(indexPath.section==(sectionCount-1)){ 

     cellIdentifier [email protected]"SplitCell"; 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     } 
     UILabel *l1 = (UILabel*)[cell viewWithTag:1]; 
     UILabel *l2 = (UILabel*)[cell viewWithTag:2]; 

     if(indexPath.row==0){ 

      l1.attributedText = [self getStyledString1:@"Room Charges:"]; 
      l2.attributedText = [self getStyledString:[NSString stringWithFormat:@"£ %@", self.s_priceOfRooms]]; 
     }else if(indexPath.row==1){ 

      l1.attributedText = [self getStyledString1:@"Government Taxes:"]; 
      l2.attributedText = [self getStyledString:[NSString stringWithFormat:@"£ %@", govtTaxes]]; 
     }else if(indexPath.row==2){ 

      l1.attributedText = [self getStyledString1:@"Enhancement Total:"]; 
      l2.attributedText = [self getStyledString:[NSString stringWithFormat:@"£ %@", enhancementTotal]]; 
     }else if(indexPath.row==3){ 

      l1.attributedText = [self getStyledString1:@"Total Charges"]; 
      l2.attributedText = [self getStyledString:[NSString stringWithFormat:@"£ %@", grandTotal]]; 
     } 



    } 
    return cell; 


} 


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    sectionCount = (int)[sectionArray count]; 
    return sectionCount; 
} 

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { 

    view.tintColor = GOLDEN_COLOR; 

} 

-(NSAttributedString*)getStyledString:(NSString*)input { 
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:input attributes:@{NSForegroundColorAttributeName:GOLDEN_COLOR, NSFontAttributeName:ARIAL_FONT}]; 
    return str; 
} 

-(NSAttributedString*)getStyledString1:(NSString*)input { 
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:input attributes:@{NSFontAttributeName:ARIAL_FONT_BOLD}]; 
    return str; 
} 

-(NSAttributedString*)getStyledString2:(NSString*)input { 
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:input attributes:@{NSFontAttributeName:ARIAL_FONT}]; 
    return str; 
} 

Ich habe einen ViewController erstellt und eine Tabellenansicht hinzugefügt. Einige Daten werden in Zellen aufgefüllt und dann angezeigt. Wenn ich es starte, sehe ich zunächst keine Daten in meinen Zellen. Wenn die Tabellenansicht jedoch gescrollt wird, zeigen die Zellen die tatsächlichen Daten an. Ich verstehe nicht, was der Grund sein könnte. Irgendwelche Hinweise bitte ???Tabellenansichtszellen zeigen die tatsächlichen Daten nur nach einmaligem Scrollen

Ich möchte meine Zellen dynamisch Größe ändern, da Daten von zufälliger Größe sein können. Daten werden nur nach einmaligem Scrollen angezeigt.

+0

einen Haltepunkt an numberOfRowsInSection Methode und sehen, was es zurückgibt, bevor Sie scrollen und scrollen. –

+0

tun Sie Ihre neu laden in Hauptwarteschlange und Layout benötigt –

+0

@Teja: Die Tabellenansicht Skelett ist vollkommen okay, d. H. Ich bekomme die richtige Anzahl von Zeilen in jedem Abschnitt von selbst zu beginnen. Nur dass sie alle leer sind. Wenn eine Zelle gescrollt wird, werden ihre Daten angezeigt. – crypt

Antwort

0

Dieses Problem bezieht sich auf die Verwendung von UITableViewAutomaticDimension und hat an anderen Stellen als auch berichtet worden. Also diese Zeile Code, löste mein Problem:

Dies lädt nur alle Tabellenabschnitte und Zeilen vor der Anzeige. Dem Benutzer werden also keine leeren Zeilen angezeigt. Siehe: http://www.appcoda.com/self-sizing-cells/

0

In setupTable überprüfen Sie und orderedData, um sicherzustellen, dass sie nicht leer sind. Fügen Sie eine Assertion in setupTable, zum Beispiel

sectionArray = [[SummaryModel sharedInstance] getTableSections:self.s_sendEnhancementServerDict]; 
orderedData = [[SummaryModel sharedInstance] getOrderedData:self.s_sendEnhancementServerDict]; 
NSAssert([sectionArray count] && [orderedData count], @"No data!"); // add this line 
[self.bookingsTableView reloadData]; 
0

Sie verwenden Nachladedaten im Hauptthread (viewDidLoad) u müssen nur unten Versand async wie der Code verwenden:

dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.mytable reloadData]; 

}

+0

Warum muss ich das tun, wenn mein gesamter Code im Hauptthread selbst läuft? – crypt

+0

Ja, Ihr Code läuft im Hauptthread, um eine Hintergrundaufgabe im Hauptthread zu erstellen, müssen Sie die Dispatch Async-Funktion aufrufen und eine Priorität angeben, um tatsächlich einen anderen Thread zu erhalten. –

Verwandte Themen