私は2つのビューを持っています。クラス1ではカスタムセルを持つテーブルがあり、セルを3回再使用しています。私は別のクラスからの選択されたテキストで各行の表のセルのテキストを更新したいと思った。私はを実装して、別のクラスからテキスト値を取得して更新しました。 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 - スウィフト:
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)
}
ここValueChanged
関数で、私の辞書は、一度に一つの値を保持しています。最初の行の最初の質問、2番目の行の質問、2番目の質問の3番目の質問を同時に保持したい。毎回辞書が更新されています。また、viewdidload
& viewwillappear
に私のdictを割り当てようとしました。他のクラスから来ている間に、すべてのメソッドが再び呼び出されます。どのようにこの問題を克服する?
再び割り当てられているあなたの二つのコントローラ間のナビゲーションは何ですか? 答えを保持する2つのコントローラーのうち、答えを含むものはどれですか? なぜSwiftとObjective Cを混在させるのですか? – Ocunidee