ViewController 내에서 TableView를 사용하고 있습니다. 3 개의 섹션과 1 개의 섹션이있는 시나리오는 두 가지가 있습니다.TableViewCell이 올바르게 초기화되지 않습니다.
앱이 기본값으로로드되면 3입니다. TableViewCell 중 하나는 사용자가 선택하는 다른 ViewController를 보여줍니다. 그런 다음 다시 버튼을 사용하여 테이블이 다시로드되는 시작 VC로 돌아갑니다.
사용자가 4 섹션에서 3 섹션으로 이동하면 문제가 발생합니다. 아래 코드에서 알 수 있듯이 3 섹션 (사례 2)은 4 섹션을 표시 할 때 textField를 구현합니다. 사용자가 다시 3으로 이동하면 textField가 유지됩니다. 테이블을 다시로드 할 때 처음부터 테이블을 시작할 것이라고 가정했습니다. 대신 이전 설정을 유지하고 새 설정을 맨 위에 추가합니다.
아이디어가 어떻게됩니까? 어떤 종류의 재설정?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *serverLocCell = [tableView dequeueReusableCellWithIdentifier:@"homeCell"];
if (sections == 3) {
switch (indexPath.section) {
case 0:
if ([testLocation isEqualToString:@""]) {
serverLocCell.textLabel.text = @"Choose location";
}
else {
serverLocCell.textLabel.text = testLocation;
}
serverLocCell.detailTextLabel.text = @"Change";
serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 1:
serverLocCell.textLabel.text = testType;
serverLocCell.detailTextLabel.text = @"Change";
serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 2:
serverLocCell.textLabel.text = @"Start Test";
serverLocCell.textLabel.textColor = [UIColor whiteColor];
serverLocCell.textLabel.textAlignment = NSTextAlignmentCenter;
serverLocCell.detailTextLabel.text = @"";
serverLocCell.accessoryType = UITableViewCellAccessoryNone;
serverLocCell.backgroundColor = [UIColor colorWithRed:0.0/255 green:102.0/255 blue:51.0/255 alpha:1.0];
break;
default:
break;
}
}
else {
switch (indexPath.section) {
case 0:
if ([testLocation isEqualToString:@""]) {
serverLocCell.textLabel.text = @"Choose location";
}
else {
serverLocCell.textLabel.text = testLocation;
}
serverLocCell.detailTextLabel.text = @"Change";
serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 1:
serverLocCell.textLabel.text = testType;
serverLocCell.detailTextLabel.text = @"Change";
serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 2:
if ([testType isEqualToString:@"Published App Test"]) {
publishedAppTextField = [[UITextField alloc]init];
publishedAppTextField.tag = 1;
[publishedAppTextField addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
serverLocCell.textLabel.text = @"";
serverLocCell.accessoryType = UITableViewCellAccessoryNone;
publishedAppTextField.frame = CGRectMake(7, 10, 300, 30);
publishedAppTextField.clearsOnBeginEditing = YES;
publishedAppTextField.delegate = self;
publishedAppTextField.returnKeyType = UIReturnKeyDone;
[serverLocCell setSelectionStyle:UITableViewCellSelectionStyleNone];
[serverLocCell.contentView addSubview:publishedAppTextField];
serverLocCell.backgroundColor = [UIColor whiteColor];
break;
}
else {
break;
}
case 3:
serverLocCell.textLabel.text = @"Start Test";
serverLocCell.textLabel.textColor = [UIColor whiteColor];
serverLocCell.textLabel.textAlignment = NSTextAlignmentCenter;
serverLocCell.detailTextLabel.text = @"";
serverLocCell.accessoryType = UITableViewCellAccessoryNone;
serverLocCell.backgroundColor = [UIColor colorWithRed:0.0/255 green:102.0/255 blue:51.0/255 alpha:1.0];
break;
default:
break;
}
}
return serverLocCell;
}
감사합니다,
가 어떻게'sections'을 추적하는 데 도움이
희망 : 그래서 다음과 같이 보일 수있다? – KDaker
힌트 : 셀은 재활용됩니다. –