Ios Objective-c我需要从UItableview获取数组的字符串
我有一个表格视图,其中的行中填充了文档目录中的.csv文件名,我需要打开行中选择的.csv文件,然后在详细视图中显示信息。我正在尝试获取indexPath.row,它已被选中以获取数组中的正确元素,但正在崩溃。这是代码的一部分,上面我将发布完整代码:Ios Objective-c我需要从UItableview获取数组的字符串,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个表格视图,其中的行中填充了文档目录中的.csv文件名,我需要打开行中选择的.csv文件,然后在详细视图中显示信息。我正在尝试获取indexPath.row,它已被选中以获取数组中的正确元素,但正在崩溃。这是代码的一部分,上面我将发布完整代码: -(UITableViewCell *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%d", ind
-(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];
}
#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
这就是错误:
2017-10-09 22:51:31.590248骨水泥板[2340:583894]2
2017-10-09 22:51:31.590460操作板[2340:583894]***-[\uu NSArrayI objectAtIndex:]:消息发送到解除分配的实例0x170246540
以下是完整的代码:
-(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];
}
#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]代码>因为您将要用返回的结果覆盖它。在第行
dirList = filePathsArray;
您将自动释放对象放入变量“dirList”。这里还有内存泄漏。
如果需要将任何对象从一个数组放到另一个数组,请使用方法addObjectsFromArray:
。若您需要将其他数组放入变量中,请使用释放/保留例程(Objective-C中的内存管理)看起来您并没有使用ARC。你可能得到了一个NSArray,但你没有保留它。尝试将dirList变量转换为属性。只需启用ARC即可。它是为你设计的。正如其他人所说,你没有使用ARC,你应该这样做。将项目转换为圆弧,并使属性dirList
strong。如果坚持使用非ARC代码,请将dirList设置为retain属性,并始终使用self.dirList
读取/写入属性,而不是直接引用实例变量。这样,编译器生成的getter和setter将为您管理内存。我在others视图中混合了openframeworks和Objective-c,因此我不能使用ARC:/