UILabel值不会在cellForRowAtIndexPath中更新?

时间:2011-10-25 12:46:58

标签: iphone objective-c ios uitableview

每次出现该视图时,我都会创建自定义单元格并更新一些UILabel

- (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 的值并跳过检查

  

if(cell == nil)

第一次之后。但不要更新lblFacingValue。

4 个答案:

答案 0 :(得分:3)

Azhar,我认为当对dequeueReusableCellWithIdentifier的调用没有返回nil时,你忘了将lblFacingValue变量设置为实际的UILabel对象。你应该添加:

lblFacingValue = [cell viewWithTag:kFacingLabelTag];

之前打电话

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

希望这有帮助!

答案 1 :(得分: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];
}

答案 2 :(得分: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];
}

答案 3 :(得分:0)

问题是您的特殊配置的单元格与其他单元格具有相同的单元格标识符,

static NSString *CellIdentifier = @"Cell";

因此,当您的特殊细胞和正常细胞之间出现细胞时,系统无法区分。您应该为特殊单元格创建不同的单元格标识符,并在indexPath.row == 0时使用它进行出列;