0

我已经创建了一个包含 tableview 的应用程序,现在我正在设置该单元格的背景并为分隔符添加一个视图。它看起来不错,但是当 tebleview 滚动时,我的分隔符消失了。像这样,

第一次 在此处输入图像描述 当表格滚动时 在此处输入图像描述

这是我添加表格视图的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *SimpleTableIdentifier;
    UITableViewCell * cell;

    SimpleTableIdentifier = @"SimpleTableIdentifier";
    cell = [tableView  dequeueReusableCellWithIdentifier: nil];

    if(cell == nil) {

        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];

        // Configure the cell...
        UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        bgview.opaque = YES;
        bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
        [cell setBackgroundView:bgview];

        UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
        separatorLineView.backgroundColor = [UIColor colorWithRed:(74/255.0) green:(89/255.0) blue:(138/255.0) alpha:1];// you can also put image here
        [cell.contentView addSubview:separatorLineView];
    }
    return cell;
}
4

5 回答 5

1

采用

   cell = [tableView  dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

尝试

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *SimpleTableIdentifier;
    UITableViewCell * cell;

    SimpleTableIdentifier = @"SimpleTableIdentifier";
    cell = [tableView  dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

    if(cell == nil) {

        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];
        UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 42, 320, 1)];
        [separatorLineView setTag:1];
        [cell.contentView addSubview:separatorLineView];

        UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        bgview.opaque = YES;
        bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
        [cell setBackgroundView:bgview];



    }

    UIView *SPview=[cell viewWithTag:1];
    SPview.backgroundColor = [UIColor blueColor];// you can also put image here

    return cell;
}

问题是您为视图设置的框架我将分隔线原点从 44 更改为 42,现在它工作正常

于 2013-06-10T12:20:34.800 回答
0

将单元格的配置移到

if (cell == nil) {
    cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
}

// everything else

应该做的伎俩。

于 2013-06-10T12:21:52.707 回答
0

用它

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *SimpleTableIdentifier;
    UITableViewCell * cell;

    SimpleTableIdentifier = @"SimpleTableIdentifier";
    cell = [tableView  dequeueReusableCellWithIdentifier: nil];

    if(cell == nil) {

        cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:SimpleTableIdentifier];
    }

    // Configure the cell...
    UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    bgview.opaque = YES;
    bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
    [cell setBackgroundView:bgview];

    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
    separatorLineView.backgroundColor = [UIColor colorWithRed:(74/255.0) green:(89/255.0) blue:(138/255.0) alpha:1];// you can also put image here
    [cell.contentView addSubview:separatorLineView];

    return cell;
}
于 2013-06-10T12:22:46.800 回答
0

您的问题是每次获得新单元时都没有配置单元。您应该在每次cellForRowAtIndexPath调用时填充您的单元格。

尝试将您的代码更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *SimpleTableIdentifier;
    UITableViewCell * cell;

    SimpleTableIdentifier = @"SimpleTableIdentifier";
    cell = [tableView  dequeueReusableCellWithIdentifier: nil];

    if(cell == nil) {

        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];
    }

    // Configure the cell...
    UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    bgview.opaque = YES;
    bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
    [cell setBackgroundView:bgview];

    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
        separatorLineView.backgroundColor = [UIColor colorWithRed:(74/255.0) green:(89/255.0) blue:(138/255.0) alpha:1];// you can also put image here
        [cell.contentView addSubview:separatorLineView];

    return cell;
}
于 2013-06-10T12:23:09.857 回答
0

很好的答案在这里

首先通过 xib 文件添加分隔符,然后将此代码放在 cellForRowAtIndexPath 中。

tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)] autorelease];
于 2013-06-10T13:21:05.153 回答