2
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 =@"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++){

        cellIdentifier=@"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 =@"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;
}

I have made a ViewController and added a table view in it. Some data is populated in cells and then displayed. When I run it, initially I don't see any data in my cells. But when the tableview is scrolled cells start showing the actual data. I don't understand what could be the reason. Any pointers please???

I want to dynamically resize my cells as data can be of random size. Data shows only after scrolling once.

crypt
  • 449
  • 5
  • 15

4 Answers4

1

This problem is related to using UITableViewAutomaticDimension and has been reported at other places as well. So this line of code, solved my problem:

-(void)viewDidAppear:(BOOL)animated {
    [self.tableView reloadData];
}

This just reloads all the table sections and rows before displaying. So user does not experience blank rows. Refer: http://www.appcoda.com/self-sizing-cells/

crypt
  • 449
  • 5
  • 15
  • @ilesh if you still haven't found the solution, can you try this piece of code once. This is in swift. Though I haven't tested this, it works in similar situations. var doOnce : Bool = true override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() if doOnce { self.view.layoutIfNeeded() self.tableView.reloadData() self.view.layoutIfNeeded() } } – crypt Aug 20 '18 at 10:32
  • Let me know if you need the objective - c version of this code. – crypt Aug 20 '18 at 10:32
1

You need to use reloadData in the main thread (viewDidLoad). You need to use dispatch_async like the code below :

dispatch_async(dispatch_get_main_queue(), ^{
    [self.mytable reloadData];
}
koen
  • 5,383
  • 7
  • 50
  • 89
MedAmine.Rihane
  • 1,193
  • 10
  • 17
  • Why do I need to do that when my whole code is running in main thread itself? – crypt Jul 23 '16 at 18:27
  • yes your code is running in main thread so to make a background task in main thread u need to call Dispatch async function and specify a priority to actually get a different thread. – MedAmine.Rihane Jul 23 '16 at 19:17
  • @MedAmine.Rihane thanks for your reply, It is already in the main thread. it's not work for me. :( – Ilesh P Aug 21 '18 at 06:59
0

In setupTable, check sectionArray and orderedData to make sure they're not empty. Add an assertion in setupTable, e.g.,

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];
jp2g
  • 682
  • 4
  • 8
-1

Swift 5+ IOS 13 Xcode 11.2.1 + Amazing Answer

override func viewDidAppear(_ animated: Bool) {
    //  self.tablev.frame.origin.y = vnavigation.frame.maxY + 5
    //   tablevheight = self.tablev.frame.size.height
    self.bgview.backgroundColor = UIColor.init(patternImage: UIImage(named: "chat_bg.png")!)
    self.registerForKeyboardNotifications()
    UIView.performWithoutAnimation {
        tablev.beginUpdates()
        tablev.endUpdates()
    }
}
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34