objective-c - UITableView 在滚动时崩溃

标签 objective-c iphone uitableview

我有一个 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/

相关文章:

iphone - UINavigationController TitleView 未从 ViewController NavigationItem 显示

iphone - NSFetchedResultsController,修改 NSFetchRequest?

iphone - 使用 UIGestureRecognizer 和点击操作进行幻灯片放映

ios - tableview swift中的行为空

objective-c - iphone 为 iphone 3gs 和 iphone4 构建应用程序

ios - UITableViewCell 中的 TapGesture 导致其他单元格状态改变

objective-c - 对 UITextField 上的操作进行单元测试

C++ 结构成员,什么类型可以在 iPhone 上保留日历时间?

swift - 将数据从 CustomTableView 类发送到其父 View Controller

objective-c - 错误 : property "count" not found on object of type "TableItem*"