【发布时间】:2023-04-03 19:46:01
【问题描述】:
我想知道当用户更新自定义uitableviewcell 中的文本字段时,是否有办法更新barcodeItemsQuantity 数组。下面是我的代码 sn-ps。并且我想在用户从自定义 tableviewcell 更改文本字段时更新我的数组中的数据。
viewcontroller.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Cell Initialized");
static NSString *cellIdentifier = @"BarcodeItemsCell";
BarcodeItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil) {
cell = [[BarcodeItemsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell...
if (indexPath.row == [barcodeItems count]) {
// Add new row
cell.barcodeLabel.text = @"scan SKU";
cell.barcodeLabel.textColor = [UIColor lightGrayColor];
UIImage *btnImage = [UIImage imageNamed:@"barcodeIcon"];
[cell.leftButton setImage:btnImage forState:UIControlStateNormal];
cell.leftButton.tintColor = [UIColor blackColor];
cell.quantityTextField.userInteractionEnabled = NO;
[cell.leftButton addTarget:self action:@selector(scanBarcode) forControlEvents:UIControlEventTouchUpInside];
NSLog(@"Add another Item Requested");
}
else {
// Display barcode items
cell.barcodeLabel.text = [barcodeItems objectAtIndex:indexPath.row];
UIImage *btnImage = [UIImage imageNamed:@"deleteIcon"];
cell.leftButton.tintColor = [UIColor redColor];
[cell.leftButton setImage:btnImage forState:UIControlStateNormal];
cell.leftButton.tag = indexPath.row;
[cell.leftButton addTarget:self action:@selector(deleteRow:) forControlEvents:UIControlEventTouchUpInside];
[barcodeItemsQuantity addObject:cell.quantityTextField];
}
NSLog(@"Cell Populated");
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return true;
}
-(void) deleteRow:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:photoCaptureView.itemsTableView];
NSIndexPath *indexPath = [photoCaptureView.itemsTableView indexPathForRowAtPoint:buttonPosition];
[barcodeItems removeObjectAtIndex:indexPath.row];
[photoCaptureView.itemsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
NSLog(@"Item Removed");
}
barcodeitestableviewcell.h
@interface BarcodeItemsTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIButton *leftButton;
@property (strong, nonatomic) IBOutlet UILabel *barcodeLabel;
@property (strong, nonatomic) IBOutlet UITextField *quantityTextField;
@end
【问题讨论】:
标签: ios objective-c uitableview