2

在我的程序中,我有一个数据库,它由一个实体组成,该实体具有多个属性,例如书名、当前页和书的总页数。所以,我想根据阅读的页面用颜色填充表格视图单元格。例如,如果我将书读了一半,则单元格也会用一半的颜色填充(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;
    }

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

4

1 回答 1

1

您每次都添加视图“v”。当单元格为零时,您应该添加它。

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
于 2013-03-06T13:20:06.530 回答