NSIndexPath 在 iOS5 上崩溃

发布于 2024-12-11 07:42:53 字数 9124 浏览 0 评论 0原文

我的应用程序在 iOS5 之前都运行良好。

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

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

更新:下面的整个相关代码:

lastIndexPath1 和 lastIndexPath2 都是我在标头中声明的 NSIndexPath 对象。数字 1 跟踪第 0 节中的最后一个索引路径,数字 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;
}

My App worked fine up until iOS5.

I have a grouped tableview with 2 sections. Each section has 1 selection, that is displayed with a UITableViewCellAccessoryCheckmark.

My App crashed in iOS5 when I try to determine if([lastIndexPath1 isEqual:indexPath])

UPDATED: Entire relevant code below:

lastIndexPath1 and lastIndexPath2 are both NSIndexPath objects that i have declared in the header. Number 1 keeps track of the last indexPath in section 0 and number 2 keeps track of section 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吾性傲以野 2024-12-18 07:42:53

lastIndexPath1 是如何定义的,具有哪些属性?您可以尝试在整个班级中使用 self.lastIndexPath1 吗?

How is lastIndexPath1 defined, with what properties?. Can you try to use self.lastIndexPath1 all thru the class?

沉睡月亮 2024-12-18 07:42:53

您没有保留最后一个索引路径。

    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 ....]

You are not retaining last index path.

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

[NSIndexPath indexPathforRow:row inSection:0] returns an autoreleased object, at the end of the next event loop it is released if it is not retained.

try

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

i'm assuming your not compiling under Xcode 4 and ARC

you really should be using property accessors to handle the memory management for your lastIndexPath1 variable

@property (nonatomic, retain) NSIndexPath lastIndexPath1;

then @synthesize lastIndexPath1;

whenever you assign lastIndexPath use self.lastIndexPath = [NSIndexPath ....]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文