【问题标题】:Trouble with text in the cell when I partly fill the TableView cell with color当我用颜色部分填充 TableView 单元格时,单元格中的文本出现问题
【发布时间】:2013-02-21 07:00:24
【问题描述】:

在我的程序中,我有一个数据库,它由一个实体组成,该实体具有多个属性,例如书名、当前页数和书的总页数。所以,我想根据阅读的页面用颜色填充表格视图单元格。例如。如果我把书读了一半,单元格也会用一半的颜色填充(curPage/totalPage*widthCell)。这是我的cellForRowAtIndexPath: 方法:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
        UITableViewCell *result = nil;
        static NSString *BookTableViewCell = @"BookTableViewCell";
        result = [tableView dequeueReusableCellWithIdentifier:BookTableViewCell];
        if (result == nil){ 
            result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BookTableViewCell];
            result.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        Book *book = [self.booksFRC objectAtIndexPath:indexPath];
        float width = result.contentView.frame.size.width;
        double fill = ([book.page doubleValue]/[book.pageTotal doubleValue])*width;
        CGRect rv= CGRectMake(0, 0, fill, result.contentView.frame.size.height);
        UIView *v=[[UIView alloc] initWithFrame:rv];
        v.backgroundColor = [UIColor clearColor];
        v.backgroundColor = [UIColor yellowColor];
        [[result contentView] addSubview:v];
        result.textLabel.text = [book.name stringByAppendingFormat:@" %@", book.author];
        result.textLabel.backgroundColor = [UIColor clearColor];
        result.detailTextLabel.text =
        [NSString stringWithFormat:@"Page: %lu, Total page: %lu",(unsigned long)[book.page unsignedIntegerValue],(unsigned long)[book.pageTotal unsignedIntegerValue]];
        result.detailTextLabel.backgroundColor = [UIColor clearColor];
        result.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        result.textLabel.font = [UIFont systemFontOfSize:12];

        return result;
    }

问题是当我滚动视图文本从我绘制的单元格的那部分消失时。我该如何解决这个问题?

【问题讨论】:

    标签: ios text colors scroll tableviewcell


    【解决方案1】:

    您每次都在添加视图“v”。你应该在 cell 为 nil 时添加它。

    if (result == nil)
    {
        result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:BookTableViewCell];
        result.selectionStyle = UITableViewCellSelectionStyleNone;
    
        UIView *v=[[UIView alloc] init];
        v.tag = 1000;
        [[result contentView] addSubview:v];
        [v release];
    }
    
    UIView *v = [cell viewWithTag:1000];
    //Set framme and color here..
    //Do rest of the stuff
    

    【讨论】:

    • 哦,非常感谢。效果很好!但是在我的代码中有一个小错误。因此,当我运行应用程序时,它会正确显示和绘制单元格,并且单元格的宽度等于 320。但是,如果我添加另一个单元格并返回主视图,应用程序会将单元格绘制为 300 像素。我设置了附件类型属性 = UITableViewCellAccessoryDisclosureIndicator,但我不明白为什么我的应用程序如此...
    猜你喜欢
    • 2013-02-18
    • 2017-01-05
    • 1970-01-01
    • 2013-01-16
    • 2020-01-11
    • 2018-04-11
    • 2016-06-29
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多