1

请多多包涵,因为我是 Objective-c 的新手。提前感谢您提供的任何帮助!

所以这基本上是我想要完成的:我有 3 个主表,它们的内容永远不会改变,因此我选择在 mainstoryboard 中构建它们。想想这些是不同的分组,逐步深入到越来越多的细节中。所以你有了:

表 1(表 2 的上级)> 表 2(表 3 的上级)> 表 3

现在我需要添加第 4 个表,但其内容将根据 CSV 文件进行更改。现在我忽略了如何使用 CSV 文件,而且似乎已经有相当多的信息了。所以我选择使用 Arrays using (NSArray) 来存储和检索信息。

我首先在 mainstoryboard 中构建了这张表的原型,以便我了解它的外观。然后我编写了下面的代码,理想情况下它将更新表 4 中的信息:

VIEWCONTROLLER.H file
#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>

@end

VIEWCONTROLLER.M file

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


NSArray *nominalManagers;
NSArray *tipsManagers;
NSArray *tipsAmt;
NSArray *nominalAmt;

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


    tipsManagers = [[NSArray alloc]
                    initWithObjects:
                    @"SSG",
                    nil];

    tipsAmt = [[NSArray alloc]
                    initWithObjects:
                    @"$tip",
                    nil];

    nominalManagers = [[NSArray alloc]
                    initWithObjects:
                    @"Wel",
                    @"Gold",
                    @"Colch",
                    @"Stand",
                    nil];

    nominalAmt = [[NSArray alloc]
                    initWithObjects:
                    @"$Wel",
                    @"$Gold",
                    @"$Colch",
                    @"$Stand",
                    nil];

}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    NSUInteger rowNum;
    if (section == 0) {
    rowNum = 1;
    }
    else if (section == 1) {
    rowNum = 4;
    }
    else {
    rowNum = 0;
    }

    return rowNum;

}


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

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

    // Configure the cell...
    NSUInteger row = [indexPath row];
    NSInteger section = [indexPath section];

    switch (section) {
        case 0: // First cell in section 1
            cell.textLabel.text = [tipsManagers objectAtIndex:[indexPath row]];
            cell.detailTextLabel.text = [tipsAmt objectAtIndex:[indexPath row]];
            break;
        case 1: // Second cell in section 1
            cell.textLabel.text = [nominalManagers objectAtIndex:[indexPath row]];
            cell.detailTextLabel.text = [nominalAmt objectAtIndex:[indexPath row]];
            break;
        default:
            cell.textLabel.text = @"WRONG SECTION";
            break;
    }

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#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

我的主要问题是我无法将此类的委托和源数据连接到表 4(其信息将更改的表)。你能帮我吗?你有什么建议可以更好地实现我的目标吗?

4

1 回答 1

0

我从您的问题中了解到的是您没有 uitableview 来连接您的数据源和委托!?

我不确定您是否已将第 4 个视图控制器添加到情节提要中,如果不只是将 UItableviewcontroller 添加到情节提要中,请创建一个推送序列,然后在身份检查器中选择您的 VIEWCONTROLLER 作为类名。

添加 IBOutlet UITableView到您的 .h 文件中,如下所示,并在您的界面构建器中连接您的数据源和委托。当你需要刷新你的 tableview 调用 [self.tableviewname reloadData];

您可以使用 NSMutableArray 编辑数组中的项目,如果使用 NSArray 数组项目将是静态的。

VIEWCONTROLLER.H 文件

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) IBOutlet UITableView *fourthTable;

@end

VIEWCONTROLLER.M 文件

    @implementation ViewController
    @synthesize fourthTable;
- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


    tipsManagers = [[NSArray alloc]
                    initWithObjects:
                    @"SSG",
                    nil];

    tipsAmt = [[NSArray alloc]
                    initWithObjects:
                    @"$tip",
                    nil];

    nominalManagers = [[NSArray alloc]
                    initWithObjects:
                    @"Wel",
                    @"Gold",
                    @"Colch",
                    @"Stand",
                    nil];

    nominalAmt = [[NSArray alloc]
                    initWithObjects:
                    @"$Wel",
                    @"$Gold",
                    @"$Colch",
                    @"$Stand",
                    nil];

 [self.fourthTable reloadData];

}
于 2013-01-02T17:55:25.123 回答