2013-03-06 39 views
2

在我的程序中,我有一個數據庫,其中包含一個實體,其中包含書名,當前頁面和本書的全部頁面等幾個屬性。所以,我想填充tableview單元格的顏色取決於readed頁面。例如。如果我將這本書讀了一半,那麼這個單元格也會被填滿一半(curPage/totalPage * widthCell)。這是我的cellForRowAtIndexPath:方法:當我用顏色部分填充TableView單元格時,單元格中的文本出現問題

- (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; 
    } 

問題是,當我從滾動我畫的細胞的那部分視圖文本disapear。我該如何解決這個問題?

回答

1

您每次都添加視圖「v」。你應該在cell爲零時添加它。

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 
+0

哦,非常感謝。它工作很好!但在我的代碼中有一個小錯誤。所以,當我運行應用程序時,它會正確顯示和繪製單元格,單元格的寬度等於320.但是,如果我添加另一個單元格並返回到主視圖,則應用程序將單元格繪製爲300px。我設置accessoryType屬性= UITableViewCellAccessoryDisclosureIndicator,我不明白爲什麼我的應用程序這樣做... – BIB 2013-03-06 18:23:42

相關問題