具有错误的IndexPath影响的UITableViewCells和动画坐标有问题

时间:2013-12-27 07:26:15

标签: ios objective-c uitableview

更新了问题。

我的iOs 7应用程序中有一个有趣的效果。我正在使用故事板(单元格没有.xib文件)这是一个购物车。单击某个单元格内的“+”按钮,我应该隐藏标签文本,并在其上方显示带有UIPicker,标签和购物车按钮的UIView。有一些 SECTIONS (不是行)有1行(它是为了在单元格之间创建一些空间)。但如果我触摸第2部分的加号按钮,第7部分也会受到影响。反过来说。附上一些截图。

在触摸加号按钮之前

Before scrolling enter image description here

enter image description here enter image description here

代码

#pragma mark - Table view data source and delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [itemsTitles_ count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MenuItemCell";
    long row = indexPath.section;

    MenuItemCell *cell        = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.delegate             = self;
    cell.itemTitleLabel.text  = [itemsTitles_ objectAtIndex:row];
    cell.itemDescLabel.text   = [itemsDescriptions_ objectAtIndex:row];
    cell.itemCostLabel.text   = [NSString stringWithFormat:@"Цена: %@ рублей / шт", [itemsCosts_ objectAtIndex:row]];
    cell.itemImageIView.image = [UIImage imageNamed:[itemsImages_ objectAtIndex:row]];
    [cell.itemImageIView.layer setMasksToBounds:YES];
    [cell.itemImageIView.layer setCornerRadius:10.0f];

    [cell.layer setCornerRadius:10.0f];
    [cell.layer setMasksToBounds:YES];
    [cell.layer setBorderWidth:1.0f];
    //Some code for horizontal picker
    return cell;
}

接下来是我MenuItemCell班的委托方法。

- (void)plusButtonTappedOnCell:(id)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    MenuItemCell *cell =  (MenuItemCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    [cell.quantityPickerView setHidden:NO];
    [cell.itemDescLabel setHidden:YES];
    [cell.plusButton setHidden:YES];
}

如果我添加

,它会起作用
[cell.itemDescLabel setHidden:NO];
[cell.plusButton setHidden:NO];

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath。但是如果我添加购物车动画,问题又会出现。仅适用于1-4个部分。 我哪里错了?:)提前谢谢。

第一个问题解决了,thx。第二个问题来了。在第1个屏幕截图中,UIImageView有正常的动画方向(适用于1-4个部分)。但是当我向下滚动时 - 方向变成5-7节的直线。 (截图2)

enter image description here enter image description here

动画代码

UIImageView *imgV = cell.itemImageIView;
        CGRect rect = [imgV.superview convertRect:imgV.frame fromView:nil];
        rect = CGRectMake(5, (rect.origin.y*-1)-10, imgV.frame.size.width, imgV.frame.size.height);

        // create new duplicate image
        UIImageView *starView = [[UIImageView alloc] initWithImage:imgV.image];
        [starView setFrame:rect];
        starView.layer.cornerRadius=10.0f;
        [self.view addSubview:starView];

        // begin ---- apply position animation
        CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        pathAnimation.calculationMode = kCAAnimationPaced;
        pathAnimation.fillMode = kCAFillModeForwards;
        pathAnimation.removedOnCompletion = NO;
        pathAnimation.duration=0.65;
        pathAnimation.delegate=self;

        // tab-bar right side item frame-point = end point
        CGPoint endPoint = CGPointMake(210+rect.size.width/2, 390+rect.size.height/2);
        CGMutablePathRef curvedPath = CGPathCreateMutable();
        CGPathMoveToPoint(curvedPath, NULL, starView.frame.origin.x, starView.frame.origin.y);
        CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, starView.frame.origin.y, endPoint.x, starView.frame.origin.y, endPoint.x, endPoint.y);
        pathAnimation.path = curvedPath;
        CGPathRelease(curvedPath);
        // end ---- apply position animation

        // apply transform animation
        CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:@"transform"];
        [basic setToValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.25, 0.25, 0.25)]];
        [basic setAutoreverses:NO];
        [basic setDuration:0.65];

        [starView.layer addAnimation:pathAnimation forKey:@"curveAnimation"];
        [starView.layer addAnimation:basic forKey:@"transform"];

解决

终点坐标:

float differenceY = 390 - (cell.frame.size.height * (items_.count - indexPath.section));
        float endY = (cell.frame.origin.y + cell.frame.size.height/2 < 390) ? 390 : differenceY + cell.frame.origin.y + cell.frame.size.height/2;
        CGPoint endPoint = CGPointMake(210+rect.size.width/2, endY +rect.size.height/2);

起点坐标:

CGRect rect = [imgV.superview convertRect:imgV.frame fromView:nil];
        rect = CGRectMake(5, cell.frame.origin.y + rect.size.height/2, imgV.frame.size.width, imgV.frame.size.height);

2 个答案:

答案 0 :(得分:1)

我认为这是重用UITableViewCell的问题。 您应该知道已点击的项目,以便您可以切换显示/隐藏视图: cell.quantityPickerView cell.itemDescLabel cell.plusButton 在方法'tableview:cellForRowAtIndexPath:'

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

    static NSString *cellIdentifier = @"MenuItemCell";


    MenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //Uncomment if the cell is not declared in the storyboard
    //if (!cell) {
        //cell = [[MenuItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.delegate  = self;
        [cell.itemImageIView.layer setMasksToBounds:YES];
        [cell.itemImageIView.layer setCornerRadius:10.0f];
        [cell.layer setCornerRadius:10.0f];
        [cell.layer setMasksToBounds:YES];
        [cell.layer setBorderWidth:1.0f];
    //}

    MenuItem *menuItem = [self.items objectAtIndex:indexPath.row];

    cell.itemTitleLabel.text  = menuItem.title;
    cell.itemDescLabel.text   = menuItem.description;
    cell.itemCostLabel.text   = [NSString stringWithFormat:@"Цена: %@ рублей / шт", menuItem.cost];
    cell.itemImageIView.image = [UIImage imageNamed:menuItem.imageName];

    if (menuItem.isSelected) {
        cell.quantityPickerView.hidden = NO;
        cell.itemDescLabel.hidden = YES;
        cell.plusButton.hidden = YES;
    }
    else {
        cell.quantityPickerView.hidden = YES;
        cell.itemDescLabel.hidden = NO;
        cell.plusButton.hidden = NO;
    }

    return cell;
}

为方便起见,你应该有一个班级:

@interface MenuItem : NSObject

@property (readwrite, nonatomic) BOOL isSelected;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *description;
@property (strong, nonatomic) NSString *imageName;
@property (strong, nonatomic) NSString *cost;

@end

然后,在方法中:

- (void)plusButtonTappedOnCell:(id)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    MenuItemCell *cell =  (MenuItemCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    MenuItem *menuItem = [self.items objectAtIndex:indexPath.row];

    if (menuItem.isSelected) {
        cell.quantityPickerView.hidden = YES;
        cell.itemDescLabel.hidden = NO;
        cell.plusButton.hidden = NO;
        menuItem.isSelected = NO;
    } else {
        cell.quantityPickerView.hidden = NO;
        cell.itemDescLabel.hidden = YES;
        cell.plusButton.hidden = YES;
        menuItem.isSelected = YES;
    }

}

答案 1 :(得分:0)

由于您在代码中使用dequeueReusableCellWithIdentifier,因此可能会出现此问题。请在表cellForRowAtIndexPath中尝试以下代码。这将解决标签问题,消失...

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

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:MenuItemCell owner:self options:Nil];

        cell = [nib objectAtIndex:0];
    }

希望这对你有帮助..