2013-03-27 48 views
0

我有這個奇怪的問題。自定義的UITableViewCell與UIImageView不加載圖像的某些單元格

基本上我有一個UITableViewCell子類,它寫了一些插口並將其連接到筆尖。 Cell.m

@synthesize myImageView = _myImageView; // do i need this? 


- (void) setUpCell... image:(UIImage*)image 
{ 
     if(image) 
     self.myImageView.image = image; 
    else 
     self.myImageView.image = nil; 
} 

在其管理該小區的表格視圖的視圖控制器Cell.h

子類

@property (weak, nonatomic) IBOutlet UIImageView *myImageView; 

亞類中,

我已與所登記的筆尖使用以下代碼:

[self.tableView registerNib:[UINib nibWithNibName:pathToNibForBaseMenuCell bundle:nil] forCellReuseIdentifier:identifierForBaseMenuCell]; 

在的cellForRowAtIndexPath,我出列單元,並設置圖像...

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForBaseMenuCell]; 

switch (indexPath.section) { 


    case 2: 
     [cell setUpCell...image:self.cogWheelImageForSettingsIcon]; 
     break; 

    default: 
     break; 
} 

return cell; 

}

奇怪的是,說我有10個細胞中的區段2(如上),和我設置它們以相同的方式起作用,有些單元顯示齒輪圖標,有些則不顯示!滾動這些細胞進出視圖似乎改變哪些細胞顯示圖標,哪些沒有。

這意味着註冊單元格,包括設置重用標識符,在nib文件中設置子類,所有這些類型的東西都很好(常見的陷阱)。

這種行爲模式在我看來,與內存有關的 - 好像有什麼東西正在某處釋放...

下面是說明我的意思的圖片:

enter image description here

請幫幫我!

感謝

Vb的

編輯:

OK僅僅是明確的,沒有使用任何數據與此表IM,但儘管如此,有沒有原因,我那些嵌齒輪不該」不會出現!

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
switch (section) { 
    case 0: 
     return 1; 
     break; 

    case 1: 
     return 10; 
     break; 

    case 2: 
     return 10; 
     break; 

    default: 
     return 0; 
     break; 
} 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForBaseMenuCell]; 

switch (indexPath.section) { 
    case 0: 
     [cell setUpCell... text:@"Username" image:nil]; 
     break; 

    case 1: 
     [cell setUpCell... text:@"Magazine" image:nil]; 
     break; 

    case 2: 
    { 
     NSString* str = [NSString stringWithFormat:@"Settings: %d",indexPath.row]; 
     [cell setUpCell... text:str image:self.cogWheelImageForSettingsIcon]; 
     break; 
    } 

    default: 
     break; 
} 

return cell; 
} 

的輸出是...

enter image description here

我的觀點是,如果IM設置相同的圖像,其是在第二部分中的「設置」,爲什麼只有做的所有單元格其中一些有圖像顯示?

自定義Cell類:

@interface CustomCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView; 
@property (weak, nonatomic) IBOutlet UILabel *mainLabel; 
@property (weak, nonatomic) IBOutlet UILabel *arrowView; 
@property (weak, nonatomic) IBOutlet UIImageView *myImageView; 
- (void) setUpCelltext:(NSString*)text image:(UIImage*)image; 


@implementation CustomCell 

- (void) setUpCelltext:(NSString*)text image:(UIImage*)image 
{ 
self.backgroundImageView.image = [UIImage imageNamed:pathForBackgroundImage]; 
self.myImageView.layer.cornerRadius = kCornerRadiusOfImageView; 
self.myImageView.layer.masksToBounds = YES; 

if(image) 
    self.myImageView.image = image; 
else 
    self.myImageView.image = nil; 

NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:self.fontForText forKey:NSFontAttributeName]; 
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attrsDictionary]; 
self.mainLabel.attributedText = attrString; 
} 
@end 

細胞具有自定義字體,箭頭圖,背景視圖 - 他們所有的工作,因爲他們應該!它們根據部分而改變。它只是不起作用的圖像。您可以看到,由cellForRowAtIndexPath調用的此函數將設置圖像(如果提供),或者將imageview設置爲零(如果沒有)。

+0

只需對代碼中隱藏的問題發表評論即可,因爲Objective-C的最新版本不再需要* @ synthesize *。它會自動完成「_」約定 – rdurand 2013-03-27 16:52:34

+0

謝謝,我刪除它! – Vazzyb 2013-03-27 16:58:51

+0

看起來你每次都在'UITableView'數據源中傳遞'self.cogWheelImageForSettingsIcon',所以我不明白你有沒有可能讓每個單元格都沒有圖標圖像。 – RileyE 2013-03-27 17:27:29

回答

1

原來,這個問題與在我的筆尖中使用'imageView'作爲我的imageView的名稱有關。這是後來改變,但原來的關係是對UITableViewCell的實例變量,而不是自定義單元格。

道德故事:不要調用自定義表視圖單元格'imageView'的圖像視圖!

+0

新的我有完全相同的問題。使用正確的子類名命名TableViewCell類屬性後,確保在storyboard上設置關係,從而解決了我的問題。 ps。當我確定該部分的一切正常時,我注意到代碼中有空的IBOutlet關係。 – mass 2013-10-20 23:45:49

0

UITableViewCell s使用dequeueReusableCellWithIdentifier:從舊單元格重新繪製。如果您不希望單元格被重複使用,那麼您需要每次在tableView:cellForRowAtIndexPath:函數中初始化一個新單元格。

但是,如果要重複使用這些單元格(建議),則應該每次調用- (void) setUpCell... image:(UIImage*)image函數以便在tableView:cellForRowAtIndexPath:以內,以確保圖像僅設置爲正確的單元格。

您似乎也正在使用存儲在您的UITableViewCell子類中的數據,但是當單元格在不同的行上重用時,它仍然會認爲它與原始行是相同的單元格。

例如:在第2行創建了一個單元格,並且有一個圖標圖像。單元格2不在屏幕上,現在用於第16行。第16行不應該有圖像,但單元格仍然認爲它是與第2行相同的單元格,並繼續顯示其圖像。

如果您希望每個單元格都被重複使用並且每個單元格都有唯一的數據,那麼只使用外部數據集(例如:NSArray),該數據集提供繪製單元格所需的信息並將該信息傳遞給每個單元格,而不是取決於您創建單元格時所提供的信息。

+0

但這正是我正在做的。最後一段代碼來自我的cellForRowAtIndexPath。即將進入第二部分的每個單元都有setUpCell調用它。 – Vazzyb 2013-03-27 16:35:49

+0

@Vazzyb哦。你做錯了。您需要從數據集中提取數據。你從單元格的數據中拉出來。這個單元格可以被任何行創建和重用,所以你需要一個外部數據集,你使用'indexPath.row'屬性引用 – RileyE 2013-03-27 16:41:34

+0

@Vazzyb請閱讀我的編輯。如果你想要一個例子,請發佈你的子類和完整的'tableView:cellForRowAtIndexPath:'代碼。 – RileyE 2013-03-27 16:47:12