0

这是我在“cellForRowAtIndexPath”中使用的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"OBBirthControlMethodsTableCell";
    OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    NSLog(@"cell id - %@",cell.subviews);
    CGRect frame = [tableView rectForRowAtIndexPath:0];
    if(nil == cell)
    {
        cell = [[[OBCustomDetailCell alloc]
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

        if (indexPath.row != 3)
        {
            //Setting the basic template
            UIView *template = [[UIView alloc] init];
            template.tag = indexPath.row+10;
            NSLog(@"path index = %d",indexPath.row);
            UIImageView *templateImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
                                                                                       200,
                                                                                       frame.size.height)];
            UILabel *templateLabel = [[UILabel alloc] initWithFrame:CGRectMake(templateImage.frame.size.width+20,
                                                                               0,
                                                                               cell.frame.size.width - templateImage.frame.size.width+20,
                                                                               frame.size.height)];
            [template addSubview:templateImage];
            [template addSubview:templateLabel];

            [cell.contentView addSubview:template];
        }
    }

    UIView *templateView = [cell.contentView viewWithTag:indexPath.row + 10];
    if (templateView)
    {
        NSLog(@"Gotten a templateView object");
        if (indexPath.row == 0)
        {
            templateView.frame = frame;

            for (UIView *view in templateView.subviews)
            {
                if ([view isKindOfClass:[UIImageView class]])
                {
                    [(UIImageView *)view setImage:[UIImage imageNamed:@"baby.jpeg"]];
                }
                else if  ([view isKindOfClass:[UILabel class]])
                {
                    [(UILabel *)view setText:@"This is not working"];
                }
            }
        }
        else
        {
            templateView.frame = CGRectMake(0, 50,
                                            frame.size.width,
                                            frame.size.height);

        }
    }
    return cell;
}

但问题是新单元格给了我与旧单元格相同的值,一旦你向下滚动,新单元格就会出队..

编辑 一旦我们向下滚动到具有与第一个单元格相同的值的新单元格,就会创建一个重复的单元格..

  • 我希望仅为 select rows() ..if (indexPath.row != 3) 创建 UIView
  • 我希望 UIView 的位置在某些行中有所不同.. if (indexPath.row == 0)
4

1 回答 1

1

这段代码有几个问题。首先,您的问题的主要原因是这一点:

template.tag = indexPath.row+10;

你为什么做这个?只需使用一个常量值,例如10. 不需要涉及索引路径,因为每个单元格都会改变。这将导致viewWithTag:重复使用的单元格失败,并将返回nil

其次,您不能只为 设置模板单元格indexPath.row != 3,因为在某些时候,非模板单元格可能会被重用。它将没有模板视图,因此以下布局将失败。您需要为这两种类型的单元格使用两个重用标识符。最终产品应如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *templateCellIdentifier = @"OBBirthControlMethodsTableCell";
    static NSString *otherCellIdentifier = @"OtherTableCell";

    if (indexPath.row != 3) {
        // Handle normal cells
        OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:templateCellIdentifier];
        if (cell == nil) {
            cell = [[[OBCustomDetailCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:templateCellIdentifier] autorelease];
            // Set up template cell
        }
        // Handle per-cell data
    } else {
        // Handle special cells
        OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:otherCellIdentifier];
        if (cell == nil) {
            cell = [[[OBCustomDetailCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:otherCellIdentifier] autorelease];
            // Set up other cell
        }
        // Handle per-cell data (not really necessary if there's only one of these)
    }
}
于 2012-07-16T06:35:11.580 回答