2012-06-07 2 views
1

나는 텍스트 필드와 레이블이 포함 된 tableview가있는 응용 프로그램에서 작업하고 있습니다. 이제 내가 원하는 것은 점수를 가진 텍스트 필드에 텍스트를 입력 할 때 무언가를 계산하고 그 셀에 대한 tableview 레이블의 결과 백분율을 알려주는 것입니다. cellForRowAtIndexpath에 tetfield와 label을 어떻게 만들었는가? Textview에서 Tableview의 Label으로 값 가져 오기

- (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; 
} 

그리고 난 방법에서이 문제를 해결 할 수있는 다음 코드

-(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]; 
    } 

을 사용 caqlculation에 대한

?

+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