2

我有一个屏幕,其中有一个 UIScrollView。UITableView 嵌入其中。我遇到的问题是细胞重用。我在这里附上一些屏幕截图以澄清问题。

滚动前:屏幕加载时。

在此处输入图像描述

滚动时:

在此处输入图像描述

然后向上滚动:

在此处输入图像描述

正如您所看到的,由于单元重用,LBM 现在显示在日期的位置。由于这是一个内置的组表单元格,我无法使用 prepForReuse。

我在这里附上了我的 cellForRowAtIndexPath 代码。

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell * cell = (UITableViewCell *)[tableView 
     dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
      reuseIdentifier:CellIdentifier] autorelease];
        // Here we are creating Custom Label to Display the TakenTime and TakenDate of Vitals
        // in center.
        if (indexPath.row==0) {
            lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 33)];
            lbl_title.textAlignment = UITextAlignmentCenter;
            lbl_title.font = [UIFont boldSystemFontOfSize:14];
            lbl_title.textColor = [UIColor whiteColor];
            lbl_title.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:lbl_title];
        }

    }

    switch (indexPath.row) {
        case 0:
            lbl_title.text = [NSString stringWithFormat:@"%@ %@",[dic_vitalsDictonary 
        valueForKey:@"taken_date"],[dic_vitalsDictonary valueForKey:@"taken_time"]];
            break;
        case 1:
            cell.textLabel.text = @"Height";
            NSString * str_height = [[dic_vitalsDictonary valueForKey:@"height"]  floatValue] 
            > 0 ? [dic_vitalsDictonary valueForKey:@"height"] : @"";
            cell.detailTextLabel.text = [self setDecimalFormatForString:str_height];
            break;
        case 2:
            cell.textLabel.text = @"Weight";
            NSString * str_weight = [[dic_vitalsDictonary valueForKey:@"weight"] floatValue] >
            0 ? [dic_vitalsDictonary valueForKey:@"weight"] : @"";
            cell.detailTextLabel.text = [self setDecimalFormatForString:str_weight];
            break;
        case 3:
            cell.textLabel.text = @"Temperature";
            NSString * str_temprature = [[dic_vitalsDictonary valueForKey:@"temp"] floatValue]
             > 0 ? [dic_vitalsDictonary valueForKey:@"temp"] : @"";
            cell.detailTextLabel.text = [self setDecimalFormatForString:str_temprature];
            break;
        case 4:
            cell.textLabel.text = @"Blood Pressure";
            NSString * str_lowbp = [dic_vitalsDictonary valueForKey:@"lowbp"];
            NSString * str_highbp = [dic_vitalsDictonary valueForKey:@"highbp"];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@/%@",[self
            setDecimalFormatForString:str_lowbp],[self setDecimalFormatForString:str_highbp]];
            break;
        case 5:
            cell.textLabel.text = @"Blood Sugar";
            NSString * str_bs1 = [dic_vitalsDictonary valueForKey:@"bs_1"];
            NSString * str_bs2 = [dic_vitalsDictonary valueForKey:@"bs_2"];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@/%@",[self 
            setDecimalFormatForString:str_bs1],[self setDecimalFormatForString:str_bs2]];
            break;
        case 6:
            cell.textLabel.text = @"Pulse";
            NSString * str_pulse = [[dic_vitalsDictonary valueForKey:@"pulse"] floatValue] > 0
            ? [dic_vitalsDictonary valueForKey:@"pulse"] : @"";
            cell.detailTextLabel.text = [self setDecimalFormatForString:str_pulse];
            break;
        case 7:
            cell.textLabel.text = @"Resp";
            NSString * str_resp = [[dic_vitalsDictonary valueForKey:@"resp"] floatValue] > 0 ?
            [dic_vitalsDictonary valueForKey:@"resp"] : @"";
            cell.detailTextLabel.text = [self setDecimalFormatForString:str_resp];
            break;
        case 8:
            cell.textLabel.text = @"Oxygen";
            NSString * str_oxyzen = [[dic_vitalsDictonary valueForKey:@"oxygen"] floatValue] >
            0 ? [dic_vitalsDictonary valueForKey:@"oxygen"] : @"";
            cell.detailTextLabel.text = [self setDecimalFormatForString:str_oxyzen];
            break;
        case 9:
            cell.textLabel.text = @"Fatmass";
            NSString * str_fatmass = [[dic_vitalsDictonary valueForKey:@"fatmass"] floatValue]
            > 0 ? [dic_vitalsDictonary valueForKey:@"fatmass"] : @"";
            cell.detailTextLabel.text = [self setDecimalFormatForString:str_fatmass];
            break;
        case 10:
            cell.textLabel.text = @"LBM";
            NSString * str_lbm = [[dic_vitalsDictonary valueForKey:@"lbm"] floatValue] > 0 ?
            [dic_vitalsDictonary valueForKey:@"lbm"] : @"";
            cell.detailTextLabel.text = [self setDecimalFormatForString:str_lbm];
            break;
        case 11:
            cell.textLabel.text = @"HC";
            NSString * str_hc = [[dic_vitalsDictonary valueForKey:@"hc"] floatValue] > 0 ? 
            [dic_vitalsDictonary valueForKey:@"hc"] : @"";
            cell.detailTextLabel.text = [self setDecimalFormatForString:str_hc];
            break;
        case 12:
            cell.textLabel.text = @"Peakflow";
            NSString * str_peakflow = [[dic_vitalsDictonary valueForKey:@"peakflow"] 
            floatValue] > 0 ? [dic_vitalsDictonary valueForKey:@"peakflow"] : @"";
            cell.detailTextLabel.text =[self setDecimalFormatForString:str_peakflow];
            break;
        default:
            break;
    }

    // Set the Background Image for TableviewCell.
    // if 1st row then we are showing different image to Display Vitals Date.

    if (indexPath.row==0) {

        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage 
        imageNamed:@"HeaderNavigation.png"]];
    }
    else{
        // Set the Background Image for TableviewCell.
        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage
         imageNamed:@"cell_background.png"]];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

