UITableViewCell 数据*最初*未显示在 UISplitViewController 中

发布于 2024-09-06 08:44:15 字数 3472 浏览 2 评论 0原文

我有一个通用的 UISplitViewController (RootViewController 中的 UITableView 和详细视图中的其他内容)

我用两种方式修改了 UITableViewCells:

  1. 行的高度为 55
  2. UITableViewCells 包含 UIImage 和 UILabel

总共有 50 行

问题:当我启动应用程序时,前 13 行(可见的行)不显示。我得到空行。

如果我从前 13 行滚动离开,然后向后滚动……它们就会出现。

对我做错了什么有什么想法吗?

这是我的相关代码(我省略了像 dealloc 这样的一般内容)。

////////// header file /////////////////////////
#import <UIKit/UIKit.h>

@class c_cell;  // my custom UITableViewCell class (with cell height of "55")
@class DetailViewController;

@interface RootViewController : UITableViewController {
    DetailViewController *detailViewController;
    IBOutlet c_cell *cc;
    NSArray                 *cell_names;
    NSArray                 *cell_tlas;
}

@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;

@property (nonatomic, retain) IBOutlet c_cell   *cc;
@property (nonatomic, retain) NSArray *cell_names;
@property (nonatomic, retain) NSArray *cell_tlas;

@end


/////////////////  end header file /////////////////////

实施文件

  #import "RootViewController.h"
    #import "DetailViewController.h"
    #import "c_cell.h"


    @implementation RootViewController

    @synthesize detailViewController;

    @synthesize cc;
    @synthesize cell_names;
    @synthesize cell_tlas;


    #pragma mark View lifecycle

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.clearsSelectionOnViewWillAppear = NO;
        self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);

    }

    - (void)viewWillAppear:(BOOL)animated { 
        if (!self.cell_names) {
            self.cell_names = [NSArray arrayWithObjects:
                                  @"123",
                                  @"456",
                                  @"789", nil];     // 50 records in total...not just 3 as shown here
        }

        if (!self.cell_tlas) {
            self.cell_tlas = [NSArray arrayWithObjects:
                                 @"aaa",
                                 @"bba",
                                @"cca", nil]; // 50 records in total...not just 3 as shown here
        }
    }


    #pragma mark Table view data source
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
        return 1;
    }


    - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
        return 50;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 55;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        c_cell *cell   = (c_cell *)[tableView dequeueReusableCellWithIdentifier:@"ccc"];  // c_cell is my custom cell class
        if (!cell) {
            [[NSBundle mainBundle] loadNibNamed:@"c_cell" owner:self options:nil];
            cell = self.cc;
        }

        NSString *cell_string           = [self.cell_names objectAtIndex:indexPath.row];
        NSString *cell_tla              = [self.cell_tlas objectAtIndex:indexPath.row];
        NSString *cell_image_name       = [NSString stringWithFormat:@"%@.png", cell_tla];
        cell.cell_image.image           = [UIImage imageNamed:cell_image_name];
        cell.cell_label.text            = cell_string;

        return cell;
    }



    @end

I have a generic UISplitViewController (UITableView in the RootViewController and other stuff in the detail view)

I have modified UITableViewCells in 2 ways:

  1. The rows have a height of 55
  2. The UITableViewCells contain both a UIImage and UILabel

There are 50 rows total

PROBLEM: When I launch the app, the first 13 rows (the ones that are visible) DO NOT DISPLAY. I get empty rows.

If I scroll away from the first 13 rows and then scroll back…THEY APPEAR.

Any thoughts on what I'm doing wrong?

Here's my relevant code (I omitted general stuff like dealloc).

////////// header file /////////////////////////
#import <UIKit/UIKit.h>

@class c_cell;  // my custom UITableViewCell class (with cell height of "55")
@class DetailViewController;

@interface RootViewController : UITableViewController {
    DetailViewController *detailViewController;
    IBOutlet c_cell *cc;
    NSArray                 *cell_names;
    NSArray                 *cell_tlas;
}

@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;

@property (nonatomic, retain) IBOutlet c_cell   *cc;
@property (nonatomic, retain) NSArray *cell_names;
@property (nonatomic, retain) NSArray *cell_tlas;

@end


/////////////////  end header file /////////////////////

implementation file

  #import "RootViewController.h"
    #import "DetailViewController.h"
    #import "c_cell.h"


    @implementation RootViewController

    @synthesize detailViewController;

    @synthesize cc;
    @synthesize cell_names;
    @synthesize cell_tlas;


    #pragma mark View lifecycle

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.clearsSelectionOnViewWillAppear = NO;
        self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);

    }

    - (void)viewWillAppear:(BOOL)animated { 
        if (!self.cell_names) {
            self.cell_names = [NSArray arrayWithObjects:
                                  @"123",
                                  @"456",
                                  @"789", nil];     // 50 records in total...not just 3 as shown here
        }

        if (!self.cell_tlas) {
            self.cell_tlas = [NSArray arrayWithObjects:
                                 @"aaa",
                                 @"bba",
                                @"cca", nil]; // 50 records in total...not just 3 as shown here
        }
    }


    #pragma mark Table view data source
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
        return 1;
    }


    - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
        return 50;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 55;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        c_cell *cell   = (c_cell *)[tableView dequeueReusableCellWithIdentifier:@"ccc"];  // c_cell is my custom cell class
        if (!cell) {
            [[NSBundle mainBundle] loadNibNamed:@"c_cell" owner:self options:nil];
            cell = self.cc;
        }

        NSString *cell_string           = [self.cell_names objectAtIndex:indexPath.row];
        NSString *cell_tla              = [self.cell_tlas objectAtIndex:indexPath.row];
        NSString *cell_image_name       = [NSString stringWithFormat:@"%@.png", cell_tla];
        cell.cell_image.image           = [UIImage imageNamed:cell_image_name];
        cell.cell_label.text            = cell_string;

        return cell;
    }



    @end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许一世地老天荒 2024-09-13 08:44:15

在viewWillAppear底部添加[[self tableView] reloadData];

At the bottom of viewWillAppear, add [[self tableView] reloadData];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文