简体   繁体   English

将不同的数组加载到一个UITableView中

[英]Load different Arrays into one UITableView

I have a storyboard based application and I would like to load a different NSArray into a single UITableView , but I don't know how to implement this into my app... 我有一个基于storyboard的应用程序,我想将另一个NSArray加载到单个UITableView ,但是我不知道如何在我的应用程序中实现这一点...

At the moment the structure of my app looks like this: 目前,我的应用程序结构如下:

Tab Bar Controller --> Navigation Controller - ADD --> Add Auction View Controller (where is UITextfield to add auction name with Next button --> TableView Controller where I load NSArray . 标签栏控制器->导航控制器-添加->添加拍卖视图控制器(在UITextfield中可以使用“下一步”按钮添加拍卖名称->加载NSArray TableView控制器)。

Based on user choice from first NSArray - didSelectRowAtIndexPath application will load another NSArray with different data according to his choice, within the same UITableViewController . 基于第一个NSArray用户选择, didSelectRowAtIndexPath应用程序将根据其选择在同一UITableViewController加载另一个具有不同数据的NSArray

Example of what I would like to achieve: 我想要实现的示例:

Selecting "Cars" will load NSArray with car list - selecting again "Car" Category will load up Car Brands NSArray and so on... 选择“汽车”将为NSArray加载汽车列表-再次选择“汽车”类别将为汽车品牌NSArray加载,依此类推...

EXAMPLE TABLEVIEW FLOW 示例表流

I would like to store user choice from NSArray and after going though all the steps display all the selected choices in new UITableView Controller . 我想存储来自NSArray用户选择,尽管所有步骤都在新的UITableView Controller显示所有选择的选择。

So far I managed to create application that allows to set the Auction name and load first NSArray . 到目前为止,我设法创建了一个应用程序,该应用程序可以设置拍卖名称并首先加载NSArray
I need help to make the next step and load new NSArray based on user choice in the same UITableView Controller 我需要帮助进行下一步,并在同一UITableView Controller根据用户选择加载新的NSArray

My files: 我的文件:

SelectClassTableViewController.h SelectClassTableViewController.h

#import <UIKit/UIKit.h>

@interface SelectClassTableViewController : UITableViewController <UITableViewDataSource, UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *Classes;

@end

SelectClassTableViewController.h SelectClassTableViewController.h

#import "SelectClassTableViewController.h"

@interface SelectClassTableViewController ()
{
    NSArray *ClassesArray;
}

@end

@implementation SelectClassTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    ClassesArray = [[NSArray alloc] initWithObjects:@"Cars", @"Motorcycles", @"Boats", @"Planes", @"Other", nil];
    self.Classes.delegate = self;
    self.Classes.dataSource = self;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Number of sections in TableView
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return ClassesArray.count;
}

// Set name of grouped table view
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"Select Car Type:";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ClassesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

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

    // Sets 
    cell.textLabel.text = [ClassesArray objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

.

I know that it is possible to do it using UITableViewDataSource but I don't know how to implement this protocol into my app. 我知道可以使用UITableViewDataSource来做到这一点,但我不知道如何在我的应用程序中实现此协议。 I'm still learning Objective-C, so any advice or help with my code would be appreciated. 我仍在学习Objective-C,因此对我的代码有任何建议或帮助。 Thank you! 谢谢!

Create a NSString in the header file. 在头文件中创建一个NSString。 In viewDidLoad, set the NSString to @"Main" . 在viewDidLoad中,将NSString设置为@"Main" In didSelectRowAtIndexPath , set the NSString to whatever array you want to show (array 1, array 2, array 3...), and reload the table. didSelectRowAtIndexPath ,将NSString设置为要显示的任何数组(数组1,数组2,数组3 ...),然后重新加载表。

In cellForRowAtIndexPath , detect which array is the focus by checking the value of the NSString using an if statement. cellForRowAtIndexPath ,通过使用if语句检查NSString的值来检测哪个数组是焦点。

if ([string isEqualToString: @"Main"]) {

    //show main array

} else if ([string isEqualToString: @"Cars"]) {

    //show cars array...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM