UITableView滚动变得不稳定

时间:2014-07-10 14:32:25

标签: ios objective-c uitableview

我有一个启用了分页的UITableView。起初我可以在单元格之间非常平滑地滚动。然而,在来回滚动几次之后,它变得相当不稳定。我没有正确地创建/重复使用我的细胞吗?

-  (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cash-Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
    Purchase *purch = (Purchase *)self.purchases[indexPath.row];
    [image setImage:[UIImage imageWithData:purch.image]];
    image.contentMode = UIViewContentModeCenter;
    image.contentMode = UIViewContentModeScaleAspectFill;
    image.clipsToBounds = YES;
    [self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    [cell addSubview:image];

    UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
    [priceLabel setText:@"$10.0"];
    [priceLabel setTextAlignment:NSTextAlignmentCenter];
    [priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
    [self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
    [price addSubview:priceLabel];
    [price setBackgroundColor:[UIColor whiteColor]];

    [cell addSubview:price];
    return cell;
}

3 个答案:

答案 0 :(得分:0)

这是因为每次单元格进入视图时都会调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath。将图像添加到数组(在viewDidLoad或某处),您将获得更平滑的结果。

答案 1 :(得分:0)

如果您想重复使用它们,请确保只将内容添加到UITableViewCell一次,然后重新使用它(只需更新图片和价格)即可。

否则,您实施的内容是每次调用(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath时添加子视图价格和图片。如果我是对的,只需尝试调试您的单元格要查看的子视图的数量。

你可以这样:

if ( ! [cell viewWithTag: 1])
{
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
    image.tag = 1;

    image.contentMode = UIViewContentModeCenter;
    image.contentMode = UIViewContentModeScaleAspectFill;
    image.clipsToBounds = YES;
    [self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    [cell addSubview:image];
}

if ( ! [cell viewWithTag: 2])
{
    UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
    priceLabel.tag = 2;

    [priceLabel setTextAlignment:NSTextAlignmentCenter];
    [priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
    [self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
    [price addSubview:priceLabel];
    [price setBackgroundColor:[UIColor whiteColor]];

    [cell addSubview:price];
}
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
UIImage *image = [cell viewWithTag: 1];
[image setImage:[UIImage imageWithData:purch.image]];

UILabel *priceLabel = [cell viewWithTag: 2];
[priceLabel setText:@"$10.0"];

答案 2 :(得分:0)

您没有重复使用单元格,这会导致延迟/不连续滚动。

建议:

 -  (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   static NSString *CellIdentifier = @"Cash-Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

   if(cell == nil){

      UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
      Purchase *purch = (Purchase *)self.purchases[indexPath.row];
      [image setImage:[UIImage imageWithData:purch.image]];
      image.contentMode = UIViewContentModeCenter;
      image.contentMode = UIViewContentModeScaleAspectFill;
      image.clipsToBounds = YES;
      image.tag = 10;
     [self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
     [cell.contentView addSubview:image];

     UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
     UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
    [priceLabel setText:@"$10.0"];
    [priceLabel setTextAlignment:NSTextAlignmentCenter];
    [priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
    [self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
    [price addSubview:priceLabel];
    [price setBackgroundColor:[UIColor whiteColor]];
    price.tag = 11;
    [cell.contentView addSubview:price];


 }

//update the value here

  UIImageView *imgV = (UIImageView *)[self.contentView viewWithTag:10];
  // update the imageView here
  //imgV.image = image

  UIView *vv = (UIView *)[self.contentView viewWithTag:11];
  // Update the UIView here
  //vv.text = @"text here";


 //Put setselection style here to allow the cell to be loaded before setting the property value
  cell.selectionStyle = UITableViewCellSelectionStyleNone;

  return cell;
}