2016-05-31 14 views
0

我是iOS開發的初學者。我面臨一個問題,我需要一點幫助。爲什麼UITableViewCell在滑動視圖時改變了它的大小

我寫了一個小程序來學習自定義的UITableViewCell。它剛開始時效果很好;之後,當我滑動視圖時,它會更改單元格的大小。我對我可能會出錯的地方感到困惑。 ContentView只有一個視圖是UIImageView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    HWHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWHomePageCell"]; 
    if (cell == nil) 
    { 
     cell = [HWHomePageCell homePageCell]; 
    } 
     cell.imageView.image = [UIImage imageNamed:@"XD"]; 

    return cell; 
} 

These are the pictures. Top two are good, but when I slide down, as you can see, the 3rd doesn't work well

enter image description here

+0

您可能需要添加cell.imageView.contentMode = UIViewContentModeScaleAspectFill;以下鏈接可能有幫助。 http://stackoverflow.com/questions/6638623/uiviewcontentmodescaleaspectfill-not-clipping –

回答

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    HWHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWHomePageCell"]; 

     cell = [HWHomePageCell homePageCell]; 

     cell.imageView.image = [UIImage imageNamed:@"XD"]; 

    return cell; 
} 

當您使用自定義的,然後電池無需檢查電池是否是零或沒有。所以請刪除這個條件並實施它。

+0

非常感謝!它的工作原理。但請你告訴我原因?該方法發生了什麼 - dequeueReusableCellWithIdentifier。爲什麼當我註釋掉「cell = [HWHomePageCell homePageCell];」在你的代碼中,錯誤再次顯示? –

+0

當第一次tableview只加載可見的單元格被分配在內存中但仍保持單元格不加載。 「dequeueReusableCellWithIdentifier」方法是每個單元格的唯一標識符。每次調用此方法時,都會滾動tableview。但每次都不是傳感器。其標識符的標識單元。 –

0
cell.imageView.contentMode = UIViewContentModeScaleAspectFill; 
相關問題