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

标签 ios objective-c uitableview

更新问题。

我在我的 iOs 7 应用程序中有一个有趣的效果。我正在使用 Storyboard(单元格没有 .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 类的委托(delegate)方法。

- (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 个部分的直线。 (截图二)

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

最佳答案

我认为这是重用 UITableViewCell 的问题。 您应该知道哪些项目已被点击,以便您可以切换显示/隐藏 View :cell.quantityPickerViewcell.itemDescLabelcell.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;
    }

}

关于ios - 具有错误的 IndexPath 影响的 UITableViewCells 和动画坐标问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20796648/

相关文章:

ios - 如何用换行符拆分字符串

iphone - 重新加载数据时 TableView Controller 不显示事件状态

ios - 如何管理不同时区的 NSDate

objective-c - 在 cocoa/objective-c 中复制 NSView

iphone - 解析 RSS 提要困难

objective-c - RGB 到 HSL 的转换似乎在数值上还可以,但在视觉上却不是这样

objective-c - 如何在 Objective C 或 Swift 中重写和测试依赖方法

ios - UINavigationController 代码推送到 UIViewController 无法正常工作

iphone - UITableView 在 TableView 中间加载单元格。为什么?

ios - 运行时自动布局的 UIButton 大小