字典每次都被分配
Dictionary getting allocated every time
我有两种看法。在 class 中,我有 table 自定义单元格,我重复使用了该单元格 3 次。我想用另一个 class 中的选定文本更新每行的 table 单元格文本。我已经实施 protocol
从另一个 class 获取文本值并更新。为了存储每一行值,我使用 NSDictionary
。这样我就可以获取字典值并在 cellforrowatindexpath
中更新以更新单元格文本值。我的代码,
Class A - Objective 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"]);
}
Class B - Swift:
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
函数中,我的字典一次只保存一个值。我想同时保留第一行的第一个问题、第二行的第二个问题和第三行的问题。我的字典每次都在刷新。还尝试在 viewdidload
和 viewwillappear
中分配我的字典。来自另一个 class 的所有方法都被再次调用。如何克服这个问题?
我认为您需要使用 dismiss(animated: true, completion: nil)
而不是 self.present(challengeVC, animated: true , completion: nil)
。
当前,当你 select 一个问题时,你会再次提出 challengeVC,因此它的 viewDidLoad
和 viewWillAppear
方法将再次被调用,因此你的字典将再次被分配
我有两种看法。在 class 中,我有 table 自定义单元格,我重复使用了该单元格 3 次。我想用另一个 class 中的选定文本更新每行的 table 单元格文本。我已经实施 protocol
从另一个 class 获取文本值并更新。为了存储每一行值,我使用 NSDictionary
。这样我就可以获取字典值并在 cellforrowatindexpath
中更新以更新单元格文本值。我的代码,
Class A - Objective 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"]);
}
Class B - Swift:
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
函数中,我的字典一次只保存一个值。我想同时保留第一行的第一个问题、第二行的第二个问题和第三行的问题。我的字典每次都在刷新。还尝试在 viewdidload
和 viewwillappear
中分配我的字典。来自另一个 class 的所有方法都被再次调用。如何克服这个问题?
我认为您需要使用 dismiss(animated: true, completion: nil)
而不是 self.present(challengeVC, animated: true , completion: nil)
。
当前,当你 select 一个问题时,你会再次提出 challengeVC,因此它的 viewDidLoad
和 viewWillAppear
方法将再次被调用,因此你的字典将再次被分配