请提出一些方法来克服这种情况。我已经尝试了很多,但似乎没有按照我想要的方式工作。我想防止单元格被重复使用,以便它在滚动过程中不会重复。

4

7 回答 7

0

尝试为每个单元格提供一个唯一标识符,

 NSString *CellIdentifier =[NSString stringWithFormat:"Cell %d",indexpath.row];

不知道这是否是正确的方法,但它应该可以解决你的问题。

于 2013-05-22T07:25:43.357 回答
0

尝试cell.textLabel.hidden = NO;在 switch 语句之前设置,然后在第一行隐藏它:

 case 0:
    cell.textLabel.hidden = YES; //this is added
    lbl_title.text = [NSString stringWithFormat:@"%@ %@",[dic_vitalsDictonary 
    valueForKey:@"taken_date"],[dic_vitalsDictonary valueForKey:@"taken_time"]];
    break;
于 2013-05-22T07:26:05.477 回答
0

两个更改:lbl_title在重用时添加标签并删除带有该标签的子视图,并在textLabel为第一行创建单元格时隐藏。或者,对第一行使用不同的单元格标识符,因为该单元格与其他单元格不同。

编辑:已经有几个例子可以说明如何做到这一点。

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

    static NSString *CellIdentifier = @"Cell";

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

    UILabel *lbl_title = (UILabel*)[cell.contentView viewWithTag: 'lblt'];
    lbl_title.hidden = YES; 

    cell.textLabel.hidden = NO;

    switch (indexPath.row) {
        case 0:
        // Here we are creating Custom Label to Display the TakenTime and TakenDate of Vitals
        // in center.
            if (lbl_title == nil)
            {
                lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 33)];
                lbl_title.tag = 'lblt';
                lbl_title.textAlignment = UITextAlignmentCenter;
                lbl_title.font = [UIFont boldSystemFontOfSize:14];
                lbl_title.textColor = [UIColor whiteColor];
                lbl_title.backgroundColor = [UIColor clearColor];
                [cell.contentView addSubview:lbl_title];
            }
            lbl_title.hidden = NO;
            cell.textLabel.hidden = YES;
            lbl_title.text = [NSString stringWithFormat:@"%@ %@",[dic_vitalsDictonary 
        valueForKey:@"taken_date"],[dic_vitalsDictonary valueForKey:@"taken_time"]];
            break;
        case 1:

