2015-03-03 111 views
0

你好我有此观点的添加或删除车中的项目,我想节省的plist项目名称,说明选择的数据,并通过用户点击结账所有选定项目后,选择数量将显示在下一个屏幕上,我可以这样做吗?存储数据

的TableView委托方法: -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [dataArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomTableCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    Product *object = [dataArray objectAtIndex:indexPath.row]; 
    cell.nameLabel.text= object.iD; 
    cell.descLabel.text=object.desc; 
    NSString *str = [NSString stringWithFormat:@"%@%@",@"₹",object.price]; 
    cell.priceLabel.text = str; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.addBtn.tag = indexPath.row; 
    cell.removeBtn.tag = indexPath.row; 
    cell.count = [NSNumber numberWithInt:j]; 
    cell.quantityLbl.text = [NSString stringWithFormat:@"%@",cell.count]; 
    cell.quantityLbl.layer.cornerRadius =YES; 
    cell.quantityLbl.layer.borderWidth = 1.0; 
    return cell; 
} 

添加按钮

-(IBAction)addButton:(id)sender 
{ 
CustomTableCell *cell; 
UIButton *button3=(UIButton *)sender; 
Product *object = [dataArray objectAtIndex:button3.tag]; 
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) 
{ 
    cell = (CustomTableCell*)((UIButton*)sender).superview.superview.superview; 
} 
else{ 
    cell = (CustomTableCell*)((UIButton*)sender).superview.superview.superview.superview; 
} 
cell.quantityLbl.text = [NSString stringWithFormat:@"%d",[cell.quantityLbl.text intValue]+1]; 
i=i+1; 
indexLbl.text = [NSString stringWithFormat:@"%@",[NSNumber numberWithInt:i]]; 
} 

在删除按钮

-(IBAction)removeButton:(id)sender 
{ 
CustomTableCell *cell; 
// check device version 
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) 
{ 
    cell = (CustomTableCell*)((UIButton*)sender).superview.superview.superview; 
} 
else{ 
    cell = (CustomTableCell*)((UIButton*)sender).superview.superview.superview.superview; 
} 

if ([cell.quantityLbl.text intValue] > 0) 
{ 
    cell.quantityLbl.text = [NSString stringWithFormat:@"%d",[cell.quantityLbl.text intValue]-1]; 
    i=i-1; 
} 
indexLbl.text = [NSString stringWithFormat:@"%@",[NSNumber numberWithInt:i]]; 
} 

custom tablecell

回答

0

创建NSMutableDictionary命名为currentOrderDict并将其保存在您的班级中。

在addButton点击后,执行如下操作。

if([currentOrderDict objectForKey:cell.nameLabel.text]) 
{ 
// get the NSNumber for this key and ++ it. 
} 
else 
{ 
// this is the first time you are adding this item to this dictionary, so create an NSNumber and add it 
NSNumber oneNumber = [NSNumber numberWithInt:1]; 
[currentOrderDict setObject:oneNumber forKey:cell.nameLabel.text]; 
} 

做需要在删除按钮以及。

+0

我知道如何将数据存储在plist中实际上我想知道如何根据单元格的添加和删除按钮来保存数据 – RaviJSS 2015-03-03 11:02:06

+0

@RaviJSS编辑了答案。希望能帮助到你。 – 2015-03-03 11:09:21