0

我创建了一个名为 ViewControll 的页面,即 tableview。我从 url 读取 2 组 NSString 并将其存储在 NSMutableArray 中。(名称:文件和文件夹)并且两个数组都存储一个具有名称的数组:(全部)此 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

视图控制器.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];
        }
    }

}
4

2 回答 2

0

-initWithNibName:bundle:是 UIViewController 的指定初始化器。

TableViewController *vc = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil];

对视图控制器使用这种初始化

于 2013-04-16T09:59:52.233 回答
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]; 
    }
}
于 2013-04-16T10:06:44.133 回答