在我的程序中,我有一个数据库,它由一个具有多个属性的实体组成,例如书名,当前页面和书的总页数。所以,我想根据readed页面填充tableview单元格的颜色。例如。如果我把这本书读成一半,那么单元格也会被填充一半(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;
}
问题是当我从我绘制的单元格的那部分滚动视图文本时。我该如何解决这个问题?
答案 0 :(得分: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