首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TableviewCell意外更改

TableviewCell意外更改
EN

Stack Overflow用户
提问于 2013-06-10 20:18:54
回答 5查看 107关注 0票数 0

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

第一次

当表格滚动时

这是我添加tableview的代码

代码语言:javascript
复制
- (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;
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-06-10 20:20:35

使用

代码语言:javascript
复制
   cell = [tableView  dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

试一试

代码语言:javascript
复制
- (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帧,现在它可以正常工作

票数 1
EN

Stack Overflow用户

发布于 2013-06-10 20:21:53

将单元的配置移到之外

代码语言:javascript
复制
if (cell == nil) {
    cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
}

// everything else

应该能行得通。

票数 0
EN

Stack Overflow用户

发布于 2013-06-10 20:22:47

使用它

代码语言:javascript
复制
- (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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17023700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档