2011-06-05 77 views
1

下面是我如何從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模式的上述情況?我猜我會使用一個單例返回一組名稱對象。我基本上想學習使用Model對象來表示我的數據的正確方式。

+0

[模型類的可能重複。他們真的是什麼意思?](http://stackoverflow.com/questions/6240860/model-classes-what-the-heck-do-they-really-mean) – 2011-06-05 04:53:12

+0

@josh - 這裏有兩個不同的問題。 [第一個問題](http://stackoverflow.com/questions/6240860/model-classes-what-the-heck-do-they-really-mean)是「爲什麼使用模型,我不能只使用數組,字典?「。這是「確定,我確信,我如何使用它們,這是我的嘗試?」。我很樂意讓他們都因爲這個區別而開放,否則我們會得到一個很大的問題來問兩個不同的事情。 – Kev 2011-06-05 12:40:25

回答

1

iOS MVC中的模型只是簡單地劃分了您的應用程序,以便將數據和應用程序算法(模型)與演示文稿和事件處理代碼分開。因此,請考慮創建一個新的Model類,以獲取,設置和保存您的應用程序數據。這個類應該沒有關於GUI的知識。

下面是一個模型示例,它通過處理加密解密來封裝應用程序算法

#import <Foundation/Foundation.h> 
#import <CommonCrypto/CommonCryptor.h> 
#import <CommonCrypto/CommonDigest.h> 
#import <Security/Security.h> 


@interface Model : NSObject { 
} 
+(id)modelGetInstance; 
+(NSData *)getRandomIV:(NSInteger)numBytes; 
-(id)init; 
-(NSString*)encrypt:(NSString*)plainText password:(NSString*)pw; // public 
-(NSString*)decrypt:(NSString*)cipherText password:(NSString*)pw; // public 
@end 

這與UNIX ENGINE-INTERFACE相似。這不是Smalltalk的MVC,其中View由MODEL(觀察者模式)中的更改直接更新。

+0

你能告訴我什麼+(id)modelGetInstance方法的實現看起來像什麼嗎? – NSExplorer 2011-06-05 06:15:53

+0

@iPhoneDeveloper我是「重新學習」Objective C,所以我將這種方法作爲便利構造的練習。通常我會這樣做:\t model = [[Model alloc] init];並在dealloc中發送釋放消息的模型,或者我相信你可以通過參數向便捷構造函數發送消息。我寫了一個不帶參數的便捷構造函數,如下所示:+(id)modelGetInstance { \t // id temp = [[self alloc] init]; \t // return [temp autorelease]; \t return [[[self alloc] init] autorelease]; }作爲練習。 – JAL 2011-06-05 06:28:53

1

如果您想要使用plists進行存儲並使用更多MVC模型進行編程,則需要將轉換函數從該模型寫入簡單的數據結構,反之亦然。舉例來說,如果你有一個Person模型:

NSMutableArray* tempArray = [[NSMutableArray alloc]initWithContentsOfFile:path]; 
NSMutableArray* people = [Person personArrayFromDictionaryArray:tempArray]; 

將數據保存到一個plist文件,那麼你就需要將數據序列化回簡單的數據結構前。

NSMutableArray* arrayToStore = [Person dictionaryArrayFromPersonArray:people]; 

如果你只需要保持名稱的列表的軌道,它可能是最好代表他們作爲字符串的整個程序中的數組。

+0

感謝您的輸入。所以你建議我首先創建一個People類,其中包含名稱,地址,電話號碼等屬性。然後創建另一個具有這些轉換功能的類? – NSExplorer 2011-06-05 05:01:58

+0

我建議將轉換方法包括爲Person類的類方法。如果您希望將轉換代碼從模型代碼中解耦出來,那麼像「PersonSerializer」這樣的單獨的類是一個很好的解決方案。 – jfocht 2011-06-05 13:37:04

相關問題