我有一个 TableView,可以正常构建和绘制,但在 ScrollView 时崩溃。我已经运行了调试器,看起来我的类级别变量正在以某种方式被覆盖,因此当再次调用 titleForHeaderInSection 时它们不再存在。非常奇怪的是,如果我替换代码:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [favouritesDataSections objectAtIndex:section];
return sectionTitle;
}
与:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = @"Test";
return sectionTitle;
}
它仍然会崩溃,但这次当您将鼠标悬停在 sectionTitle 变量上时,调试器列出的不是 NSString。
这是我用来创建 View 和设置类级变量的代码:
- (void)loadView {
[super loadView];
CGRect tableSize = CGRectMake(0,0,320,460);
UITableView *favouritesTableView = [[UITableView alloc] initWithFrame:tableSize style:UITableViewStylePlain];
favouritesTableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
favouritesTableView.dataSource = self;
favouritesTableView.delegate = self;
favouritesTableView.rowHeight = 52;
[self.view addSubview:favouritesTableView];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Get the full path of the favourites plist
NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"Favourites.plist"];
// Initialise Dictionary and array
favouritesDataAll = [[NSMutableDictionary alloc] init];
favouritesDataSections = [[NSArray alloc] init];
NSDictionary *dict = [[[NSMutableDictionary alloc] initWithContentsOfFile:filename] retain];
favouritesDataAll = dict;
[dict release];
favouritesDataSections = [favouritesDataAll allKeys];
}
我非常疯狂地试图追踪这个 - 到目前为止已经花了 2 天时间,所以从外部感谢任何帮助。
最好的问候
戴夫
最佳答案
好的,修复它...改变了
favouritesDataSections = [favouritesDataAll allKeys];
收件人:
favouritesDataSections = [[favouritesDataAll allKeys] retain];
这一切似乎都有效。由此我可以推断,我用来存储章节标题的数组指向在某个随机点自动释放的数据,这就是为什么它在看似奇怪的地方呕吐。
我确实承认,我仍处于编码的“试错”阶段,并不完全理解我在做什么(我相信您读到这里会感到畏缩)。如果您有任何关于进一步阅读的想法/评论链接或关于所有这些是如何工作的(即何时以及为什么使用保留等)以加深我的理解,这对我来说将很有用。
再次感谢戴夫
关于objective-c - UITableView 在滚动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1814970/