• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 表格 View 单元格仅在滚动一次后显示实际数据

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:26:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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)viewDidDisappearBOOL)animated {
    if(doesSendNotification){
        NSLog(@"summary view disappeared");
        [[NSNotificationCenter defaultCenter] postNotificationName"SummaryViewDismissedNotification" object:self];
    }
}

-(void)viewWillAppearBOOL)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)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section{

    if(section==0){

        return 3;

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

    } else {

        return 4;

    }
}

-(NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)section {

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

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 objectForKeyNSString*)[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;
}

我制作了一个 ViewController 并在其中添加了一个表格 View 。一些数据填充在单元格中,然后显示。 当我运行它时,最初我在单元格中看不到任何数据。但是当表格 View 滚动时,单元格开始显示实际数据。我不明白可能是什么原因。请大家指点???

我想动态调整单元格的大小,因为数据可以是随机大小。数据只在滚动一次后显示。



Best Answer-推荐答案


此问题与使用 UITableViewAutomaticDimension 有关,其他地方也有报告。所以这行代码,解决了我的问题:

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

这只是在显示之前重新加载所有表格部分和行。所以用户不会遇到空白行。引用:http://www.appcoda.com/self-sizing-cells/

关于ios - 表格 View 单元格仅在滚动一次后显示实际数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505598/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap