iOS5 上的 NSIndexPath 崩溃问题

6

我的应用在 iOS5 之前运行得很好。

我有一个分组的表格视图,有2个部分。每个部分都有1个选择,显示为 UITableViewCellAccessoryCheckmark。

当我试图确定if([lastIndexPath1 isEqual:indexPath])时,我的应用在 iOS5 中崩溃了。

更新:完整相关代码如下:

lastIndexPath1 和 lastIndexPath2 都是我在头文件中声明的 NSIndexPath 对象。Number 1 跟踪第 0 部分的最后一个 indexPath,而 Number 2 则跟踪第 1 部分。

- (void)viewDidLoad
{



 [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];  

//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];

NSArray *fontName = [NSArray arrayWithObjects:@"Baskerville", @"Palatino", @"Times New Roman", @"Verdana", nil];
NSDictionary *fontNameInDict = [NSDictionary dictionaryWithObject:fontName forKey:@"FontOptions"];

NSArray *fontSize = [NSArray arrayWithObjects:@"Der var engang", @"Der var engang", @"Der var engang", @"Der var engang", @"Der var engang", @"Der var engang", @"Der var engang", @"Der var engang", @"Der var engang", @"Der var engang", @"Der var engang", @"Der var engang", nil];
NSDictionary *fontSizeInDict = [NSDictionary dictionaryWithObject:fontSize forKey:@"FontOptions"];

[listOfItems addObject:fontNameInDict];
[listOfItems addObject:fontSizeInDict];


}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];


     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    //SET SKRIFTTYPE 
    NSString *neverOpendFonts1 = [prefs objectForKey:@"neverOpendFonts1"];  

    if (![neverOpendFonts1 isEqualToString:@"1"]) {



            lastIndexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];

            UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath1]; 

            newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

            NSString *lastIndexPathString1 = [NSString stringWithFormat:@"1"];

            [prefs setObject:lastIndexPathString1 forKey:@"lastIndexPath1"];

            NSString *fontName = [NSString stringWithFormat:@"Palatino"];

            [prefs setObject:fontName forKey:@"fontName"];

            neverOpendFonts1 = [NSString stringWithFormat:@"1"];

            [prefs setObject:neverOpendFonts1 forKey:@"neverOpendFonts1"];

            [prefs synchronize];

            NSLog(@"Har aldrig været åbnet - font options"); 


    }

    else
    {
        NSInteger row = [[prefs objectForKey:@"lastIndexPath1"] intValue];

        NSLog(@"FONT ROW: %i", row);

        lastIndexPath1 = [NSIndexPath indexPathForRow:row inSection:0];

        UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath1]; 

        newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

    }
 //SET SKRIFT STØRRELSE


    NSString *neverOpendFonts2 = [prefs objectForKey:@"neverOpendFonts2"];  

    if (![neverOpendFonts2 isEqualToString:@"1"]) {

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {

        lastIndexPath2 = [NSIndexPath indexPathForRow:2 inSection:1];

        UITableViewCell *newCell2 = [myTableView cellForRowAtIndexPath:lastIndexPath2]; 

        newCell2.accessoryType = UITableViewCellAccessoryCheckmark; 

        NSString *lastIndexPathString2 = [NSString stringWithFormat:@"2"];

        [prefs setObject:lastIndexPathString2 forKey:@"lastIndexPath2"];

        NSString *fontSize = [NSString stringWithFormat:@"24.0"];  

        [prefs setObject:fontSize forKey:@"fontSize"];

        neverOpendFonts2 = [NSString stringWithFormat:@"1"];

        [prefs setObject:neverOpendFonts2 forKey:@"neverOpendFonts2"];

        [prefs synchronize];

        }

        else {

            lastIndexPath2 = [NSIndexPath indexPathForRow:0 inSection:1];

            UITableViewCell *newCell3 = [myTableView cellForRowAtIndexPath:lastIndexPath2]; 

            newCell3.accessoryType = UITableViewCellAccessoryCheckmark; 

            NSString *lastIndexPathString2 = [NSString stringWithFormat:@"0"];

            [prefs setObject:lastIndexPathString2 forKey:@"lastIndexPath2"];

            NSString *fontSize = [NSString stringWithFormat:@"16.0"];  

            [prefs setObject:fontSize forKey:@"fontSize"];

            neverOpendFonts2 = [NSString stringWithFormat:@"1"];

            [prefs setObject:neverOpendFonts2 forKey:@"neverOpendFonts2"];

            [prefs synchronize];
        }

    }

    else
    {

        NSInteger row2 = [[prefs objectForKey:@"lastIndexPath2"] intValue];

        NSLog(@"FONTSIZE ROW: %i", row2);

       lastIndexPath2 = [NSIndexPath indexPathForRow:row2 inSection:1];

        UITableViewCell *newCell2 = [myTableView cellForRowAtIndexPath:lastIndexPath2]; 

        newCell2.accessoryType = UITableViewCellAccessoryCheckmark; 


    }

    [myTableView reloadData];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //Number of rows it should expect should be based on the section
    NSDictionary *dictionary = [listOfItems objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"FontOptions"];
    return [array count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [listOfItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
       // cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...

    //First get the dictionary object
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"FontOptions"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;


    if (indexPath.section == 0)
    {

        if([lastIndexPath1 isEqual:indexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else 
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

        switch (indexPath.row) {
            case 0:
            {
             cell.textLabel.font = [UIFont fontWithName:@"Baskerville" size:24];   
            }
                break;
            case 1:
            {
              cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:24];    
            }
                break;
            case 2:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Times New Roman" size:24];  
            }
                break;
            case 3:
            {
               cell.textLabel.font = [UIFont fontWithName:@"Verdana" size:24];   
            }
                break;
        }

    }
    if (indexPath.section == 1) {


        if([lastIndexPath2 isEqual:indexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else 
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

        switch (indexPath.row) {
            case 0:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:16];   
            }
                break;
            case 1:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:20];    
            }
                break;
            case 2:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:24];  
            }
                break;
            case 3:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:28];   
            }
                break;
            case 4:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:32];   
            }
                break;
            case 5:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:36];   
            }
                break;
            case 6:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:40];   
            }
                break;
            case 7:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:44];   
            }
                break;
            case 8:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:48];   
            }
                break;
            case 9:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:52];   
            }
                break;
            case 10:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:56];   
            }
                break;
            case 11:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:60];   
            }
                break;
        }


        }


        return cell;
}

