我正在使用DLStarRating来显示评分。它工作正常,但是当我在tableview单元格中使用它时,然后在滚动时开始冻结问题。我想在UITableview中显示评分星号,例如: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;

最佳答案

你这样做是不对的。

您应该遵循以下步骤。

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

  • 您不需要像对DLStarRatingControl那样每次都创建子视图。这将影响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:],因为您需要将评分控件添加为子视图。您每次调用cellForRowAtIndexPath方法时都在添加它,现在仅在创建新单元格时才添加它。

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

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

    10-13 04:56