请跟我一起,因为我在objective-c上是全新的。提前感谢您提供的任何帮助!
所以这基本上就是我要完成的事情:我有3个主表,其内容永远不会改变,因此我选择在mainstoryboard中构建。想想这些是不同的分组,逐步钻进越来越多的细节。所以你有:
表1(表2的更高级别)>表2(表3的更高级别)>表3
现在我需要添加第4个表,但其内容将根据CSV文件进行更改。现在我忽略了如何使用CSV文件,似乎已经有相当多的信息。所以我选择使用Arrays(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
我的主要问题是我无法将此类的委托和sourcedata连接到表4(其信息将更改的表)。你能帮我吗?你有什么建议可以更好地实现我的目标吗?
答案 0 :(得分:0)
我从您的问题中了解到,您没有一个uitableview来连接您的数据源和委托!
我不确定你是否已将第4个viewcontroller添加到你的故事板中,如果不只是在你的故事板中添加一个UItableviewcontroller,创建一个push segue然后在身份检查器中选择你的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];
}