2011-10-25 33 views
0

我每次出現該視圖時都會創建自定義單元格並更新一些UILabelUILabel值在cellForRowAtIndexPath中不更新?

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

     UILabel *lblFacingValue = nil; 
     static NSUInteger const kFacingLabelTag = 7; 

     CGFloat LeftLabelWidth = 130.0; 

     static NSString *CellIdentifier = @"Cell"; 
     ClientState *clientState = [ClientState sharedInstance]; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

     if(indexPath.row == 0) 
     { 
      lblFacingValue = [[[UILabel alloc] initWithFrame:CGRectMake(246, -11, LeftLabelWidth, (cell.contentView.frame.size.height))] autorelease]; 
      lblFacingValue.font = [UIFont systemFontOfSize:21];   
      lblFacingValue.textAlignment = UITextAlignmentLeft; 
      lblFacingValue.backgroundColor = [UIColor clearColor]; 
      lblFacingValue.tag = kFacingLabelTag;   
      lblFacingValue.textColor = [UIColor yellowColor]; 
      [cell.contentView addSubview:lblFacingValue]; 
     } 
    } 
    lblFacingValue.text = [NSString stringWithFormat:@"%@", clientState.Direction]; 
} 

但問題是它包含我第一次分配的值,並且在打開該視圖時不更新它。該視圖在標籤中。

它沒有更新lblFacingValue的價值,並跳過檢查

如果(細胞==零)

第一時間之後。但不要更新lblFacingValue。

+0

你是什麼意思,它包含我第一次分配的值,當我打開該視圖時不更新它。你如何更新細胞?編輯:好吧,明白了。 – akashivskyy

+0

你的意思是你有兩個標籤 - 其中一個應該更新,另一個不應該? – akashivskyy

回答

3

Azhar,我想你忘記在調用dequeueReusableCellWithIdentifier時不會返回nil時,將lblFacingValue變量設置爲實際的UILabel對象。您應該添加:

lblFacingValue = [cell viewWithTag:kFacingLabelTag]; 

調用

lblFacingValue.text = [NSString stringWithFormat:@"%@", clientState.Direction]; 

希望這有助於之前!

+0

+1 ..謝謝....阿爾默,它的工作 – Azhar

1

如果您將一個單元格出列,您會忘記一個標籤,您可以通過它標記標籤。

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

    UILabel *lblFacingValue = nil; 
    static NSUInteger const kFacingLabelTag = 7; 

    CGFloat LeftLabelWidth = 130.0; 

    static NSString *CellIdentifier = @"Cell"; 
    ClientState *clientState = [ClientState sharedInstance]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

     if(indexPath.row == 0) 
     { 
      lblFacingValue = [[[UILabel alloc] initWithFrame:CGRectMake(246, -11, LeftLabelWidth, (cell.contentView.frame.size.height))] autorelease]; 
      lblFacingValue.font = [UIFont systemFontOfSize:21];   
      lblFacingValue.textAlignment = UITextAlignmentLeft; 
      lblFacingValue.backgroundColor = [UIColor clearColor]; 
      lblFacingValue.tag = kFacingLabelTag;   
      lblFacingValue.textColor = [UIColor yellowColor]; 
      [cell.contentView addSubview:lblFacingValue]; 
     } 
    } else { 
     lblFacingValue = [cell.contentView viewWithTag:kFacingLabelTag]; 
    } 

    lblFacingValue.text = [NSString stringWithFormat:@"%@", clientState.Direction]; 
} 
0

的問題是,你專門配置電池具有相同的小區標識爲他人,

static NSString *CellIdentifier = @"Cell"; 

所以你的特殊細胞和正常細胞之間的出列單元格時,系統不區分。您應該爲特殊單元格創建不同的單元標識符,並在indexPath.row == 0;

+0

你確定嗎? – SleepsOnNewspapers

2

請記住一點,當單元格爲零時,您可以使用構造單元格。不要在那裏分配值。只需創建子視圖等。

此代碼應該修復它。另請參閱我的評論。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSUInteger const kFacingLabelTag = 7; //This should rather be a #define .. 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     if(indexPath.row == 0) 
     { 
      CGFloat LeftLabelWidth = 130.0; 
      UILabel *lblFacingValue = [[[UILabel alloc] initWithFrame:CGRectMake(246, -11, LeftLabelWidth, (cell.contentView.frame.size.height))] autorelease]; 
      lblFacingValue.font = [UIFont systemFontOfSize:21];   
      lblFacingValue.textAlignment = UITextAlignmentLeft; 
      lblFacingValue.backgroundColor = [UIColor clearColor]; 
      lblFacingValue.tag = kFacingLabelTag;   
      lblFacingValue.textColor = [UIColor yellowColor]; 
      [cell.contentView addSubview:lblFacingValue]; 
     } 
    } 

    //now, we might be reusing the cell, so in either case, pick the label 
    //and edit the value. 

    ClientState *clientState = [ClientState sharedInstance]; 
    UILabel *theLabel = (UILabel*) [cell.contentView viewForTag:kFacingLabelTag];  
    theLabel.text = [NSString stringWithFormat:@"%@", clientState.Direction]; 
}