我有一个UITableview
,我使用来自api的数据填充行。
如果将超过相同项目添加到tableview中,我已经创建了代码来更新单元格行中项目的标签。如果添加了新项目,则会使用该信息填充新的单元格行。
我现在正在尝试使用UITableview
编辑方法从UITableview
删除行。
我删除行没有问题,但我的问题是如果单元格行中的项目大于1,如果quantuty只有一个删除单元格行,如何删除数量。
尝试这样做时,我遇到了一个奇怪的错误。
我的代码很多次,请查看我的代码和截图。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"Order ID: %ld", (long)self.orderId);
if ([self.prevVC isKindOfClass:[MyOrdersViewController class]]) {
NSLog(@"from my orders view");
[self.tableView reloadData];
}else {
NSLog(@"from my deli item view %@", self.deli.deliId);
NSLog(@"from my menu item view %ld", (long)self.menuItem.menuItemId);
NSLog(@"menu item array %@", self.menuItem);
self.viewOrderSummaryDictionary = [[NSMutableDictionary alloc] init];
[self.viewOrderSummaryDictionary setObject: [NSNumber numberWithInteger: self.menuItem.menuItemId] forKey: @"id"];
[self.viewOrderSummaryDictionary setObject: self.menuItem.menuItemDescription forKey: @"description"];
[self.viewOrderSummaryDictionary setObject: self.menuItem.menuItemImageURL forKey: @"imageUrlString"];
[self.viewOrderSummaryDictionary setObject: self.menuItem.menuItemPrice forKey: @"price"];
[self.viewOrderSummaryDictionary setObject: self.menuItem.menuItemCurrency forKey: @"currency"];
NSArray *arr = [self.newestViewOrderSummaryArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"id == %@", [NSNumber numberWithInteger: self.menuItem.menuItemId]]];
NSLog(@"arr arr arr %@", arr);
if (arr && arr.count == 1) {
NSMutableDictionary* foundMenuItem = (NSMutableDictionary*) [arr objectAtIndex: 0];
self.quantity = [[foundMenuItem objectForKey: @"quantity"] intValue];
self.quantity++;
[foundMenuItem setObject: [NSString stringWithFormat: @"%d", self.quantity] forKey: @"quantity"];
[self.newestViewOrderSummaryArray removeObject: foundMenuItem];
self.viewOrderSummaryArray = [[NSMutableArray alloc] initWithArray: self.newestViewOrderSummaryArray];
[self.viewOrderSummaryArray addObject: foundMenuItem];
}
else {
[self.viewOrderSummaryDictionary setObject: @"1" forKey: @"quantity"];
[self.viewOrderSummaryArray addObject: self.viewOrderSummaryDictionary];
[self.viewOrderSummaryArray addObjectsFromArray: self.newestViewOrderSummaryArray];
}
[self.tableView reloadData];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self showBackButton];
self.navBarBackLabel = [[UILabel alloc] initWithFrame: CGRectMake(65, 5, 200, 30)];
self.title = NSLocalizedString(@"View Order Summary", nil);
self.navBarBackLabel.textColor = [UIColor appRedColor];
self.navBarBackLabel.font = [UIFont fontWithName:@"SegoeWP" size:15];
[self.navigationController.navigationBar addSubview:self.navBarBackLabel];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)];
self.tableView.contentInset = UIEdgeInsetsZero;
self.viewOrderSummaryArray = [[NSMutableArray alloc] init];
[self setupTableViewHeader];
}
#pragma mark - UITableViewDataSource
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0f;
}
- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGRect frame = tableView.frame;
self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.headerView.backgroundColor = [UIColor appOrderScreenHeaderBackgroundColor];
self.headerViewWhite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30.0f)];
self.headerViewWhite.backgroundColor = [UIColor appYellowColor];
[self.headerViewWhite addSubview:self.deliNameLabel];
[self.headerView addSubview:self.headerViewWhite];
return self.headerView;
}
- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{
return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 115;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.viewOrderSummaryArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewOrderSummaryCell *cell = (ViewOrderSummaryCell *)[tableView dequeueReusableCellWithIdentifier:@"ViewOrderSummaryCell"];
cell.separatorInset = UIEdgeInsetsZero;
self.tableView.separatorInset = UIEdgeInsetsZero;
self.edgesForExtendedLayout = UIRectEdgeNone;
//self.tableView.layoutMargins = UIEdgeInsetsZero;
//cell.layoutMargins = UIEdgeInsetsZero;
NSDictionary* currentItem = [self.viewOrderSummaryArray objectAtIndex: indexPath.row];
cell.productNameLabel.text = [currentItem objectForKey: @"description"];
cell.productPriceLabel.text = [NSString stringWithFormat: @"%@%@ x %@",[currentItem objectForKey: @"currency"],[currentItem objectForKey: @"price"], [currentItem objectForKey: @"quantity"]]; // TODO: Must implement this later
[cell.productImageView sd_setImageWithURL:[currentItem objectForKey: @"imageUrlString"] placeholderImage:[UIImage imageNamed:@"appPlaceholderImage"]];
CALayer * l = [cell.productImageView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
float sumq = 0;
NSArray *array = [self.viewOrderSummaryArray valueForKey:@"price"];
for (NSNumber *num in array)
{
sumq += [num floatValue];
}
float currentPrice = [[currentItem objectForKey: @"price"] floatValue];
float quantity = [[currentItem objectForKey: @"quantity"] intValue];
sumPrice += (currentPrice * quantity);
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
NSLog(@"Selected row of section >> %ld",(long)indexPath.row);
NSLog(@"Selected row is %@", [self.viewOrderSummaryArray objectAtIndex:[indexPath row]] );
NSMutableDictionary* currentItem = [self.viewOrderSummaryArray objectAtIndex: indexPath.row];
if ([[currentItem objectForKey: @"quantity"] isEqualToString:@"1"]) {
[self.viewOrderSummaryArray removeObject:[self.viewOrderSummaryArray objectAtIndex:[indexPath row]]];
NSLog(@"Quantity label");
}
else {
self.quantity--;
[currentItem setObject: [NSString stringWithFormat: @"%d", self.quantity] forKey: @"quantity"];
[self.newestViewOrderSummaryArray removeObject: currentItem];
self.viewOrderSummaryArray = [[NSMutableArray alloc] initWithArray: self.newestViewOrderSummaryArray];
[self.viewOrderSummaryArray addObject: currentItem];
NSLog(@"Many label");
}
[self.tableView reloadData];
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}