2011-04-29 90 views
0

我有這樣的代碼,其中「lineaRedToday」是的UIImageView:的iOS的tableview細胞處理

- (void)viewDidLoad { 

[super viewDidLoad]; 

lineaRedToday = [UIImage imageNamed:@"line4.png"];} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


static NSString *MyIdentifier = @"MyIdentifier"; 
MyIdentifier = @"tblCellView"; 

TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if(cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil]; 
    cell = tblCell; 
} 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

[[lineDay objectAtIndex:currentTodaylineRed -1] setImage:lineaRedToday]; 


return cell; 



- (IBAction) change{ 

    lineaRedToday = [UIImage imageNamed:@"linea.png"]; 
    [lineDay setImage:lineaRedToday]; 
    [myTableView reloadData]; 
} 

它與15個自定義的UITableViewCell一個UITableView,我想在一個的UIImageView改變形象,這ImageView的是「 lineDay「中,lineDay存在於所有單元格中,我想在所有單元格中更改其圖像,但IBAction」change「僅在最後一個單元格中更改UIImageView,而不是全部....爲什麼?

回答

0

是的,它會改變最後一個單元格中的圖像,因爲它只有最後一個單元格的引用,如果要這樣做,那麼當您在cellForRowAtIndexPath中創建單元格時,請檢查BOOL並根據該BOOL設置圖像。同樣在IBAction中,更改BOOL值並在表視圖上調用reloadData以重新加載它。作爲 -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier     = @"Test"; 

     UITableViewCell *cellView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cellView == nil) 
     { 
      cellView = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

      UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 85.0f)]; 
      [bgImgView setTag:1]; 
      [bgImgView setBackgroundColor:[UIColor clearColor]]; 
      [cellView.contentView addSubview:bgImgView]; 
      [bgImgView release]; 
     } 

     UIImageView *imgView = (UIImageView*)[cellView viewWithTag:1]; 

     if (buttonPressed) 
     { 
      [imgView setImage:[UIImage imageNamed:@"listing_bg"]]; 
     } 
     else 
     { 
      [imgView setImage:[UIImage imageNamed:@"featured_listing_bg"]]; 
     } 
     return cellView; 
    } 


- (IBAction) change{ 
    buttonPressed = !buttonPressed; 
    [myTableView reloadData]; 
} 
+0

我嘗試使用我在IBAction爲改變一個布爾值,但它改變,但只有最後一個單元 – CrazyDev 2011-04-29 08:33:32

+0

我已經更新了我的答案 – saadnib 2011-04-29 08:56:50

0

首先,lineaRedToday是UIImage,而不是一個UIImageView。第一個是圖像,第二個是視圖層次結構中的對象。

然後,在你的代碼

if(cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil]; 
    cell = tblCell; 
} 

您使用tblCell。我看不到這是什麼。您應該從剛剛從筆尖加載的內容中指定一些內容。

我不知道lineDay到底是什麼,但單一的視圖(即UIImageView)只能出現在一個細胞,而不是很多。如果您更改圖像(lineaRedToday),則仍需在視圖中設置此新圖像。應該有類似

cell.yourImageView.image = linaRedToday; 

配置單元時。