【发布时间】:2011-09-08 14:54:51
【问题描述】:
下面是我如何从 plist 中读取数据并在表格视图中显示数据的简单示例。如果我要使用对象来表示我的模型,我会怎么做?
@interface RootViewController : UITableViewController {
NSMutableArray *namesArray;
}
@property (nonatomic, retain) NSMutableArray *namesArray;
@end
@implementation RootViewController
@synthesize namesArray;
- (void)viewDidLoad{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"plist"];
NSMutableArray *tempArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.namesArray = tempArray;
[tempArray release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [namesArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [namesArray objectAtIndex:indexPath.row];
return cell;
}
谁能告诉我按照 MVC 模式为上述场景构建模型的正确方法?我猜我会使用单例来返回一组 Name 对象。我基本上想学习使用模型对象来表示我的数据的正确方式。
【问题讨论】:
-
@josh - 这里有两个不同的问题。 first question 是“为什么要使用模型,我不能只使用数组、字典吗?”。这是“好的,我确信,我该如何使用它们,这就是我尝试过的?”。由于这种区别,我很高兴让它们都保持开放,否则我们会遇到一个大问题,问两个不同的事情。
标签: iphone objective-c ios model-view-controller