我有两个UITableView
个对象,它们都有一个名为sectionHeaders
的数组,用作引用对象,为表中给定部分的标题名称提供,这并不奇怪。第一个表视图将其作为NSMutableArray
,并根据需要动态添加标头。第二个视图使用NSArray
并使用@[item1, item2, ...]
简写创建数组。这两个对象都被声明为全局实例变量。
第一个表视图创建第二个表并将其推送到导航控制器堆栈。第一次显示前一个视图时标题会正确显示,并且只要显示后一个视图,它们就会始终正确显示,但当我回击返回第一个视图时,节标题与第二个视图的标题匹配。通过更改两个变量名中的一个立即解决了这个问题,但我更愿意理解为什么问题首先发生。
如果有任何其他信息有用,请告诉我。我不知道可能导致这种情况的原因,所以我不完全确定哪些信息可能有用。
编辑:这是两个对象设置和交互方式的简化版本。
CalendarViewController.m
:
NSMutableArray *sectionHeaders;
@implementation CalendarViewController
-(id) initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
sectionHeaders = [[NSMutableArray alloc] init];
}
//this is the method that adds items to the sectionHeaders object with irrelevant information excluded
-(void) distributeEvents {
[self.tableView beginUpdates];
if(condition1) {
if(![sectionHeaders containsObject:@"Today"]) {
[headers addObject:@"Today"];
}
}
else if(condition2) {
if(![sectionHeaders containsObject:@"Next week"]) {
[headers addObject:@"Next week"];
}
}
//et cetera...
}
//the only other time the sectionHeaders object comes up is in
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return sectionHeaders[section];
}
第二个对象EventViewController
在点击单元格时被初始化。它不是使用与sectionHeaders
对象关联的任何变量创建的。
EventViewController.m
:
NSArray *sectionHeaders;
@implementation EventViewController
//within the init method
sectionHeaders = @[@"What", @"When", @"Where"];
//later...
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return sectionHeaders[section];
}
这是我对这些对象的所有引用。我为没有开头的问题中的简化代码而道歉。希望有更多的背景将有所帮助。
答案 0 :(得分:0)
从您的代码中可以看出,两个sectionHeaders
数组都定义在相应类的范围之外,并且基本上是两个具有相同名称的全局变量 - 在这种情况下,链接器应该给您一个警告。
我建议你把它们移到他们所属的班级里面。