2012-06-07 42 views
1

我在一個應用程序中工作,其中我有tableview,其中包含Textfield和Label。現在我想要的是,當我在文本框中輸入文本時,它應該計算一些東西,併爲該單元格的tableview標籤提供結果百分比。 繼承人如何在cellForRowAtIndexpath中創建tetfield和標籤。從textfield獲取值到Tableview中的標籤

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

static NSString *CellIdentifier = @"Cell"; 

lblpercent = [[UILabel alloc]initWithFrame:CGRectMake(395, 5, 270, 30)]; 
UITextField *txtscore = [[UITextField alloc] initWithFrame: CGRectMake(306,5,100,30)]; 

txtscore.delegate = self; 
txtscore.keyboardType = UIKeyboardTypeNumberPad; 
[txtscore addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEnd]; 
lblpercent.text = per; 
} 

而對於caqlculation我使用下面的代碼

-(void) textFieldDone: (id) sender 
{ 


     UITextField *field = sender; 
      NSLog(@"%d",i); 
     NSString *total = field.text; 
     int tot = [total intValue]; 
     NSLog(@"The text is %d", tot); 
     per = [[NSString alloc] init]; 
     if (tot == 90) { 
      percent=90; 

     } 
per = [NSString stringWithFormat:@"%d",percent]; 
    } 

在如何解決這個問題?

+0

解決什麼問題?在我們解決它之前,我們首先需要一個問題... –

回答

1

我有,我肯定會從來如果不是你的問題,所以,謝謝你嘗試過一個不錯的主意;)

如果您不需要爲其他,設置標籤文本字段的任何其他委託方法你可以用這種方式解決你的問題。

上的UILabel

@interface UILabel (CopyTextField) <UITextFieldDelegate> 
@end 


@implementation UILabel (CopyTextField) 
-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    // do whatever you want with your textfield's text and set self.text to the value 
    self.text = textField.text; // here I'm just copying the text as it is 
} 

@end

創建一個類別在你需要設置標籤的文本框的委託另一方面(進口的UILabel + CopyTextField.h)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UILabel *lblpercent = [[UILabel alloc]initWithFrame:CGRectMake(395, 5, 270, 30)]; 
    UITextField *txtscore = [[UITextField alloc] initWithFrame:CGRectMake(306,5,100,30)]; 

    txtscore.delegate = lblpercent; 
    txtscore.keyboardType = UIKeyboardTypeNumberPad; 
} 
+0

非常感謝,我已經嘗試了代碼,它的工作方式與我想要的一樣,我可以用同樣的方法來找到該等級,因爲我需要找到基於等級在lblPercent上,它是同一行上的標籤。 – Purva

+0

使用此解決方案,您只能爲您的文本字段指定一個委託,如果您需要在更改textField的值時更改多個對象,則應該引用這些對象(例如propertys)並將您的控制器用作委託。然後在 - (void)textFieldDidEndEditing:(UITextField *)textField實現中,您需要進行所有需要的更改。 – Moxy