2014-03-31 27 views
0

我使用的是AMRatingControl,我喜歡它,首先它看起來一切正常,但後來我發現它在TableView中遇到了一些麻煩。我加入控制單元與此代碼:iOS - UITableView爲單元格顯示錯誤的星號數

AMRatingControl *coloredRatingControl = [[AMRatingControl alloc] initWithLocation:CGPointMake(136,59) 
                      emptyColor:[UIColor whiteColor] 
                      solidColor:[UIColor whiteColor] 
                     andMaxRating:5]; 
    [coloredRatingControl setRating:(NSInteger)([place.Rating integerValue]/2.0)]; 
    [coloredRatingControl setEnabled:NO]; 
    //[coloredRatingControl setNeedsDisplay]; 
    //[coloredRatingControl setNeedsLayout]; 
    [cell addSubview:coloredRatingControl]; 

我有它的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    ..... 

最初的少數細胞也沒關係,顯示控制很好,但是當你爲以後的細胞,然後滾動它顯示錯誤的星星數量,但在適當的位置。在那裏是正確的數字。我設置了中斷點,代碼和值看起來很好。也許這是延遲加載和使用此控件的東西,但我不知道如何解決這個問題。我發現它取決於幾行第一行顯示的星星。有模式。在我的屏幕中,前4個單元格顯示並且有正確數量的星號(第5個單元格沒有顯示,但它也有正確的編號),然後這5個評級重複,並且第6個與第1個評分相同,但它是錯誤的,和第二個一樣)等等。我在cellForRowAtIndexPath中設置了其他的東西,比如圖片等,沒關係。這只是這些評級控制。我試圖在第一個示例代碼中強制使用註釋方法重繪,但它沒有幫助。

+0

請試試這個定義UITableViewCell *電池= [[UITableViewCell的頁頭] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]。單元格在上述情況下重用 – Rose

回答

2

試試這個,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    AMRatingControl *coloredRatingControl; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     coloredRatingControl = [[AMRatingControl alloc] initWithLocation:CGPointMake(136,59) 
                     emptyColor:[UIColor whiteColor] 
                     solidColor:[UIColor whiteColor] 
                    andMaxRating:5]; 
     [coloredRatingControl setEnabled:NO]; 
     [coloredRatingControl setTag:8888]; 
     [cell.contentView addSubview:coloredRatingControl]; 

    } 

    coloredRatingControl = (AMRatingControl *)[cell.contentView viewWithTag:8888]; 
    [coloredRatingControl setRating:(NSInteger)([place.Rating integerValue]/2.0)]; 
    .... 
+0

感謝您的幫助,但現在刪除的第二個答案是正確的。我正在嘗試您的解決方案,並沒有將控制權添加到單元格中。它沒有陷入條件。我猜是因爲我在Storyboard中定義了Cell。 –

+0

@LiborZapletal它沒有進入條件,因爲你正在使用故事板。你使用自定義的uitableview單元格嗎? – Akhilrajtr

相關問題