2017-10-17 7 views
0

두 가지보기가 있습니다. 클래스 1에서는 사용자 정의 셀이있는 테이블이 있는데, 셀을 세 번 다시 사용합니다. 각 행의 표 셀 텍스트를 다른 클래스의 선택된 텍스트로 업데이트하려고했습니다. 을 구현하여 다른 클래스의 텍스트 값을 가져 와서 업데이트했습니다. 각 행 값을 저장하려면 NSDictionary을 사용하십시오. 그래서 사전 값을 가져 와서 cellforrowatindexpath에서 업데이트하여 셀 텍스트 값을 업데이트 할 수 있습니다. 내 코드,매번 사전 할당 됨

클래스 A - 목적 C :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"challengeTableCell"; 
    cell = (EnrollmentChallengeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
    cell = [[EnrollmentChallengeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 

cell.answerTextfield.delegate = self; 

if (indexPath.row == 0) { 
    cell.questionTitleLabel.text = @"Challenge Question 1"; 
    cell.questionLabel.tag = 100; 
    cell.questionLabel.textColor = [UIColor blackColor]; 
    NSString *user1 = [myDict objectForKey:@"firstQuestion"]; 
    NSLog(@"firstquestion: %@",user1); 
} 
else if (indexPath.row == 1) { 
    cell.questionTitleLabel.text = @"Challenge Question 2"; 
    cell.questionLabel.tag = 101; 
    NSString *user2 = [myDict objectForKey:@"secondQuestion"]; 
    NSLog(@"secondQuestion: %@",user2); 

} 
else { 
    cell.questionTitleLabel.text = @"Challenge Question 3"; 
    cell.questionLabel.tag = 102; 
    NSString *user3 = [myDict objectForKey:@"thirdQuestion"]; 
    NSLog(@"thirdQuestion: %@",user3); 
} 

if (cell.questionLabel.tag == 100 || cell.questionLabel.tag == 101 || cell.questionLabel.tag == 102) { 
    UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectQuestion:)]; 
    [cell.questionLabel setUserInteractionEnabled:YES]; 
    [cell.questionLabel addGestureRecognizer:gesture]; 
} 

    return cell; 
} 

-(void)selectQuestion:(UITapGestureRecognizer *) sender 
{ 
    CGPoint touchLocation = [sender locationOfTouch:0 inView:challengeQuestionsTable]; 
    newIndexPath = [challengeQuestionsTable indexPathForRowAtPoint:touchLocation]; 
    selectQuestionVC = [EnrollmentSelectQuestionViewController instantiate]; 
    selectQuestionVC.questionDelegate = self; 
    [self presentViewController:selectQuestionVC animated:YES completion:nil]; 
} 

#pragma mark - Table Selection Delegate 

-(void)valueChanged:(NSString *)questionString { 
    //[challengeQuestionsTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone]; 
    passedValue = questionString; 
    NSLog(@"questionvalue: %@",passedValue); 
    NSLog(@"mydict: %lu",(unsigned long)[myDict count]); 
    cell = [challengeQuestionsTable cellForRowAtIndexPath:newIndexPath]; 
    NSLog(@"you have selected index: %ld", (long)newIndexPath.row); 
    [[NSUserDefaults standardUserDefaults] setInteger:newIndexPath.row forKey:@"SelectedIndex"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 

    // NSInteger numberOfRows = [challengeQuestionsTable numberOfRowsInSection:[indexPath section]]; 
    NSLog(@"indexpath row: %ld",newIndexPath.row); 

    if (newIndexPath.row == 0) { 
     [myDict setValue:passedValue forKey:@"firstQuestion"]; 
    } else if (newIndexPath.row == 1) { 
     [myDict setValue:passedValue forKey:@"secondQuestion"]; 
    } else { 
     [myDict setValue:passedValue forKey:@"thirdQuestion"]; 
    } 

    NSLog(@"mydict: %@",myDict); 
    NSLog(@"1st: %@",[myDict objectForKey:@"firstQuestion"]); 
    NSLog(@"2nd: %@",[myDict objectForKey:@"secondQuestion"]); 
    NSLog(@"3rd: %@",[myDict objectForKey:@"thirdQuestion"]); 
} 

클래스 B - 스위프트 :

여기 ValueChanged 기능에
var questionDelegate: SelectedQuestionDelegate? 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableView.deselectRow(at: indexPath, animated: true) 
    print("selected index :",indexPath.row) 

    let selectedCell = tableView.cellForRow(at: indexPath) 
    print("did select and the text is \(selectedCell?.textLabel?.text)") 
    let storyboard = UIStoryboard(name: "EnrollmentChallengeQuestions", bundle: nil) 
    challengeVC = storyboard.instantiateViewController(withIdentifier: "ChallengeQuestions") as! EnrollmentChallengeQuestionsViewController 
    challengeVC.passedValue = selectedCell?.textLabel?.text 
    print("text label value: ", challengeVC.passedValue) 
    questionDelegate?.valueChanged(challengeVC.passedValue) 
    self.present(challengeVC, animated: true , completion: nil) 
} 

, 내 사전은 한 번에 하나의 값을 들고있다. 첫 번째 질문에 대한 첫 번째 질문은 두 번째 질문과 두 번째 질문에 대한 질문을 동시에 수행하고자합니다. 매번 사전이 새로 고침됩니다. 또한 viewdidload & viewwillappear에 내 딕트를 할당하려고했습니다. 다른 클래스에서 오는 동안 모든 메소드가 다시 호출됩니다. 이 문제를 극복하는 방법?

+0

당신이 컨트롤러 사이의 탐색이 무엇을 할당되고, 다시 호출되고 다시 있도록 challengeVC 제시? 답변을 보유하고있는 두 개의 컨트롤러 중 어느 것이 답을 갖고 있습니까? 왜 Swift와 Objective C를 섞어 놓았습니까? – Ocunidee

답변

0

self.present(challengeVC, animated: true , completion: nil) 대신 dismiss(animated: true, completion: nil)을 사용해야한다고 생각합니다. 당신이 질문을 선택하면

현재, 당신은 viewDidLoadviewWillAppear 방법 따라서 귀하의 사전이 다시