ios - 在 iOS 中使用评级星 (DLStarRating) 时在滚动时卡住 UITableView

标签 ios uitableview

我正在使用 DLStarRating用于显示评级。它工作完美,但是当我在 tableview 单元格中使用它时,滚动时卡住问题开始。我想在 UITableview 中显示评级星星分数enter image description here例如:- 3.2 ,1.9,2,4.6 等评级。

UITableViewCell *cell=nil;
if(cell==nil)
{

[tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"CellID"];
cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
cell.selectionStyle = UITableViewCellEditingStyleNone;
}
   UILabel *LabelTitle= (UILabel*)[cell viewWithTag:1];
LabelTitle.text=[[ArrayData objectAtIndex:indexPath.row] objectForKey:@"name"];


UILabel *LabelAddress= (UILabel*)[cell viewWithTag:100];
LabelAddress.text=[[ArrayData objectAtIndex:indexPath.row] objectForKey:@"address"];

UIImageView *imgView = (UIImageView*)[cell viewWithTag:200];
[imgView setImageWithURL:[NSURL URLWithString:[[ArrayData objectAtIndex:indexPath.row] objectForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"loading.png"]];

DLStarRatingControl *customNumberOfStars = [[DLStarRatingControl alloc] initWithFrame:CGRectMake(20, 140, 140, 30) andStars:5 isFractional:YES];
customNumberOfStars.delegate = self;
customNumberOfStars.backgroundColor = [UIColor groupTableViewBackgroundColor];
customNumberOfStars.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
customNumberOfStars.rating = [[ArrayData objectAtIndex:indexPath.row] floatValue];
[cell addSubview:customNumberOfStars];
    return cell;

最佳答案

你这样做是不对的。

您应该按照以下步骤操作。

  • 首先,您需要为标识符获取可重复使用的单元格。
  • 如果没有可重用的单元格可用,则在其中创建并添加所需的组件/ subview 。
  • 根据您的逻辑更改单元格及其组件/ subview 的属性和行为。

  • 您不需要像为 DLStarRatingControl 所做的那样每次都创建 subview 。这会影响 tableview 的性能并且会卡住。

    通过执行这些步骤,我将更改您的实现,如下所示
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString * CellIdentifier = @"CellID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil)
        {
            cell = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:nil options:nil][0];
            cell.selectionStyle = UITableViewCellEditingStyleNone;
    
            DLStarRatingControl *customNumberOfStars = [[DLStarRatingControl alloc] initWithFrame:CGRectMake(20, 140, 140, 30) andStars:5 isFractional:YES];
            customNumberOfStars.tag = 1001;
            customNumberOfStars.delegate = self;
            customNumberOfStars.backgroundColor = [UIColor groupTableViewBackgroundColor];
            customNumberOfStars.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
            [cell addSubview:customNumberOfStars];
        }
        UILabel *LabelTitle= (UILabel*)[cell viewWithTag:1];
        LabelTitle.text=[[ArrayData objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    
        UILabel *LabelAddress= (UILabel*)[cell viewWithTag:100];
        LabelAddress.text=[[ArrayData objectAtIndex:indexPath.row] objectForKey:@"address"];
    
        UIImageView *imgView = (UIImageView*)[cell viewWithTag:200];
        [imgView setImageWithURL:[NSURL URLWithString:[[ArrayData objectAtIndex:indexPath.row] objectForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"loading.png"]];
    
        DLStarRatingControl *customNumberOfStars = (customNumberOfStars *)[cell viewWithTag:1001];
        customNumberOfStars.rating = [[ArrayData objectAtIndex:indexPath.row] floatValue];
    
        return cell;
    }
    

    注意没有用户[UITableView registerNib: forCellReuseIdentifier:]因为您需要将评级控件添加为 subview 。您每次都添加它cellForRowAtIndexPath方法调用,现在它只会在创建新单元格时添加。

    我还怀疑表格 View 卡住,因为您正在从某个 URL 加载图像。我不确定该图像的大小,但如果 tableView 仍然卡住,您需要为该图像实现延迟加载。

    关于ios - 在 iOS 中使用评级星 (DLStarRating) 时在滚动时卡住 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24185968/

    相关文章:

    php - JSON从mysql数据库获取和推送

    ios - 自定义表格 View 的 ScrollView

    iphone - 在两个标签之间保持 map 同步

    ios - 以 Core Data 作为数据源的 QLPreviewController

    ios - 设备旋转时 UITableViewHeader 不保持位置

    swift - awakeFromNimb 不会在 TableView reloadData Swift 上被调用

    iOS Swift - 带有左侧细节和右侧细节和副标题的 UITableViewCell?

    ios - 向选定的表格单元格添加复选标记还会检查另一个表格单元格

    ios - UIDocumentInteractionController 导航栏颜色

    objective-c - 设计数据加载和对象创建的正确方法