【问题标题】:pass NSMutableArray to UITableViewController将 NSMutableArray 传递给 UITableViewController
【发布时间】:2012-04-21 13:11:30
【问题描述】:

我有 UIViewController 包含 NSMutableArray ,我想将此数组传递给 UITableViewController 并在桌子上查看它。我该怎么做??

我的意思是我想(传递)NSMutableArray 或从 UIViewController 到 UITableViewController 的任何变量,而不是(创建)一个表

我想把newBooksArray传给UITableViewController,我在UIViewController里写的:

mytable.gettedBooks = newBooksArray; // gettedBooks is NSMutableArray in UITableViewController

mytable.try = @"Emyyyyy"; // try is a NSString in UITableViewController

我在 DidloadView 的 UITableViewController 中写了

NSLog(@"Try: %@", try); // out is null
NSLog(@"my getted array count: %d", [gettedBooks count]); // out is 0

任何帮助???

【问题讨论】:

标签: iphone objective-c ios ipad ios5


【解决方案1】:

Creating a UITableView and filling with an array

我专门为这个问题创建了上面的教程。

您还可以在developer documents上了解更多方法

首先,您要确保您的@interface 中有所需的委托调用:

@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray         * feed;
    UITableView            * tableView;
}

您希望控制器中的内容类似于以下内容:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [feed count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [mutableArray objectAtIndex:indexPath.row];
    return cell;
}

numberOfRowsInSection 确保您从NSMutableArray 实际加载所需数量的单元格行。而cellForRowAtIndexPath 实际上会将NSMutableArray 每一行的内容加载到UITableView 的每一行中。

为了将它传递给另一个控制器,你不想要这样的东西吗?

UITableViewController *viewController = [[UITableViewController alloc] initWithNibName:@"TableXIB" bundle:nil];
[viewController setGettedBooks:newBooksArray];
[self.navigationController pushViewController:viewController animated:YES];

【讨论】:

  • 我没有观看视频,但您在此处的示例代码中缺少一堆委托方法。他们可能会帮助其他提出问题的人。
  • 更好吗? - 我也添加了numberOfRowsInSection
  • 是的。对不起,我应该说一种数据源方法。我没有想。现在看起来不错!
  • 我使用的是 xcode 4.3,我只想将数组从 UIViewController 传递给 UITableView Controller - 不创建表
  • 意思是我想打开表并查看从 UITableViewController 获取的数组并使用 NSLog 打印它
【解决方案2】:

UITableview tutorial and sample code

希望,这会帮助你..享受

【讨论】:

    【解决方案3】:

    如果您使用的是 Tab Bar 控制器,并且第一个视图控制器是表格视图控制器,第二个是 UIView 控制器。您可以通过以下代码段将数据传递给表视图控制器。您需要在表格视图控制器中声明名为arrayData (NSMutableArray) 的变量并设置属性(因为我们需要从另一个类访问它。)从这个arrayData,您需要在tableView 中加载数据。在 View 控制器类中编写以下代码。

        NSArray *viewControllers = [self.tabBarController viewControllers];
        MyTableViewController *mTable = [viewControllers objectAtIndex:0];
    
        [mTable SetArrayData:arrayFromViewController];
    
        [mTable.tableView reloadData];
    

    如果你使用的是导航控制器,你可以这样做

        NSArray *viewControllers = [self.navigationController viewControllers];
        MyTableViewController *mTable = [viewControllers objectAtIndex:0];
    
        [mTable SetArrayData:arrayFromViewController];
    
        [mTable.tableView reloadData];
    

    您可以选择使用委托。

    【讨论】:

    • 对不起,我不明白数组内容在哪里.. U Illuminate more please??
    猜你喜欢
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多