gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:41:16 26 4
gpt4 key购买 nike

我正在使用 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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com