繁体   English   中英

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

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

我有一个表格视图,行中填充了文档目录中 .csv 文件的名称,我需要打开在行中选择的 .csv,然后在详细信息视图中显示信息。 我正在尝试获取 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];
}

这是错误: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

这是完整的代码:

#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

当您从文件管理器取回阵列时,请保留它。

此外,也不需要filePathsArray =[[NSMutableArray alloc] init]; ; filePathsArray =[[NSMutableArray alloc] init]; ; 因为您即将用返回的结果覆盖它。

在线

dirList = filePathsArray;

您将自动释放对象放入变量“dirList”中。 你这里也有内存泄漏。
如果您需要将任何对象从一个数组放入另一个数组,请使用addObjectsFromArray:方法。 如果您需要将其他数组放入变量中,请使用释放/保留例程(Objective-C 中的内存管理)

暂无
暂无

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

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