将数据从 .plist 加载到 iPhone 的 TableView
我正在练习《开始 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码存在一些问题,但根本问题是您永远不会将 UITableView 添加为 UIViewController 中的视图/子视图:
或者,您可以在 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:
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 theloadView
method:Check out the UIViewController class reference for more details