简体   繁体   English

Objective-c 我需要从 UItableview 获取一个数组的字符串

[英]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.我有一个表格视图,行中填充了文档目录中 .csv 文件的名称,我需要打开在行中选择的 .csv,然后在详细信息视图中显示信息。 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:我正在尝试获取 indexPath.row ,它已被选择以获取数组中的正确元素,但在这里崩溃的是代码的一部分,上面我将发布完整代码:

-(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这是错误:2017-10-09 22:51:31.590248 oFiOSstoryboard[2340:583894] 2 2017-10-09 22:51:31.590460 oFiOSstoryboard[2340:58389]: ***NS_Array_sentIndex[2340:583894]释放实例 0x170246540 ![错误] 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]; ; 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'.您将自动释放对象放入变量“dirList”中。 Also you have a memory leak here.你这里也有内存泄漏。
If you need to put any objects from one array to other, use method addObjectsFromArray: .如果您需要将任何对象从一个数组放入另一个数组,请使用addObjectsFromArray:方法。 If you need to put other array into variable, use release/retain routine (Memory management in Objective-C)如果您需要将其他数组放入变量中,请使用释放/保留例程(Objective-C 中的内存管理)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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