简体   繁体   中英

Objective-c I need to get a string of an array from a UItableview

I have a tableview that the rows are been filled with the name of .csv files that are in the document directory, i need to open the .csv that is been selected in the row and then show the info in a detail view. I'm trying to get the indexPath.row that it's been selected to get the correct element in the array but is crashing here's is that part of the code and above i will post the full code:

-(UITableViewCell *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"%d", indexPath.row); 
  currentcsvfile = [dirList objectAtIndex:indexPath.row ];;
   NSLog(@"\n current csv %@",currentcsvfile);
  [self performSegueWithIdentifier:@"detailsegue" sender:self];
}

This is the error: 2017-10-09 22:51:31.590248 oFiOSstoryboard[2340:583894] 2 2017-10-09 22:51:31.590460 oFiOSstoryboard[2340:583894] *** -[__NSArrayI objectAtIndex:]: message sent to deallocated instance 0x170246540 ![error ] 1

Here is the full code:

#import "CameraViewController.h"
#import "resultsDetailView.h"


@interface CameraViewController ()

@property (strong, nonatomic) IBOutlet UITableView *data;
@property (retain, nonatomic) IBOutlet UILabel *timeStamp;

@end

                                 ////////////////////////csv readder
NSMutableArray *tableDataArray;

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot];
NSString *filename;

NSMutableArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *strPath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"csv"];
NSString *strFile = [NSString stringWithContentsOfFile:strPath  encoding:NSUTF8StringEncoding error:nil];
NSMutableArray *timeStampb = [[NSMutableArray alloc] init]; ;
NSMutableArray *arrayToDelete = [[NSMutableArray alloc] init]; ;
NSMutableArray *filePathsArray ;
NSMutableArray *dirList= [[NSMutableArray alloc] init]; ;
NSString *currentcsvfile;


@implementation CameraViewController



@synthesize data;


- (void)viewDidLoad  {
[super viewDidLoad];
  // ////lista de documentos

    self.data.scrollEnabled = YES;

    self.data.delegate = self;
    self.data.dataSource = self;


    filePathsArray =[[NSMutableArray alloc] init]; ;


    NSMutableArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSFileManager *manager = [NSFileManager defaultManager];
    NSMutableArray*   fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
    //--- Listing file by name sort
    NSLog(@"\n File list %@",fileList);

    //---- Sorting files by extension
    NSMutableArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.csv'"];
    filePathsArray =  [filePathsArray filteredArrayUsingPredicate:predicate];
    NSLog(@"\n\n Sorted files by extension %@",filePathsArray);



    dirList = filePathsArray;

    NSString *docPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

   if (!strFile) {
        NSLog(@"Error reading file.");
    }

    [timeStampb release];


      timeStampb = [[NSMutableArray alloc] initWithArray:[strFile componentsSeparatedByString:@"\,"]];
        // this .csv file is seperated with new line character
        // if .csv is seperated by comma use "," instesd of "\n"
    for(NSString *countryname in timeStampb) {
        NSLog(@"%@", timeStampb);

    }


    }


////////////////////////////////////////////////////////////////Delete csv files
//- (IBAction)delet:(id)sender {
//    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//    
//    NSString *filePath = [docPath stringByAppendingPathComponent:@"jorge.csv"];
//    NSError *error = nil;
//    [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

# pragma – mark table view DataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dirList count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier ];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

        UIColor* color = [UIColor colorWithRed:(254.0/255.0) green:(251.0/255.0) blue:(248.0/255.0) alpha:1];
        UIView *bgColorView = [[UIView alloc] init];

        bgColorView.backgroundColor = [UIColor colorWithRed:(253.0/255.0) green:(0.0/255.0) blue:(237.0/255.0) alpha:1];


        [cell setSelectedBackgroundView:bgColorView];
        cell.backgroundColor = color;
    }

    cell.textLabel.text = [timeStampb objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [dirList objectAtIndex:indexPath.row];


    cell.textLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(0.0/255.0) blue:(0.0/255.0) alpha:1];
    cell.textLabel.font=[UIFont systemFontOfSize:8.0];

    cell.detailTextLabel.font=[UIFont systemFontOfSize:15.0];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:(235.0/255.0) green:(120.0/255.0) blue:(33.0/255.0) alpha:1];



    return cell;


}

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

    NSLog(@"%d", indexPath.row); // you can see selected row number in your console;


        currentcsvfile = [dirList objectAtIndex:indexPath.row ];;
        NSLog(@"\n current csv %@",currentcsvfile);


    [self performSegueWithIdentifier:@"detailsegue" sender:self];

}

- (IBAction)deleteRow:(id)sender {

    //we are not in edit mode yet
    if([self.data isEditing] == NO){
        //up the button so that the user knows to click it when they
        //are done
        [self.data setTitle:@"Done"];
        //set the table to editing mode
        [self.data setEditing:YES animated:YES];
    }else{
        //we are currently in editing mode
        //change the button text back to Edit
        //take the table out of edit mode
        [self.data setEditing:NO animated:YES];

}
}


#pragma mark – TableView delegate

- (void)dealloc {
    [_timeStamp release];

    [dirList release];
    self.data.delegate = nil;
    self.data.dataSource = nil;
    [super dealloc];
}
@end

When you get the array back from the file manager, retain it.

Also, there's no need for filePathsArray =[[NSMutableArray alloc] init]; ; filePathsArray =[[NSMutableArray alloc] init]; ; since you are about to overwrite it with the returned results.

At line

dirList = filePathsArray;

You put autorelease object into variable 'dirList'. Also you have a memory leak here.
If you need to put any objects from one array to other, use method addObjectsFromArray: . If you need to put other array into variable, use release/retain routine (Memory management in Objective-C)

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