3
控制台是否有关于错误的提示?或者,至少,这个错误是一个SIGABRT还是EXC_BAD_ACCESS? - user767985
是的,我得到了一个EXC_BAD_ACCESS错误。对于这一点我没有表达清楚感到抱歉...如果我注释掉if([lastIndexPath1 isEqual:indexPath])函数,应用程序就可以正常运行... - CCDEV
我正在进行比较:-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath... 如果我使用整数设置值,我也会遇到错误。这只是在iOS5上的问题。 - CCDEV
1
lastIndexPath1是如何定义的,具有哪些属性?您能否尝试在整个类中使用self.lastIndexPath1? - ott--
如果我注释掉if([lastIndexPath1 isEqual:indexPath])函数,应用程序就可以正常加载。我刚刚注意到当我关闭视图时,在我的dealloc中会出现相同的错误 af [lastIndexPath 1 release];我可能初始化有误吗? - CCDEV
显示剩余5条评论
2个回答

7
如何定义lastIndexPath1,它包含哪些属性?你能试着在整个类中使用self.lastIndexPath1吗?

正如richerd所评论的那样,问题的根源在于保留对象。self.indexPath = ... 确保如果属性标记为“retain”,则调用保留setter方法 - 对于非属性,您应该自己保留/释放。 - Oded Ben Dov
1
我在支持iOS 5.0的应用程序方面遇到了相同的问题,谢谢大家。 - Mayur Birari

2
您没有保留最后一个索引路径。
    lastIndexPath1 = [NSIndexPath indexPathForRow:row inSection:0];

[NSIndexPath indexPathForRow:row inSection:0]返回一个自动释放的对象,如果没有被保留,它会在下一个事件循环结束时被释放。

尝试一下:

[[NSIndexPath indexPathForRow:row inSection:0] retain];  

我猜你没有在Xcode 4和ARC下编译

你应该使用属性访问器来处理lastIndexPath1变量的内存管理

@property (nonatomic, retain) NSIndexPath lastIndexPath1;

然后使用@synthesize lastIndexPath1;

每当您分配lastIndexPath时,请使用self.lastIndexPath = [NSIndexPath ....]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接