在iOS中使用评级星(DLStarRating)时,在滚动时冻结UITableView

时间:2014-06-12 13:40:56

标签: ios uitableview

我正在使用DLStarRating来显示评分。它工作正常但是当我在tableview单元格中使用它时,冷却问题会在滚动时开始。我想在UITableview中显示评分星数enter image description here Ex: - 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;

1 个答案:

答案 0 :(得分:0)

你做错了。

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

  1. 首先,您需要获取可重复使用的标识符单元格。
  2. 如果没有可重复使用的单元格,则在其中创建并添加所需的组件/子视图。
  3. 根据您的逻辑更改单元格及其组件/子视图的属性和行为。
  4. 您不需要每次都像创建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仍然冻结,则需要为该图像实现延迟加载。