2013-08-04 81 views
0

我正在使用本教程介紹如何創建彈出式菜單。 http://www.appcoda.com/ios-programming-sidebar-navigation-menu/我的表格視圖無法填充

我在閱讀教程時瞭解了它,現在我正試圖將它實現到我的應用程序中。

我在我可以按菜單按鈕並彈出菜單出現的階段。問題是,它不會填充我的表格視圖單元格。

我爲每個表視圖單元格設置標識符,然後在代碼中將它們引用爲完整的數組。

我知道在教程中如何在定義數組中的內容時拼寫其中的一個標識符時,程序會崩潰。在我的應用程序中,情況並非如此。希望這可以幫助確定問題。它甚至不會改變代碼的第一部分的顏色。

這是代碼。

#import "SidebarViewController.h" 
#import "SWRevealViewController.h" 


@interface SidebarViewController() 

@property (nonatomic, strong) NSArray *menuItems; 


@end 

@implementation SidebarViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
self = [super initWithStyle:style]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f]; 
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f]; 
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f]; 

_menuItems = @[@"markup",@"tax"]; 

} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return [self.menuItems count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row]; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

return cell; 
} 

任何幫助將是偉大的。

感謝

+0

即使應該執行的第一段代碼應該改變顏色,使其非常黑暗(這在我的教程文件中有效),但我認爲代碼和對象之間的連接存在問題。我已經檢查了連接檢查器,並且我的當前項目與我的教程文件具有完全相同的連接。 – user1923975

+0

可能聽起來像一個愚蠢的問題,但你檢查你的表視圖控制器對象的類? – Yazid

回答

0

OMG我覺得像這樣的小白。事實證明,ViewControler沒有目標,因此它沒有響應。

我很抱歉浪費你的時間,我現在真的很難過。

2

所有你正在做的是創造有兩個字符串文字的數組。您必須設置爲textLabel的text財產在下面的方法查看錶單元格來顯示字符串:

- (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 = [_menuItems objectAtIndexPath:indexPath.row]; 
    return cell; 
} 
+3

一定要添加'tableView.delegate = self;'和'tableView.dataSource = self;'並正確實現這兩個。 – 2013-08-04 20:22:18

+0

很好的回答! –

+0

我檢查過,代表和數據源都設置爲self。對不起Kraken,那些代碼沒有做任何事情,仍然顯示空白。 – user1923975