2013-10-17 136 views
1

我正在創建一個使用故事板的iphone應用程序。我基本上是新的目標C和Xcode。創建視圖控制器

我有一個類別列表,每次我點擊一個類別它應該打開一個tableView,所以我可以在該類別中添加一個項目。但不是爲每個類別獲取不同的tableView,而是爲所有類別和添加的項目複製相同的表格。

如何爲每個標籤創建一個新表格?

在此先感謝!

下面是我對加入一個類別

@interface ListViewController() 

@end 

@implementation ListViewController{ 
    NSMutableArray *items; 
} 



@synthesize lists; 



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

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    items = [NSMutableArray arrayWithCapacity:20]; 
    List *item = [[List alloc] init]; 
    item.title = @"Grocery List"; 
    [items addObject:item]; 

    item = [[List alloc]init]; 
    item.title = @"Project List"; 
    [items addObject:item]; 

    item = [[List alloc] init]; 
    item.title = @"Events List"; 
    [items addObject:item]; 

    self.lists = items; 

} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return [self.lists count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    // Configure the cell... 

    /*UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListsCell"]; 

    List *list = [self.lists objectAtIndex:indexPath.row]; 

    cell.textLabel.text = list.title;*/ 

    ListCell *cell = (ListCell *)[tableView dequeueReusableCellWithIdentifier:@"ListsCell"]; 

    List *list = [self.lists objectAtIndex:indexPath.row]; 

    cell.titleLabel.text = list.title; 

    return cell; 

} 

//Add new list, new row will be added on the bottom and its data source must always be sync 
-(void)addViewControllerSave:(AddViewController *)controller addList:(List *)list{ 

    [self.lists addObject:list]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.lists count] - 1 inSection:0]; 

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 


/* 
// 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) { 
     if(editingStyle == UITableViewCellEditingStyleDelete){ 

      [self.lists removeObjectAtIndex:indexPath.row]; 

      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: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 - Navigation 
*/ 

// In a story board-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 

    if([segue.identifier isEqualToString:@"AddList"]){ 
     UINavigationController *navigationController = segue.destinationViewController; 

     AddViewController *addViewController = [[navigationController viewControllers] objectAtIndex:0]; 

     addViewController.delegate = self; 
    } 

    else if([segue.identifier isEqualToString:@"ViewItem"]){ 
     UINavigationController *nav = segue.destinationViewController; 

     ItemViewController *itemViewController = [[nav viewControllers] objectAtIndex:0]; 

     itemViewController.delegate = self; 
    } 
} 



#pragma mark - AddViewControllerDelegate 
-(void)addViewControllerCancel:(AddViewController *)controller{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

-(void)addViewControllerSave:(AddViewController *)controller{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

-(void)itemViewControllerBack:(ItemViewController *)controller{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

@end 

下面是我對加入一個項目:

@interface ItemViewController() 

@end 

@implementation ItemViewController{ 
    NSMutableArray *newItems; 
} 

@synthesize items; 

@synthesize delegate; 

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

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    newItems = [NSMutableArray arrayWithCapacity:20]; 

    Item *i = [[Item alloc]init]; 

    i.listItem = @"a"; 
    [newItems addObject:i]; 

    self.items = newItems; 


} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return [self.items count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Configure the cell... 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemsCell"]; 

    Item *item = [self.items objectAtIndex:indexPath.row]; 

    cell.textLabel.text = item.listItem; 

    return cell; 
} 

-(void)addIteViewControllerSave:(AddItemViewController *)controller addItem:(Item *)item{ 

    [self.items addObject:item]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.items count] -1 inSection:0]; 

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 

} 



#pragma mark - Navigation 

// In a story board-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 

    if([segue.identifier isEqualToString:@"AddItem"]){ 
     UINavigationController *navigationController = segue.destinationViewController; 

     AddItemViewController *addItemViewController = [[navigationController viewControllers]objectAtIndex:0]; 

     addItemViewController.itemDelegate = self; 
    } 
} 

#pragma mark - AddItemViewControllerDelegate 
-(void)addItemviewControllerCancel:(AddItemViewController *)controller{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

-(void)addIteViewControllerSave:(AddItemViewController *)controller{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

#pragma mark - ItemViewControllerDelegate 
-(IBAction)back:(id)sender{ 
    [self.delegate itemViewControllerBack:self]; 
} 



@end 

AddItemViewController

#import "AddItemViewController.h" 
#import "Item.h" 

@interface AddItemViewController() 


@end 

@implementation AddItemViewController 

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

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

} 

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


-(IBAction)cancel:(id)sender{ 
    [self.itemDelegate addItemviewControllerCancel:self]; 
} 

-(IBAction)save:(id)sender{ 
    Item *item = [[Item alloc] init]; 

    item.listItem = self.listItemTextField.text; 

    [self.itemDelegate addIteViewControllerSave:self addItem:item]; 
} 

@end 
+0

請顯示您的工作。 –

回答

1

不要創建新表查看每個標籤,而是用每個標籤的不同數據填充它,則需要更改該對象您存儲的數據,如果你使用的是添加額外內容之前的對象存儲所有你可能需要清除您的陣列添加的項調用

[tableView reloadData];

[self.items removeAllObjects]; 
+0

我仍然感到困惑,但是如何每次創建一個新的viewController,我創建一個新的列表類別? – hs34

+0

每次選擇一個類別時,都不需要創建一個新的tableViewController,只需創建一次,然後重新填充表中的數據即可。我對你的過程有點困惑,所以讓我說出來,告訴我是否做出了錯誤的假設(我們將用食物爲例)你有一個類別列表(它將包含一個表格視圖,跟隨行肉類,蔬菜,水果)。當你點擊一個類別例如水果,你想要一個新的表格視圖來顯示(蘋果,橙,香蕉等) –

+0

我想我誤解了你的問題。你不需要爲每個新類別創建一個新的viewController,你只需要創建該類別的數據。然後調用它與其他類別使用的viewController相同(記住要確保重新加載表格視圖及其相關數據)。 –

相關問題