2011-09-15 44 views
0

我是Iphone的新手,我需要一些幫助才能在tableview上顯示文本。Iphone上的tableView中沒有文字

這裏是我的代碼:

.h 

@class MainMenuViewController; 

@interface RecherchePartenaireViewController : UIViewController <UINavigationControllerDelegate> { 

    MainMenuViewController *mainMenuViewController; 

    UINavigationController *navigationController; 

    GradientButton *rechercheButton; 

    RecherchePartenaireResultatListeViewControleur *recherchePartenaireResultatListeViewControleur; 

    IBOutlet UITableView *categorystable; 
    NSMutableArray *listData; 
} 

@property (nonatomic, retain) IBOutlet MainMenuViewController *mainMenuViewController; 

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 

@property (nonatomic, retain) IBOutlet GradientButton *rechercheButton; 

@property (nonatomic, retain) IBOutlet RecherchePartenaireResultatListeViewControleur *recherchePartenaireResultatListeViewControleur; 

@property (nonatomic, retain) IBOutlet UITableView *categorystable; 
@property(nonatomic, retain) NSArray *listData; 

@end 

在.M我:

@synthesize mainMenuViewController, navigationController, rechercheButton, recherchePartenaireResultatListeViewControleur,listData; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)dealloc 
{ 

    [mainMenuViewController release]; 
    [navigationController release]; 
    [rechercheButton release]; 

    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    listData = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil]; 
    NSLog(@"hey %@",listData); 

    [rechercheButton useRedDeleteStyle]; 

    // Do any additional setup after loading the view from its nib. 

} 

- (void)viewDidUnload 
{ 
    self.rechercheButton = nil; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 


    } 

    // Set up the cell... 
    cell.textLabel.text = @"label"; 

    /*[[cell textLabel] setText: [[listData objectAtIndex:indexPath.row] valueForKey:@"name"]];*/ 


    return cell; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return listData; 
} 

@end 

,我得到tableView.Where沒有什麼是我的錯?我在網上看到了很多例子,但是我不知道我做錯了什麼。誰能幫我?

+2

在你的Xib文件中,你是否將你的tableView的委託和數據源設置爲你的viewController? – Zoleas

+0

謝謝你......這就是問題......非常感謝:) – Gabrielle

+0

你想讓我把它寫在答案中嗎?就像你可以接受它一樣。 – Zoleas

回答

3

在你的XIB文件,你有沒有設置委託和您的tableView的數據源作爲您的viewController?

1

如果你的意思是你的tableView是可見的但沒有單元格/內容可見,也許你可以嘗試設置一個值;

編輯:在你的.m中設置行高爲44px。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
return 44.0; 
} 

還同意你需要返回的其他答案[listData count]而不僅僅是該方法的listData。

+0

你能更明確嗎?我只需要將這行放在.m中? – Gabrielle

+0

查看我編輯的Gabrielle。 – Madhu

+0

是一樣的:(.. – Gabrielle

2

如果你想使用電池的標準樣式在你UITableView,那麼你應該更換,你創造新的細胞與這一個,例如行:

更換

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

您還可以爲單元選擇另一個預定義樣式:UITableViewCellStyleDefault,UITableViewCellStyleValue1,UITableViewCellStyleValue2,UITableViewCellStyleSubtitle

此外,您在方法numberOfRowsInSection:中有錯誤。它應該看起來像這樣:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
{ 
    return [listData count]; 
} 
+0

我把[listData count]但tableview是空的。單元格是可見的,但它們都沒有 – Gabrielle

+0

你替換了我注意到的那一行嗎? – Nekto

2

您必須在下面進行更改。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    listData = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil]; 
    NSLog(@"hey %@",listData); 

    [rechercheButton useRedDeleteStyle]; 

    // Do any additional setup after loading the view from its nib. 

    [categorystable reloadData]; 
} 

而且還改變。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [listData count]; 
} 
2

更改這個樣子,

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [listData count]; 
} 
1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     return [listData count];//that many times cellFoeRowAt Index method Called. 
    } 
    in above case you are returning Array instead OF Count. 

歡呼