내 tableView에 세그먼트 컨트롤이있는 헤더가 있습니다. 세그먼트 0은 3 행이있는 내 tableView를 표시하고 세그먼트 1은 4 행을 표시합니다.tableview 셀에서 하위 뷰 제거/추가 토글
각 셀에 textField 하위보기가 추가됩니다. 세그먼트 0에서 우편 번호, 반경 및 API 키가있는 행이 세 개 있습니다. 세그먼트 1에서는 위도, 경도, 반경 및 API 키가있는 네 개의 행이 있습니다. 그러나 앞뒤로 토글하면 textField 하위 뷰가 제대로 제거되지 않습니다. 반지름과 API 키 필드는 동일한 필드이지만 토글 할 때 다른 행에 표시됩니다.
textFields에 태그를 지정하고 해당 태그로 textField를 제거하려고했으나 이상했습니다. 나는 textField의 존재를 테스트했고, 존재했다면 토글에서 그것을 제거했다. (이것은 아래의 예제 코드에 있습니다.) 또한 segmentChanged 메서드를 사용하면 문제가 발생하지 않았습니다.
제 코드를 살펴 보시기 바랍니다. 하위 뷰를 제거하기 가장 좋은 위치와 방법을 잘 모르기 때문에 어떤 포인터를 사용해 주시면 감사하겠습니다.
는- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([mySegmentControl selectedSegmentIndex] == 0) {
return 3;
} else {
return 4;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if ([mySegmentControl selectedSegmentIndex] == 0) {
if (latField) {
[latField removeFromSuperview];
[longField removeFromSuperview];
[radiusField removeFromSuperview];
[apiKeyField removeFromSuperview];
}
if ([indexPath row] == 0 && [indexPath section] == 0) {
zipCodeField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
[[cell textLabel] setText:@"Zip Code:"];
[cell.contentView addSubview:zipCodeField];
} else if ([indexPath row] == 1 && [indexPath section] == 0) {
radiusField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
[[cell textLabel] setText:@"Radius:"];
[cell.contentView addSubview:radiusField];
} else if ([indexPath row] ==2 && [indexPath section] == 0) {
apiKeyField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 145, 30)];
[[cell textLabel] setText:@"API Key:"];
[cell.contentView addSubview:apiKeyField];
}
else if ([mySegmentControl selectedSegmentIndex] == 1) {
if (zipCodeField) {
[zipCodeField removeFromSuperview];
[radiusField removeFromSuperview];
[apiKeyField removeFromSuperview];
}
if ([indexPath row] == 0 && [indexPath section] == 0) {
latField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
[[cell textLabel] setText:@"Latitude:"];
[cell.contentView addSubview:latField];
} else if ([indexPath row] ==1 && [indexPath section] == 0) {
longField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
[[cell textLabel] setText:@"Longitude:"];
[cell.contentView addSubview:longField];
} else if ([indexPath row] ==2 && [indexPath section] == 0) {
radiusField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
[[cell textLabel] setText:@"Radius:"];
[cell.contentView addSubview:radiusField];
} else if ([indexPath row] == 3 && [indexPath section] == 0) {
apiKeyField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 145, 30)];
[[cell textLabel] setText:@"API Key:"];
[cell.contentView addSubview:apiKeyField];
}
}
return cell;
}
- (IBAction)segmentChanged:(id)sender {
[storeFinderTableView reloadData];
}