为我的应用程序设计购物车

时间:2014-02-25 05:14:57

标签: ios objective-c ipad

I am developing a shopping application for iPad and I want to add a shopping cart into it just like this image.

我正在为iPad开发购物应用程序,我想在其中添加一个购物车,就像这个图像一样,我也想在角落的右侧添加数量(图中未显示)。

我已经完成了设计并加载了视图。但我不知道如何在每个视图中更改不同的图像,并在所有不同的标签中设置不同的文本。如何在单击取消按钮后删除特定视图?

CGFloat x = 0.0;
for (int k=0; k<=15; k++)
{

    NSArray *sub = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
    UIView *tmp = [sub objectAtIndex:0];

    tmp.frame = CGRectMake(x, 0, 60, 50);

    [_scrollView addSubview:tmp];


    x = x + 60;
}

4 个答案:

答案 0 :(得分:2)

//make custom circle UILable and put on Image. solve same issue




  UILabel *lbl_cart_count = [[UILabel alloc]initWithFrame:CGRectMake(302,6, 22, 22)];
    lbl_cart_count.textColor = [UIColor whiteColor];
    lbl_cart_count.textAlignment = NSTextAlignmentCenter;
    lbl_cart_count.text = [NSString stringWithFormat:@"%d",Temp_card_count];
    lbl_cart_count.layer.borderColor = [UIColor clearColor].CGColor;
    lbl_cart_count.layer.borderWidth = 0;
    lbl_cart_count.layer.cornerRadius = 11;
    lbl_cart_count.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:45.0/255.0 blue:143.0/255.0 alpha:1.0];
    lbl_cart_count.font = [UIFont fontWithName:@"ArialMT" size:11];
    [self.navigationController.navigationBar lbl_cart_count];
    [lbl_cart_count release];

  [view addSubview:lbl_cart_count];

答案 1 :(得分:1)

鉴于您有一个用户点击取消按钮的引用,并且取消按钮是视图的子视图,其中包含图像(可能是购物车项目)(如图所示)

在按钮点击事件上尝试以下操作:

[cartItemView removeFromSuperview];

//Your code to realign the items after the removed cart item should follow.

因此,如果用户删除了第二个项目(如图中突出显示的那样),则必须更新索引2,3,4等项目的X坐标(给定0是第一项) 。动画这将更好。

答案 2 :(得分:0)

根据您的要求,您似乎最好使用横向UITableView投放。您可以轻松地在其数据源中添加和删除对象,只需调用reloadData

答案 3 :(得分:0)

这对我有用

 CGFloat x = 0.0;
 NSMutableArray * cartArr = [CartDetails getAllProducts];
 for (int k=0; k< [cartArr count]; k++)
 {
   //Add custom view

    UIView * tmpV = [[UIView alloc]initWithFrame:CGRectMake(x, 0, 60, 50)];
    tmpV.backgroundColor = [UIColor clearColor];

    // Add imageview to custom view

    UIImageView *tmpImg = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 50, 40)];
    [tmpImg setImage:[UIImage imageNamed:@"cancel.jpeg"]];
    [tmpV addSubview:tmpImg];

    // Add label to custom view

    UILabel *tmplabel =[[UILabel alloc]initWithFrame:CGRectMake(5, 0, 15, 15)];
    tmplabel.textColor = [UIColor blackColor];
    tmplabel.text = [NSString stringWithFormat:@"%d",k];
    tmplabel.backgroundColor = [UIColor clearColor];
    [tmpV addSubview:tmplabel];

    // Add button to custom view

    UIButton * tmpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [tmpBtn addTarget:self action:@selector(clearCart:) forControlEvents:UIControlEventTouchUpInside];
    tmpBtn.frame = CGRectMake(45, 0, 15, 15);
    [tmpBtn setBackgroundImage:[UIImage imageNamed:@"CloseButton.png"] forState:UIControlStateNormal];
    [tmpV addSubview:tmpBtn];

   // Add customview to Scroollview

    [_scrollView addSubview:tmpV];

    x = x + 60;
}