我创建一个名为ViewControll的页面,即tableview。 我从url读取2组NSString并将其存储在NSMutableArray中。 (名称:文件和文件夹),两个数组都存储一个名称为:( all)的NSMutableArray显示在根页面(ViewController)
我有另外两个名字:(TabelViewController& DetailViewController),TabelViewController用于文件夹数组,DetailViewController用于文件数组。
我希望何时单击单元格,如果该单元格来自文件夹数组,请转到TableViewController页面,否则,如果该单元格来自文件数组,请转到DetailViewController页面。
这是我的代码,但不工作!!!: 的 ViewController.h
@interface ViewController : UITableViewController
{
NSMutableArray *folder;
NSMutableArray *file;
NSMutableArray *all;
NSInteger folderNumber,fileNumber;
}
@property (nonatomic,strong) IBOutlet UITableView *tables;
@end
ViewController.m
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *Number1 = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.101/mamal/filemanager.php?dir=root&filenum"]];
fileNumber = [Number1 integerValue];
NSString *Number2 = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.101/mamal/filemanager.php?dir=root&foldernum"]];
folderNumber = [Number2 integerValue];
NSLog(@"file: %d & folder: %d",fileNumber,folderNumber);
for (int i=0; i < folderNumber; i++)
{
NSString *folderName = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.101/mamal/filemanager.php?dir=root&folder=%d&name",i]]];
if (!folder)
{
folder = [NSMutableArray array];
}
[folder addObject:folderName];
}
for (int j=0; j < fileNumber; j++)
{
NSString *fileName = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.101/mamal/filemanager.php?dir=root&file=%d&name",j]]];
NSLog(@"%@",fileName);
if (!file)
{
file = [NSMutableArray array];
}
[file addObject:fileName];
}
all = [[NSMutableArray alloc]init];
[all addObjectsFromArray:folder];
[all addObjectsFromArray:file];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return all.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [all objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%d",indexPath.row+1);
if (indexPath.row+1 <= folderNumber)
{
TableViewController *tab = [[TableViewController alloc]init];
[self.navigationController pushViewController:tab animated:YES];
}
else
{
if (indexPath.row+1 > folderNumber)
{
DetailViewController *detail = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detail animated:YES];
}
}
}
答案 0 :(得分:0)
-initWithNibName:bundle:
是UIViewController的指定初始值设定项。
TableViewController *vc = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
对视图控制器使用此类初始化
答案 1 :(得分:0)
试试这个
在单个文件夹和文件数组中搜索所选名称
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
if([folder containsObject:[all objectAtIndex:indexPath.row])
{
TableViewController *tableVC = [storyboard instantiateViewControllerWithIdentifier:@"TableViewController"];
[self.navigationController pushViewController:tableVC animated:YES];
}
else
{
DetailViewController *detail = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
[self.navigationController pushViewController:detail animated:YES];
}
}