或者,

只需更改[tableView dequeueReusableCellWithIdentifier:CellIdentifier];为,[tableView dequeueReusableCellWithIdentifier: (indexPath.row == 0 ? CellIdentifier2 : CellIdentifier)];因为第一行使用不同类型的单元格。

于 2013-05-22T07:29:20.320 回答
0

这是因为如果 cell==nil 条件,您正在配置您的标题标签。它不会每次都执行。所以使用下面的代码来满足您的要求。您不需要将标题标签声明为 globle。

    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                   reuseIdentifier:CellIdentifier] autorelease];
    // Here we are creating Custom Label to Display the TakenTime and TakenDate of Vitals
    // in center.
    UILabel *lbl_title= [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 33)];
    lbl_title.textAlignment = UITextAlignmentCenter;
    lbl_title.tag=21;
    lbl_title.font = [UIFont boldSystemFontOfSize:14];
    lbl_title.textColor = [UIColor whiteColor];
    lbl_title.backgroundColor = [UIColor clearColor];
    [cell addSubview:lbl_title];
}

if (indexPath.row==0) {
    UILabel *titleLabel=(UILabel*)[cell viewWithTag:21];
    titleLabel.text=@"May 1 ,2013 17:23:49";
}else{
    UILabel *titleLabel=(UILabel*)[cell viewWithTag:21];
    titleLabel.text=@"";
}
于 2013-05-22T07:33:48.217 回答
0

我认为你可以这样做:

 if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
  reuseIdentifier:CellIdentifier] autorelease];
    // Here we are creating Custom Label to Display the TakenTime and TakenDate of Vitals
    // in center.

        UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 33)];
        topLabel.textAlignment = UITextAlignmentCenter;
       topLabel.font = [UIFont boldSystemFontOfSize:14];
       topLabel.textColor = [UIColor whiteColor];
       topLabel.backgroundColor = [UIColor clearColor];
       topLabel.tag = 1;// unique for all subviews of cell
       [cell.contentView addSubview:topLabel];

 }
  topLabel = (UILabel *)[cell.contentView viewWithTag:1];
  topLabel.text = nil;
  cell.detailTextLabel.text = nil;
  cell.textLabel.text = nil;
  switch (indexPath.row) {
    case 0: 

        topLabel.text = [NSString stringWithFormat:@"%@ %@",[dic_vitalsDictonary 
    valueForKey:@"taken_date"],[dic_vitalsDictonary valueForKey:@"taken_time"]];
        break;
  //OTHER CODE
于 2013-05-22T07:34:55.670 回答
0

您必须在滚动时删除标签才能重复使用它。所以使用这些代码行:

 if (cell == nil)
{

    // your code for adding label and set tag as 99
}
else
{ 
     UILabel *label = (UILabel *)[cell.contentView viewWithTag:99];
     [label removeFromSuperview];
     label = nil;
} 

祝你好运 !!

于 2013-05-22T07:43:21.783 回答
-1

如果您不想重用单元格,请更新此代码

UITableViewCell * cell = (UITableViewCell *)[tableView 
 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
  reuseIdentifier:CellIdentifier] autorelease];
    // Here we are creating Custom Label to Display the TakenTime and TakenDate of Vitals
    // in center.
    if (indexPath.row==0) {
        lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 33)];
        lbl_title.textAlignment = UITextAlignmentCenter;
        lbl_title.font = [UIFont boldSystemFontOfSize:14];
        lbl_title.textColor = [UIColor whiteColor];
        lbl_title.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:lbl_title];
    }

}

有了这个

UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
  reuseIdentifier:CellIdentifier] autorelease];

    // Here we are creating Custom Label to Display the TakenTime and TakenDate of Vitals
    // in center.
    if (indexPath.row==0) {
        lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 33)];
        lbl_title.textAlignment = UITextAlignmentCenter;
        lbl_title.font = [UIFont boldSystemFontOfSize:14];
        lbl_title.textColor = [UIColor whiteColor];
        lbl_title.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:lbl_title];
    }
于 2013-05-22T07:25:44.160 回答