简体   繁体   中英

UITableView Crash on scroll , add or delete cell

I've just changed my population method to use a plist and it's caused a crash upon attempting to add or delete a cell. Scrolling the table also causes a crash. I am not using IB and this is all created programmatically. Searching questions results in no answers or IB related solutions. What I have for code is

//****************** Set Table Data



NSString *filePath = [[NSBundle mainBundle] pathForResource:@"stuff" ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
devices = [NSMutableArray arrayWithArray:[dict objectForKey:@"Devices"]];


//****************** Customize Number of Rows in Table


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    int count = [devices count];
    if(self.editing) count++;
    return count;
}


//****************** Customize the Cells


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];

    if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];

}

    switch(indexPath.section){
    case 0:{ cell.textLabel.text = [devices objectAtIndex: indexPath.row];} break;
    default: break;
}


    return cell;

}

This is what I have deemed relevant code since everything was fine until I populated from a plist.

More code has been requested

 //***************** Allow Editing of Cells

  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

 [devices removeObjectAtIndex:indexPath.row];
 [myTable reloadData];

}



//****************** Add New Cell If ok button is pressed

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

UITextField * newDevice = [alertView textFieldAtIndex:0];
        NSLog(@"newDevice - %@",newDevice.text);

if (alertView.tag == 1 && buttonIndex == 1){
[devices addObject: newDevice.text];
[myTable reloadData];

UPDATE

Commenting out my data loading solves the crash leading me to believe I've incorrectly invoked the way to load data. Of course I'll look into this and appreciate a bigger brain throwing in his two cents in the mean time.

Change switch to

 switch(indexPath.section){
   case 0:{ 
       if ([devices count] > indexPath.row) {
            cell.textLabel.text = [devices objectAtIndex: indexPath.row];
       }
   } break;
   default: break;
 }

Fixed. It seems my array was being auto released instantly and a manual retain then release fixed my problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM