iphone - 在手机而不是模拟器上测试时,由于 UITableView 滚动位置居中导致应用程序崩溃

标签 iphone ios uitableview

我有一个 uitableview,其中包含一组字典值,生成了一个相当大的列表。 用户可以选择......一旦用户选择了一个单元格,我就从导航堆栈中弹出 View 并将所有值传递回主视图......然后我允许用户返回到这个 View ,如果他们做了错误并想更改他们的输入。

当他们点击回到那个 View 时,我有一个方法可以使用 UITableViewScrollPositionMiddle 自动滚动到旧的选定 View 。

但是我认为它在手机上使用时会导致一些错误,因为模拟器工作正常。

这里是我认为错误发生的地方

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //Center previously selected cell to center of the screen
    [self.tableView scrollToRowAtIndexPath:oldCheckedData  atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; //<--- this method
}

这是我在将它构建到我的手机时在日志中遇到的错误。为了获得此错误的过程是用户单击加载 subview 的父 View 中的单元格,用户在 subview 中选择一个单元格并被带回父 View ..然后当用户返回时到日志中显示的同一 subview 。

2011-10-19 15:00:05.790 icode[1564:707] -[__NSArrayM section]: unrecognized selector sent to instance 0x181cd0
2011-10-19 15:00:05.797 icode[1564:707] CoreAnimation: ignoring exception: -[__NSArrayM section]: unrecognized selector sent to instance 0x181cd0

(没有滚动效果) 然后用户选择一个不同的单元格,这个应用程序崩溃并显示在日志中

2011-10-19 15:01:08.272 icode[1564:707] -[__NSArrayM row]: unrecognized selector sent to instance 0x181cd0
2011-10-19 15:01:08.275 icode[1564:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM row]: unrecognized selector sent to instance 0x181cd0'
*** First throw call stack:
(0x35e9e8b3 0x366e61e5 0x35ea1acb 0x35ea0939 0x35dfb680 0x334a76cf 0x3353c713 0x30e1d 0x3352cd69 0x335a60ab 0x35cc32ab 0x35e72a57 0x35e726bd 0x35e71293 0x35df44e5 0x35df43ad 0x30fa4fed 0x334a7dc7 0x24f7 0x24a0)
terminate called throwing an exception(gdb) 

如有任何帮助,我们将不胜感激。

表格 View 数据源

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [arraysByLetter count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *currentLetter = [sectionLetters objectAtIndex:section];
    return [[arraysByLetter objectForKey:currentLetter] count];
}

/*
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    //override state-based properties set earlier by the table view, such as selection and background colorset up shit here
}
 */

- (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] autorelease];
    } 

    //Customize cell here
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection

    //Replaces previously selected cells accessory view (tick)
    if (indexPath == oldCheckedData) 
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } 
    else 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    } 

    //Display cells with data
    NSArray *keys = [self.arraysByLetter objectForKey:[self.sectionLetters objectAtIndex:indexPath.section]];
    NSString *key = [keys objectAtIndex:indexPath.row];

    cell.textLabel.text = key;
    return cell;
}

//Section headers
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return [sectionLetters objectAtIndex:section];
}

//Section header titles
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return sectionLetters;

}

最佳答案

看起来您在 oldCheckedData 中引用的索引路径可能在 View Controller 从堆栈中弹出时已被释放。确保在 View Controller 消失之前保留它。

关于iphone - 在手机而不是模拟器上测试时,由于 UITableView 滚动位置居中导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7815765/

相关文章:

javascript - 如何通过 iphone html 像电话号码一样使地址可点击(可检测)?

iphone - 将核心数据添加到应用程序时出现编译器错误

ios - 如何隐藏状态栏+在另一个 View Controller (swift)中使用白光内容?

ios - Adobe Air IOS 中的黑色边框

ios - 在 View Controller Swift 中的 UITableView 中加载多个 UICollectionView

iphone - Xcode - 如何禁用页面控制?

iphone - UIView.我可以使用的最大边界尺寸是多少?

iphone - 如何隐藏导航栏后退按钮

ios - 使用 NSFetchedResultsController 滑动删除表格中的单元格

ios - 如何在呈现单元格后检查表格 View 上的复选标记图标(附件 View )?