Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a view controller
    primarykey
    data
    text
    <p>I'm creating an iphone app using storyboard. I'm basically new on objective c and Xcode.</p> <p>I have a list of categories, every time I click on a category it should open a tableView, so I can add an item in that category. But instead of getting a different tableView for each category, its the same table for all the categories and the added items are copied. </p> <p>How can I create a new table for each label? </p> <p>Thanks in advance!</p> <p>Here's what I have for adding a category</p> <pre><code>@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 </code></pre> <p>Here's what I have for adding an item:</p> <pre><code>@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 </code></pre> <p>AddItemViewController</p> <pre><code>#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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload