繁体   English   中英

UITableViewCells具有错误的IndexPath影响和动画协调问题

[英]UITableViewCells with the wrong IndexPath affected and Animation coordinates trouble

更新的问题。

我在iOs 7应用程序中产生了有趣的效果。 我正在使用情节提要(单元中没有.xib文件)。这是购物车。 单击某个单元格中的“ +”按钮时,我应该隐藏标签文本,并显示带有UIPicker,标签和购物车按钮的UIView。 有一些带有1行的SECTION (不是行)(用于在单元格之间创建一些空间)。 但是,如果我触摸第2 部分的加号按钮, 第7部分也会受到影响。 相反。 附有一些截图。

触摸加号按钮之前

滚动之前在此处输入图片说明

在此处输入图片说明在此处输入图片说明

编码

#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个部分。 我哪里错了::)在此先感谢。

添加

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

在此处输入图片说明在此处输入图片说明

动画代码

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);

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

- (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;
    }

}

由于您在代码中使用了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];
    }

希望这对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM