【问题标题】:how to go on next page from cell tableView to 2 page如何从单元格 tableView 转到下一页到 2 页
【发布时间】:2013-04-16 09:55:14
【问题描述】:

我创建了一个名为 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

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];
        }
    }

}

【问题讨论】:

    标签: ios objective-c uitableview nsmutablearray


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      试试这个

      在单个文件夹和文件数组中搜索所选名称

      - (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]; 
          }
      }
      

      【讨论】:

      • 因为 DetailViewController 是空白最可能使用 [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];我已经编辑了答案
      • 请告诉我更多关于这个的信息。我很困惑
      • 然后使用 [self performSegueWithIdentifier: @"Table" sender: self];而不是 pushViewController
      • 试试这个 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; TableViewController *tableVC = [storyboard instantiateViewControllerWithIdentifier:@"TableViewController"]; [self.navigationController pushViewController:tableVC 动画:YES];
      • 你能给我崩溃日志吗
      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多