我試圖創建一個簡單的應用程序,其中我有一個應該顯示文件內容的TableView。我在IB中創建了一個表視圖,並將它的代表和數據源拖到文件的所有者,我手動創建了一個包含2個項目的1個數組的.plist文件。無法將文件的內容從.plist文件中獲取到數組中
在我的TableViewController.h我已經聲明瞭一個數組。
NSArray * posts;
我在執行文件我已經宣佈了UITableViewDataSource這樣所需的方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"Returning num sections");
return posts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// create a cell
UITableViewCell * post = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"post"];
// fill it with content
post.textLabel.text = [posts objectAtIndex:indexPath.row];
// return it
return post;
}
而且在我的ViewController「viewDidLoad中」的方法我嘗試我的文件的內容添加到了「的帖子'array like this:
- (void)viewDidLoad
{
NSString * postFile = [[NSBundle mainBundle] pathForResource:@"Posts" ofType:@"plist"];
posts = [[NSArray alloc] initWithContentsOfFile:postFile];
NSLog(@"%@", postFile);
NSLog(@"%i", posts.count);
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
NSLog(@「%i」,posts.count);返回0,儘管我已將值添加到我的.plist文件。並且表格視圖中不顯示任何內容。
建議如何解決這個問題將不勝感激。
和'viewDidLoad'第一個日誌的輸出是什麼?它不是零嗎? –
當您構建應用程序時,您的Posts.plist是否已預填充,或者您是否在向其動態添加內容? – Joe
我手動創建它並添加了初始值,但我將保存用戶職位。我將該文件放入我的Xcode(4)項目的「支持文件」目錄中。 – Anders