Iphone 如何对用作计算器的表视图的可重用自定义单元格的内容求和
我构建了一个带有自定义表视图单元格的表视图,其中每个单元格都有几个UITextFields,允许用户输入提供计算的数字。该单元格有一个提供该单元格结果的UITextField小计 我的问题是,我想对该单元格以及用户使用的每个附加单元格进行小计,并在不同的UIView上提供总计 我不知道如何获取每个单元格的每个小计的值,而且,我如何知道用户使用了多少单元格,以及所有这些,以便在不同的视图中为用户提供总计 实现这一目标的最佳方式是什么 这是代码 我想添加新的单元格,并能够将它们添加到计算中。但每个单元格将保存用户提供的不同值 最好的Iphone 如何对用作计算器的表视图的可重用自定义单元格的内容求和,iphone,xcode,uitableview,Iphone,Xcode,Uitableview,我构建了一个带有自定义表视图单元格的表视图,其中每个单元格都有几个UITextFields,允许用户输入提供计算的数字。该单元格有一个提供该单元格结果的UITextField小计 我的问题是,我想对该单元格以及用户使用的每个附加单元格进行小计,并在不同的UIView上提供总计 我不知道如何获取每个单元格的每个小计的值,而且,我如何知道用户使用了多少单元格,以及所有这些,以便在不同的视图中为用户提供总计 实现这一目标的最佳方式是什么 这是代码 我想添加新的单元格,并能够将它们添加到计算中。但每个单
#导入
@接口处理器:UIViewController{
UITextField*totalField;
UITextField*差异字段;
UITextField*productField;
UITextField*价格1字段;
UITextField*折扣1p1Field;
UITextField*折扣2p1Field;
UITextField*小计1字段;
UITextField*product2Field;
UITextField*price2Field;
UITextField*折扣1p2field;
UITextField*折扣2p2field;
UITextField*小计2字段;
}
@属性(非原子,保留)IBUITextField*totalField;
@属性(非原子,保留)IBOutlet UITextField*differenceField;
@属性(非原子,保留)IBOutlet UITextField*productField;
@属性(非原子,保留)IBOutlet UITextField*price1Field;
@属性(非原子,保留)IBOutlet UITextField*折扣1p1Field;
@属性(非原子,保留)IBOutlet UITextField*折扣2p1Field;
@属性(非原子,保留)IBOutlet UITextField*小计1字段;
@属性(非原子,保留)IBOutlet UITextField*product2Field;
@属性(非原子,保留)IBOutlet UITextField*price2Field;
@属性(非原子,保留)IBOutlet UITextField*折扣1p2Field;
@属性(非原子,保留)IBOutlet UITextField*折扣2p2Field;
@属性(非原子,保留)IBOutlet UITextField*小计2字段;
-(iAction)计算选项:(id)发送方;
-(iAction)移除键盘;
@结束
#导入“ProCalculator.h”
#导入“ProductCell.h”
@实现前计算程序
@综合总字段、差异字段、产品字段、价格字段、折扣1字段、折扣2字段、小计1字段;
@综合产品2字段、价格2字段、折扣1字段、折扣2字段、小计2字段;
-(iAction)计算选项:(id)发送方
{
//计算第一个单元格内的乘积1
NSString*firstPrice=self.price1Field.text;
NSString*discount1P1=self.discount1P1Field.text;
NSString*折扣2P1=self.discount2P1Field.text;
双倍小计1,含折扣1;
双倍小计1,不含折扣2;
double firstPrice double=[firstPrice doubleValue];
双重折扣1p1double=[折扣1p1doublevalue];
双折扣2P1双值=[折扣2P1双值];
双百分比折扣1p1;
双百分比折扣2P1;
双重储蓄1p1;
双重储蓄2p1;
百分比折扣1p1=折扣1p1倍/100;
百分比折扣2P1=折扣2P1倍/100;
储蓄1p1=首次价格双倍*百分比折扣1p1;
小计1含折扣1=第一价格双倍储蓄1p1;
储蓄2p1=小计1含折扣1*百分比折扣2p1;
小计1带折扣2=小计1带折扣1-储蓄2p1;
NSString*finalSubtotal1=[[NSString alloc]initWithFormat:@“$%.02f”,小计1含折扣2];
self.subtotal1Field.text=finalSubtotal1;
//在第二个单元格中计算产品2(不是构建更多的单元格,我需要从用户输入中获取信息来创建单元格,存储来自单元格的信息并将其添加到计算中)。
NSString*secondPrice=self.price1Field.text;
NSString*折扣1P2=self.discount1P2Field.text;
NSString*折扣2P2=self.discount2P2Field.text;
双倍小计2,不含折扣1;
双倍小计2,不含折扣2;
double secondPrice double=[secondPrice doubleValue];
双重折扣1p2double=[折扣1p2doublevalue];
双重折扣2p2double=[折扣2p2doublevalue];
双百分比折扣1 P2;
双百分比折扣2P2;
双重储蓄1p2;
双重储蓄2p2;
百分比折扣1P2=折扣1P2倍/100;
百分比折扣2P2=折扣2P2倍/100;
储蓄1p2=二次价格双倍*百分比折扣1p2;
小计2含折扣1=二次价格双倍储蓄1p2;
储蓄2p2=小计2(含折扣1)*百分比折扣2p2;
小计2带折扣2=小计2带折扣1-储蓄2p2;
NSString*finalSubtotal2=[[NSString alloc]initWithFormat:@“$%.02f”,小计2 WithDiscount2];
self.subtotal1Field.text=最终subtotal2;
//计算总数
双倍总额;
总金额=小计1带折扣2+小计2带折扣2;
NSString*总=[[NSString alloc]initWithFormat:@“$%.02f”,totalAmount];
self.totalField.text=总数;
//计算节省下来的钱
节省双倍的钱;
存钱=储蓄1p1+储蓄2p1+储蓄1p2+储蓄2p2;
NSString*theMoneySaved=[[NSString alloc]initWithFormat:@“$%.02f”,moneySaved];
self.differenceField.text=货币已保存;
}
-(iAction)移除键盘
{
[self.productField辞职FirstResponder];
[self.price1Field辞职第一响应者];
[self.discount1P1Field辞职第一响应者];
[self.discount2P1Field-resignFirstResponder];
}
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)解除锁定
{
[super dealoc];
}
-(无效)未收到记忆警告
{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
}
-(无效)视图卸载
{
[超级视频下载];
//释放任何保留的
#import <UIKit/UIKit.h>
@interface ProCalculator : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITextField *totalField;
UITextField *differenceField;
UITextField *productField;
UITextField *price1Field;
UITextField *discount1P1Field;
UITextField *discount2P1Field;
UITextField *subtotal1Field;
UITextField *product2Field;
UITextField *price2Field;
UITextField *discount1P2Field;
UITextField *discount2P2Field;
UITextField *subtotal2Field;
}
@property (nonatomic, retain) IBOutlet UITextField *totalField;
@property (nonatomic, retain) IBOutlet UITextField *differenceField;
@property (nonatomic, retain) IBOutlet UITextField *productField;
@property (nonatomic, retain) IBOutlet UITextField *price1Field;
@property (nonatomic, retain) IBOutlet UITextField *discount1P1Field;
@property (nonatomic, retain) IBOutlet UITextField *discount2P1Field;
@property (nonatomic, retain) IBOutlet UITextField *subtotal1Field;
@property (nonatomic, retain) IBOutlet UITextField *product2Field;
@property (nonatomic, retain) IBOutlet UITextField *price2Field;
@property (nonatomic, retain) IBOutlet UITextField *discount1P2Field;
@property (nonatomic, retain) IBOutlet UITextField *discount2P2Field;
@property (nonatomic, retain) IBOutlet UITextField *subtotal2Field;
-(IBAction)calculateDiscounts:(id)sender;
-(IBAction)removeKeyboard;
@end
#import "ProCalculator.h"
#import "ProductCell.h"
@implementation ProCalculator
@synthesize totalField, differenceField, productField, price1Field, discount1P1Field, discount2P1Field, subtotal1Field;
@synthesize product2Field, price2Field, discount1P2Field, discount2P2Field, subtotal2Field;
-(IBAction)calculateDiscounts:(id)sender
{
//Calculate product 1 within first Cell
NSString *firstPrice = self.price1Field.text;
NSString *discount1P1 = self.discount1P1Field.text;
NSString *discount2P1 = self.discount2P1Field.text;
double subtotal1WithDiscount1;
double subtotal1WithDiscount2;
double firstPriceDouble = [firstPrice doubleValue];
double discount1P1Double = [discount1P1 doubleValue];
double discount2P1Double = [discount2P1 doubleValue];
double percentageDiscount1P1;
double percentageDiscount2P1;
double savings1P1;
double savings2P1;
percentageDiscount1P1 = discount1P1Double / 100;
percentageDiscount2P1 = discount2P1Double / 100;
savings1P1 = firstPriceDouble * percentageDiscount1P1;
subtotal1WithDiscount1 = firstPriceDouble - savings1P1;
savings2P1 = subtotal1WithDiscount1 * percentageDiscount2P1;
subtotal1WithDiscount2 = subtotal1WithDiscount1 - savings2P1;
NSString *finalSubtotal1 = [[NSString alloc] initWithFormat:@"$ %.02f", subtotal1WithDiscount2];
self.subtotal1Field.text = finalSubtotal1;
//Calculate product 2 within second Cell (Instead of Building more cells, I need to get information from User Input to create Cells and store information from them and add them to the calculation.
NSString *secondPrice = self.price1Field.text;
NSString *discount1P2 = self.discount1P2Field.text;
NSString *discount2P2 = self.discount2P2Field.text;
double subtotal2WithDiscount1;
double subtotal2WithDiscount2;
double secondPriceDouble = [secondPrice doubleValue];
double discount1P2Double = [discount1P2 doubleValue];
double discount2P2Double = [discount2P2 doubleValue];
double percentageDiscount1P2;
double percentageDiscount2P2;
double savings1P2;
double savings2P2;
percentageDiscount1P2 = discount1P2Double / 100;
percentageDiscount2P2 = discount2P2Double / 100;
savings1P2 = secondPriceDouble * percentageDiscount1P2;
subtotal2WithDiscount1 = secondPriceDouble - savings1P2;
savings2P2 = subtotal2WithDiscount1 * percentageDiscount2P2;
subtotal2WithDiscount2 = subtotal2WithDiscount1 - savings2P2;
NSString *finalSubtotal2 = [[NSString alloc] initWithFormat:@"$ %.02f", subtotal2WithDiscount2];
self.subtotal1Field.text = finalSubtotal2;
//Calculate Total
double totalAmount;
totalAmount = subtotal1WithDiscount2 + subtotal2WithDiscount2;
NSString *theTotal = [[NSString alloc] initWithFormat:@"$ %.02f", totalAmount];
self.totalField.text = theTotal;
//Calculate Money Saved
double moneySaved;
moneySaved = savings1P1 + savings2P1 + savings1P2 + savings2P2;
NSString *theMoneySaved = [[NSString alloc] initWithFormat:@"$ %.02f", moneySaved];
self.differenceField.text = theMoneySaved;
}
-(IBAction)removeKeyboard
{
[self.productField resignFirstResponder];
[self.price1Field resignFirstResponder];
[self.discount1P1Field resignFirstResponder];
[self.discount2P1Field resignFirstResponder];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ProductTableViewCell";
ProductCell *cell = (ProductCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLabel = [[NSBundle mainBundle] loadNibNamed:@"ProductTableViewCell" owner:self options:nil];
for (id currentObject in topLabel) {
if ([currentObject isKindOfClass:[ProductCell class]]) {
cell = (ProductCell *) currentObject;
break;
}
}
}
return cell;
}
@end
@interface TableViewCellModel : NSObject {
NSString * _valueOfTextfield;
}
@property (nonatomic, retain) NSString * valueOfTextfield;
@end
@implementation TableViewCellModel
@synthesize valueOfTextfield = _valueOfTextfield;
@end
@interface TableViewModel : NSObject {
int _index;
NSString * _textFieldValue;
}
@property (nonatomic, assign) int index;
@property (nonatomic, retain) NSString * textFieldValue;
@end
#import "TableViewModel.h"
@implementation TableViewModel
@synthesize index = _index;
@synthesize textFieldValue = _textFieldValue;
@end
#import <UIKit/UIKit.h>
@class TableViewModel;
@interface TableViewCell : UITableViewCell {
IBOutlet UILabel * _label;
}
@property (nonatomic, retain) TableViewModel * tableViewModel;
- (IBAction)useTextField;
@end
#import "TableViewCell.h"
#import "TableViewModel.h"
#import "AppDelegate.h"
@implementation TableViewCell
- (void)dealloc; {
[super dealloc];
}
- (void)setTableViewModel:(TableViewModel *)tableViewModel; {
_label.text = [tableViewModel textFieldValue];
}
- (TableViewModel *)tableViewModel; {
return nil;
}
- (IBAction)useTextField; {
[[AppDelegate appDelegate] useTextFieldForIndex:[self.tableViewModel index]];
}
@end
#import <UIKit/UIKit.h>
@class TableViewCell;
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> {
int _index;
NSMutableArray * _items;
IBOutlet TableViewCell * _cell;
IBOutlet UITableView * _tableView;
IBOutlet UITextField * _textField;
}
- (void)useTextFieldForIndex:(int)aIndex;
@end
#import "ViewController.h"
#import "TableViewModel.h"
#import "TableViewCell.h"
@implementation ViewController
- (void)dealloc; {
[_items release];
[super dealloc];
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad; {
[super viewDidLoad];
// This is an example of how to fill in the _items array with Models
_items = [[NSMutableArray alloc] init];
TableViewModel * model = [[TableViewModel alloc] init];
model.textFieldValue = @"1";
model.index = 0;
[_items addObject:model];
[model release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField; {
TableViewModel * model = [_items objectAtIndex:_index];
model.textFieldValue = [_textField.text retain];
[_items replaceObjectAtIndex:_index withObject:model];
[_tableView reloadData];
[_textField resignFirstResponder];
return YES;
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; {
return 100;//This should be whatever the height of the cell is!
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
return [_items count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
NSString * cellIdentifier = @"TableViewCell";
TableViewCell * cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil]; // hooks up _cell for us
cell = _cell;
}
cell.tableViewModel = ((TableViewModel *) [_items objectAtIndex:indexPath.row]);
return cell;
}
- (void)useTextFieldForIndex:(int)aIndex; {
_index = aIndex;
[_textField becomeFirstResponder];
}
@end
+ (AppDelegate *)appDelegate; {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (void)useTextFieldForIndex:(int)aIndex; {
[self.viewController useTextFieldForIndex:aIndex];
}
double sum = 0;
for(TableViewCellModel * model in _items){
sum += [model.valueOfTextfield doubleValue];
}