Ниже у меня есть код, который перечисляет файлы из каталога моих документов в UITableView
. Однако код работает некорректно, и как только я тестирую свое устройство, даже если в каталоге документов есть файлы, ничего не отображается, а отображается только несколько пустых ячеек. Вот код, я в настоящее время с помощью:Objective-C: Как перечислить файлы из каталога документов в UITableView?
NSArray *filePathsArray;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [filePathsArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
return cell;
}
Почему «numberOfRowsInSection:' return '1', когда нет файлов? – rmaddy
@maddy: В этой строке вы можете отобразить сообщение типа «Нет записей» или любое соответствующее сообщение, чтобы пользователь не путался. –
@maddy: Убедитесь, что вы установили userInteractionEnabled в No для этой ячейки –