2013-01-18 28 views
0

我有tableviewcell設置爲自定義故事板中的tableview。每個單元格的右側有一個按鈕(單元格中的按鈕)。按鈕在tableviewcell不顯示/隱藏如預期,滾動

加載每個單元格時,根據條件,此按鈕應顯示或隱藏。但它沒有按預期工作。當我向下滾動並再次向上滾動時,現在正在顯示先前隱藏的按鈕(具有正確的條件)。

我對單元格中的圖像做了同樣的處理,即使在滾動時圖像也正確加載。

這裏是我的代碼

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

    UIButton *btnCall = (UIButton *) [cell viewWithTag:5]; 
    UIImageView *imgIcon = (UIImageView *) [cell viewWithTag:6]; 
    [btnCall setHidden:TRUE]; 


    if (... some condition ...) { 
     [btnCall setHidden:false]; 
     [imgIcon setImage:[UIImage imageNamed:@"icon1.png"]]; // load correctly 
    } else { 
     [btnCall setHidden:true]; // doesn't seem to work consistently. 
     [imgIcon setImage:[UIImage imageNamed:@"icon2.png"]]; // load correctly 
    } 

    return cell; 
} 

在我的故事板,在原型細胞此按鈕也設置爲隱藏。

我已經閱讀了很多關於這個問題的問題和解答。

我也有其他的東西,像每個單元格中的圖像。即使滾動向上/向下,圖像加載也是正確的。

有人可以幫忙嗎?非常感謝!

****編輯1 - 問題已解決! ****

我的細胞子類,以便我的重置可以更合適。下面是它是如何做(我替換上面的圖像,而不是按鈕 - 保持它作爲一個按鈕,應該只是罰款太):

@implementation PSCellMyTableViewCell 

    @synthesize imgView1 = _imgView1, imgView2 = _imgView2; 


    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
     self.imgView1.image = nil; 
     self.imgView2.image = nil; 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 

代碼的cellForRowAtIndexPath

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

    if (... some condition ...) { 
     [cell.imgView1 setImage:[UIImage imageNamed:@"icon-1.png"]];// no longer using tag 
     [cell.imgView2 setImage:[UIImage imageNamed:@"icon-12.png"]]; // no longer using tag 

    } else { 
     [cell.imgView1 setImage:[UIImage imageNamed:@"icon-2.png"]];// no tag 
     [cell.imgView2 setImage:[UIImage imageNamed:@"icon-22.png"]]; // no tag 
    } 

    return cell; 
} 

回答