将数据从 .plist 加载到 iPhone 的 TableView

发布于 2024-12-11 04:34:51 字数 3464 浏览 0 评论 0原文

我正在练习《开始 iPhone 4 开发:探索 iOS SDK》中的示例。第 8 章展示了如何将数据从 plist 加载到 tableview 中。该示例是使用 Interface Builder 完成的。我想通过代码实现但遇到问题。屏幕上没有显示任何内容...

这是 .h

#import <UIKit/UIKit.h>
@interface FifthViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSDictionary *names;
    NSArray      *keys;
}
@property (nonatomic, retain) NSDictionary *names;
@property (nonatomic, retain) NSArray *keys;
@end

这是 .m

#import "FifthViewController.h"
@implementation FifthViewController
@synthesize names;
@synthesize keys;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStyleGrouped];
    [table setDataSource:self];
    [table setDelegate:self];


    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames"
                                                 ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc]
                      initWithContentsOfFile:path];
    self.names = dict;
    [dict release];

    NSArray *array = [[names allKeys] sortedArrayUsingSelector:
                  @selector(compare:)];
    self.keys = array;
    [self.view addSubview:table];
    [table release];
}

- (void)dealloc 
{
    [names release];
    [keys release];
    [super dealloc];    
}

- (void)viewDidUnload
{
    self.names = nil;
    self.keys = nil;
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return [keys count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    return [nameSection count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                         SectionsTableIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:SectionsTableIdentifier] autorelease];
    }

    cell.textLabel.text = [nameSection objectAtIndex:row];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    NSString *key = [keys objectAtIndex:section];
    return key;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{
    return keys;
}

@end

I'm praticing example from Beginning iPhone 4 Development:Exploring the iOS SDK. In Chapter 8 it shows how to load data from a plist into a tableview. The example was done with Interface Builder. I want to do it by code but encounter problems. Nothing shows up on screen....

Here's the .h

#import <UIKit/UIKit.h>
@interface FifthViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSDictionary *names;
    NSArray      *keys;
}
@property (nonatomic, retain) NSDictionary *names;
@property (nonatomic, retain) NSArray *keys;
@end

Here's .m

#import "FifthViewController.h"
@implementation FifthViewController
@synthesize names;
@synthesize keys;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStyleGrouped];
    [table setDataSource:self];
    [table setDelegate:self];


    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames"
                                                 ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc]
                      initWithContentsOfFile:path];
    self.names = dict;
    [dict release];

    NSArray *array = [[names allKeys] sortedArrayUsingSelector:
                  @selector(compare:)];
    self.keys = array;
    [self.view addSubview:table];
    [table release];
}

- (void)dealloc 
{
    [names release];
    [keys release];
    [super dealloc];    
}

- (void)viewDidUnload
{
    self.names = nil;
    self.keys = nil;
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return [keys count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    return [nameSection count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                         SectionsTableIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:SectionsTableIdentifier] autorelease];
    }

    cell.textLabel.text = [nameSection objectAtIndex:row];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    NSString *key = [keys objectAtIndex:section];
    return key;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{
    return keys;
}

@end

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

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

发布评论

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

评论(1

还给你自由 2024-12-18 04:34:51

您的代码存在一些问题,但根本问题是您永远不会将 UITableView 添加为 UIViewController 中的视图/子视图

UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStyleGrouped];
[table setDataSource:self];
[table setDelegate:self];
[table release];

// These two lines are what you're missing:
self.view = table;
[table release];

或者,您可以在 Interface Builder 中创建界面并避免必须以编程方式创建这些东西。

此外您不应该在您所在的位置创建 UITableView。 viewDidLoad 应用于在创建所有界面组件后执行任何其他操作。

您应该将 UITableView 的创建移至 loadView 方法:

查看 UIViewController 类参考以了解更多详细信息

There are a few issues with your code, but the root issue is that you're never adding the UITableView as a view/subview in your UIViewController:

UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStyleGrouped];
[table setDataSource:self];
[table setDelegate:self];
[table release];

// These two lines are what you're missing:
self.view = table;
[table release];

Alternatively, you could create the interface in Interface Builder and avoid having to create this stuff programmatically.

In addition you should not be creating your UITableView where you are. viewDidLoad should be used to perform any additional operations after all your interface components have been created.

You should move the creation of your UITableView to the loadView method:

Check out the UIViewController class reference for more details

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