OGeek|极客世界-中国程序员成长平台

标题: ios - 当我用颜色部分填充 TableView 单元格时,单元格中的文本出现问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:26
标题: ios - 当我用颜色部分填充 TableView 单元格时,单元格中的文本出现问题

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

    - (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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"age: %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;
    }

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



Best Answer-推荐答案


您每次都在添加 View “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

关于ios - 当我用颜色部分填充 TableView 单元格时,单元格中的文本出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15248160/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4