我有一个 UIViewController 允许我查看和编辑类的信息。
它只分配一次,但它的行数、节数和数据根据用户选择的值传递给它。
例如。这里它正在编辑一个名称和类型属性(表的标题视图太大了。我这样做是为了让你看到下面的怪异)
所以我输入了一个名字,一切都很好。然后我单击地址属性,详细视图现在如下所示:
到目前为止一切正常。如果我单击取消并返回编辑名称属性,它显示正常。问题是这样的。我像这样向下滚动地址表:
然后单击保存/取消。现在当我回去编辑 name 属性时,tableview 看起来像这样!
就好像以前的表格仍然可以通过表格的标题视图看到..?
我的 viewWillAppear: 这个 uiviewcontroller 的方法如下所示:
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"Retain count of poolcopy is %d\n\n", [self.thePoolFacilityCopy retainCount] );
//This will be a reference to find our which pool Instance field we are editing.
self.sectionFromParentTable = self.cellPath.section;
//This will only be used by the arrays
self.rowFromPreviousTable = self.cellPath.row;
NSString *sectionName;
switch (self.sectionFromParentTable) {
case KNameIndex:
sectionName = @"name";
break;
case KAddressIndex:
sectionName = @"address";
break;
case KPhoneNumberIndex:
sectionName = @"phone";
break;
case KWebAddressIndex:
sectionName = @"url";
break;
case KPricesIndex:
sectionName = @"prices";
break;
case KPoolIndex:
sectionName = @"pools";
default:
break;
}
self.title = [NSString stringWithFormat:@"Editing %@", sectionName];
// use an empty view to position the cells in the vertical center of the portion of the view not covered by
// the keyboard
if (self.sectionFromParentTable == KPhoneNumberIndex || self.sectionFromParentTable == KWebAddressIndex) {
UIView *singleSectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 60)];
self.theTableView.tableHeaderView = singleSectionHeaderView;
[singleSectionHeaderView release];
self.theTableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 10)]autorelease];
}
else if (self.sectionFromParentTable == KAddressIndex) {
UIView *addressHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 0)];
self.theTableView.tableHeaderView = addressHeaderView;
[addressHeaderView release];
self.theTableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 250)]autorelease];
}
else if (self.sectionFromParentTable == KPoolIndex) {
UIView *poolHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 2)];
self.theTableView.tableHeaderView = poolHeaderView;
[poolHeaderView release];
self.theTableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 10)]autorelease];
}
else {
UIView *doubleSectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
self.theTableView.tableHeaderView = doubleSectionHeaderView;
[doubleSectionHeaderView release];
self.theTableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 10)]autorelease];
}
[self.theTableView reloadData];
}
在表格看起来混乱的图片中,cellForRowAtIndexPath 方法只被调用了两次,即使有 3 个单元格可见(2 个正常单元格和一个奇怪的单元格)。奇怪的单元格随着滚动而移动,并且仍然可以点击。
我可以通过在此方法中注释掉以下行来解决此问题。但是,我不想每次有人想要编辑细节时都分配一个新的控制器:
- (PoolFacilityDetailEditController *)childController {
// Instantiate the editing view controller if necessary.
// if (childController == nil) {
PoolFacilityDetailEditController *controller = [[PoolFacilityDetailEditController alloc] initWithNibName:@"PoolFacilityDetailEditController" bundle:nil];
self.childController = controller;
[controller release];
// }
return childController;
}