2011-12-02 109 views
1

我有一個觀點,即增加了一個圖在上面以這種方式:的cellForRowAtIndexPath不會被調用

- (void)showAreaEditView { 

    NSLog(@"SHOWING AREA EDITOR VIEW"); 

    if (self.thisAreaEditorView == nil) { 

     // Create View 
     AreaEditorView *tmpViewController = [[AreaEditorView alloc] initWithNibName:@"AreaEditorView" bundle:nil]; 
     self.thisAreaEditorView = tmpViewController; 
     [tmpViewController release]; 

     // Hide the back button 
     self.thisAreaEditorView.navigationItem.hidesBackButton = YES; 

    } 

    self.thisAreaEditorView.myInspectionID = self.myInspectionID; 
    self.thisAreaEditorView.loggedIn = loggedIn; 
    self.thisAreaEditorView.loggedInGroup = loggedInGroup; 

    // Slide view up 

    [self.view addSubview:thisAreaEditorView.view]; 


    CGRect endFrame = CGRectMake(self.view.frame.size.width/2 - thisAreaEditorView.view.frame.size.width/2, 
           self.view.frame.size.height/2 - thisAreaEditorView.view.frame.size.height/2, 
           thisAreaEditorView.view.frame.size.width, 
           thisAreaEditorView.view.frame.size.height); 

    CGRect startFrame = endFrame; // offscreen source 

    // new view starts off bottom of screen 
    startFrame.origin.y += self.view.frame.size.height; 
    self.thisAreaEditorView.view.frame = startFrame; 

    // start the slide up animation 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:.6]; 
    thisAreaEditorView.view.frame = endFrame; // slide in 
    [UIView commitAnimations]; 

} 

我敢肯定,你可以忽略的滑動部分,我覺得addSubview是相關的。

然後在thisAreaEditor中,我使用表格和按鈕等來查看視圖。 UITableView委託/數據源將正常進入文件所有者。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    NSLog(@"numberOfRowsInSection returning %d", [tableData count]); 
    [tableData count]; 

} 

這個函數返回numberOfRowsInSection 4

- (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]; 
    } 

    // Configure the cell... 
    NSString *thisText = [tableData objectAtIndex:indexPath.row]; 
    cell.textLabel.text = thisText; 

    NSLog(@"looking at cell %d text:%@", indexPath.row, thisText); 

    return cell; 

} 

不過的cellForRowAtIndexPath不會被調用。

我在這裏不知所措,我不知道它如何看起來工作正常,但其中一個委託函數根本不會被調用。

我試過[bigTable reloadData]等等,表只是永遠不會被填充,並沒有日誌從函數輸出。

在此先感謝。

回答

8

你可能剛剛編輯過這個,如果是的話,我很抱歉,但看起來你忘了返回tableData的計數。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"numberOfRowsInSection returning %d", [tableData count]); 
    return [tableData count]; 
} 
+0

哦,我的,你是對的。我錯過了'numberOfRowsInSection'中的'return'。不能相信它那麼簡單,謝謝! – Batnom

+0

im在'numberOfRowsInSection'中使用switch語句來處理8個不同的部分。並忽略了不包括「return」這個詞。有些問題可能很難解決。 – lizzy81

0

也許你沒有將tableView委託設置爲self或數據源爲self。添加以下代碼&看看現在的工作 -

[tableView setDelegate:self]; 
[tableView setDataSource:self]; 
在你的頭文件

也繼承這些代表 - UITableViewDelegate, UITableViewDataSource

@interface yourViewController: UIViewController <UITableViewDelegate, UITableViewDataSource> 

希望這有助於。

+0

不幸的是仍然無法正常工作。在'viewWillAppear'中,我調用'populateTableData'函數觸發'[bigTable reloadData]',所以'titleForHeaderInSection'被調用(不返回任何內容),然後'numberOfRowsInSection'(返回4),就是這樣。 – Batnom

3

看來你很想念UITableViewDelegate。

如果您使用Interface Builder,請右鍵單擊表格視圖插座並將delegatedatasource拖動到File's Owner

如果不使用Interface Builder添加這個,你初始化你的tableView

bigTable.delegate = self; 
bigTable.dataSource = self; 

記住要導入的UITableViewDelegate和UITableViewDataSource協議,就像Srikar說。

希望這是對任何幫助。

乾杯!

0

這是一個較舊的鏈接,但我想更新此信息以解決我如何解決此問題。 對我來說,問題是數組填充表有0行,所以cellForRowAtIndexPath從未被調用過。 確保您用來填充表的數組中包含數據。

相關問題