파일 이름이 내 uitableview
인 코드가 있습니다. 그러나 한 번 파일을 삭제하고 새로 고침 오류가 나타납니다. 여기 내 파일 이름을 표시하려면 코드, 내 삭제 버튼 액션과 오류는 다음과 같습니다Objective-C : UITableView를 새로 고치는 방법?
나는 테이블에 파일 추가 할 때 작동이 코드를 실행 버튼을 누르면 첫째 때
-(IBAction)refresh{
[[self mytable] reloadData];
}
을
둘째, 테이블을 표시 할 값을 가져 와서 표시하는 코드입니다.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(!filePathsArray)
{
return 0;
}
if ([filePathsArray count] > 0)
return [filePathsArray count];
else
return 0;
}
- -
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
NSString *currentFileName = filePathsArray[indexPath.row];
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:currentFileName];
fileURL = [NSURL fileURLWithPath:filePath];
NSLog(@"urlstring %@",fileURL);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:filePath error:NULL];
NSLog(@"successfully deleted");
}
}
-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
NSLog(@"urlstring %@",[filePathsArray objectAtIndex:indexPath.row]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];
filePathsArray = [[NSArray alloc] initWithArray: [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy]];
return cell;
}
-
,536,913- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy];
mytable.dataSource = self;
mytable.delegate = self;
}
: 삭제 및 업데이트가 발생할 때까지이 잘 작동합니다 63,210
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
내가받을 오류는 이것이다 :
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** First throw call stack:
(0x2049012 0x1473e7e 0x1ffeb44 0x3c3a 0x69b8fb 0x69b9cf 0x6841bb 0x694b4b 0x6312dd 0x14876b0 0x2c84fc0 0x2c7933c 0x2c79150 0x2bf70bc 0x2bf8227 0x2bf88e2 0x2011afe 0x2011a3d 0x1fef7c2 0x1feef44 0x1feee1b 0x23dc7e3 0x23dc668 0x5e0ffc 0x2212 0x2145)
libc++abi.dylib: terminate called throwing an exception
(lldb)
내가 인해이 오류가 나타날 수
NSLog(@"urlstring %@",[filePathsArray objectAtIndex:indexPath.row]);
하지만 한 번이 문제가 다음 라인으로 말한다 제거. 아무도 도와 줄 수 있니? 추가 tableView:commitEditingStyle:
에서이 코드 filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
:
[filePathsArray removeObjectAtIndex:indexPath.row];
이것은 당신이 (당신이 테이블 뷰를 채우는 데 사용하는 데이터의 무결성을 그렇게 유지해야을 tableView:cellForRowAtIndexPath:
에서
'numberOfRowsInSection :'의 구현을 좋아합니다. 'return filePathsArray.count; '대신에, 당신을 보호하지 못하는 불필요하고 중복 된 검사를 두 번하고 있습니다. 가독성을 떨어 뜨리고 코드를 파헤 치려고하는 사람을 혼란에 빠뜨립니다. –
@ H2CO3 감각적 인 조언을 비난적인 의견으로 바꾸는 방법을 "사랑"합니다. – herzbube
@herzbube 너무 좋아합니다. 그것은 특별한 힘입니다. –