2013-02-10 178 views
6

我看到這個在這裏被問了很多次,但沒有一個解決方案的工作對我來說後消失內容。這裏是我的代碼片段:UITableView的自定義單元格滾動

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellClassName = @"DemoTableViewCell_sched"; 
    DemoTableViewCell_sched *cell = [tableView dequeueReusableCellWithIdentifier:CellClassName]; 

    if (cell == nil) 
    { 
     NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil]; 
     cell = [topLevelItems objectAtIndex:0]; 
    } 
    cell.fooData = [self.bugs objectAtIndex:indexPath.row];  
    return cell; 
} 

然後我在ViewDidLoad

cellLoader = [UINib nibWithNibName:@"DemoTableViewCell_sched" bundle:[NSBundle mainBundle]]; 
+0

你是否檢查過你的單元格中是否指定了標識符? – 2013-02-10 15:07:38

+0

是的。在自定義單元格中,我確定標識符是「DemoTableViewCell_sched」。我還應該檢查什麼? – user1235155 2013-02-10 15:16:34

+0

[This thread](http://stackoverflow.com/a/36037259/4034301)爲我工作。偉大的解決方案..! – 2016-03-16 13:37:46

回答

2

問題走了既可以當:

  1. 表視圖包含許多細胞(10+)

  2. 每個單元具有大於50

    的高度

奇怪。

+0

此鏈接適用於我。 http://stackoverflow.com/a/36037259/4034301 – 2016-03-16 13:38:10

-1

您需要根據其內容計算出正確的高度爲每個細胞。

這可以在UITableView的委託方法來完成:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
     CGFloat height; 
     //Calculate height for each cell 
     return height; 
} 
16

在我的情況下,發生了什麼事是,我沒有保持強烈的指針的tableview的數據源。所以當tableview被滾動時,數據源已經被釋放並設置爲nil,這導致tableview爲numRowsForSection獲取0,爲cellForRowAtIndexPath獲取nils。

如果任何人有類似的問題,你可能會如果你的數據源和/或自定義對象適當保留的樣子。

+0

感謝分享,它解決了我的問題! – N0un 2017-07-20 09:15:47

2

在我的情況下,問題是以下幾點:

  1. 我有哪裏視圖控制器的觀點並沒有推動或模態呈現一種特殊情況,但我已經添加它作爲一個子視圖(view.addSubview)至另一種觀點。

  2. 這樣,實際視圖被其超級視圖所保留,但ViewController對象(它是用於表視圖的DataSource)未被保留並且內存不足。

  3. 我不得不視圖控制器分配給一個(強)變量,以便它停留在存儲器中。

1

我知道這個問題已經有了答案,只是讓Google:

在我的情況下,問題是,意見仍然在UITableViewCell但在frame財產由於滾動到錯誤後的意見,他們在下一個UITableViewCell下,我看不到他們。

我認爲,作爲公認的答案已經明確表示:

問題走了既可以當:

  • 表視圖包含許多細胞(10+),

  • 每個單元的高度均大於50.

他有類似的問題,我的。

-1

在TableView中的這種行爲讓我頭撞字面。原來我用了

@property (weak, nonatomic) NSMutableArray *itemsArray; 

,而不是

@property (strong, nonatomic) NSMutableArray *itemsArray; 

希望這個信息幫助。

+0

沒有什麼幫助。如果向下滾動,單元格仍然會重新生成並顯示錯誤的UI – 2016-01-27 02:29:07

+0

,請檢查在使用cellForRowAtIndexPath返回單元格時,您正在使用的NSArray/NSDictionary傳遞的數據是否可以再次檢查約束(關於frame&代碼界限)或Autolayout設置。同樣考慮向下投票@wcobbett的答案,因爲我剛剛在代碼中寫道,以避免閱讀冗長的答案並指出您在理論上或代碼中面臨的問題。 – skypirate 2016-01-27 02:51:19

8

您是否將UITableViewcontroller添加爲addSubView? 然後你應該這樣做:

1) addChildViewController,then 
2) addSubview 
The problem is delegate and datasource of the tableview getting nil since the parent controller unable to a reference to the UITableViewController.