iOS - UITableView显示单元格的星数错误

时间:2014-03-31 09:50:21

标签: ios objective-c uitableview lazy-loading

我正在使用AMRatingControl,我喜欢它,首先它看起来一切正常,但后来我发现它在TableView中有些麻烦。我正在使用以下代码向单元格添加控件:

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个相同,但是它是错误的,同样适用于第7个(与第2个相同)等等。我在cellForRowAtIndexPath中设置了像图像一样的其他东西,所以没关系。这只是这些评级控制。我尝试在第一个示例代码中强制使用注释方法重绘,但它没有帮助。

1 个答案:

答案 0 :(得分: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)];
    ....