[英]Objective-C: How to refresh a UITableView?
我有一些代碼在uitableview
中顯示文件名。 但是,一旦刪除文件並刷新,我會收到一個錯誤。 這是顯示我的文件名,刪除按鈕操作和錯誤的代碼:
首先,當我按下一個按鈕時,我運行以下代碼,該代碼在向表中添加文件時有效:
-(IBAction)refresh{
[[self mytable] reloadData];
}
其次,我有這段代碼來獲取和顯示表將要顯示的值。 在刪除和更新發生之前,它可以正常工作:
- (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;
}
--
-(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;
}
--
-(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]);
但是一旦刪除,它說問題出在下一行。 有人可以幫忙嗎?
由於要在filePathsArray中設置數據,因此tableView渲染的單元格超出了需要的數量。
刪除文件后,更新filePathsArray。
發生的情況是,在您的commitEditingStyle
方法中,您刪除了一個文件,但沒有更新filePathsArray對象以刪除該文件的子路徑。
更新filePathsArray的唯一位置是在cellForRowAtIndexPath
,該位置在 numberOfRowsInSection
之后 numberOfRowsInSection
所以基本上:
commitEditingStyle
的文件,但不更新filePathsArray(該文件仍然包含6個子路徑)。 reloadData
numberOfRowsInSection
以獲取要在UITableView上顯示的單元格的數量。這將返回[filePathsArray count]仍為6。 cellForRowAtIndexPath
。 filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
因此,由於文件已刪除,因此您的filePathsArray具有5個對象。 cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];
現在數組的最后一個索引為4。這將導致崩潰。 只需做filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
在您的commitEditingStyle
,錯誤應停止。
另外,請嘗試不要在cellForRowAtIndexPath
修改數據源對象。 以filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
在那里調用,因為您的數組現在正在commitEditingStyle
中更新。
在tableView:cellForRowAtIndexPath:
,刪除以下代碼: filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
在tableView:commitEditingStyle:
,添加以下代碼:
[filePathsArray removeObjectAtIndex:indexPath.row];
這應該保持您用來填充表格視圖的數據的完整性(因此,當表格不知道該項目時,您不會更改項目數;而當表格認為您擁有該數據時,您就可以更改項目數)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.