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

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

最佳答案

试试这个,

- (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)];
    ....

关于iOS - UITableView 显示错误的单元格星数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22758504/

相关文章:

ios - Netflix 使用什么 NSExtensionActivationRule key ?

ios - UIButton 图像只改变一个按钮

ios - Amazon AWSS3TransferManager 在 ios 中无法上传多部分数据

ios - Swift 5 - 让 tableview 等到来自 api 调用的数据返回(使用多个 tableviews)

iphone - 为此方法自定义 UITableViewCell 的手动布局?

macos - 容器 "…"必须只包含一个证书及其私钥

ios - 从NSMutableArray提取特定数据的更快方法?

ios - UIImageview 在获取图像后不会在静态 TableView 单元格中保留其圆形

ios - CJSON反序列化错误

objective-c - cocoa 中的单例,他们是邪恶的吗?