0

I was wondering what is the best way to go about adding a new cell to a tableview in iOS. Let's say I have a firstviewcontroller and a secondviewcontroller. On the firstviewcontroller I have a tableview already populated with some data, I also have a navigation bar with an add button on the top. Tapping the add button will present a new view and then I want to pass data from that secondviewcontroller to firstviewcontroller and populate my tableview with it.

Thanks, Sam

sam
  • 117
  • 1
  • 13

3 Answers3

1

You secondViewController should create a delegate protocol. Your firstViewController should then be assigned as its delegate.

Once secondViewController saves the data it calls a method that your firstViewController should implement

A good exemple here: How do I create delegates in Objective-C?

To get you started: secondViewController.h

@protocol secondViewControllerDelegate;

@interface secondViewController : UIViewController{
    id<secondViewControllerDelegate> __unsafe_unretained delegate;
}
@property (nonatomic, unsafe_unretained) id<secondViewControllerDelegate> delegate;
@end

@protocol secondViewControllerDelegate <NSObject>
- (void)dataSaved;
@end
Community
  • 1
  • 1
Johann
  • 12,158
  • 11
  • 62
  • 89
0

You can add a new class with tableView data source. initiate it with nesessary data (from both View controllers. use delegate to cooperate them) and after anu data chaging call [tableView setDataSource:]:

//  DataSourceAtoZ.h
#import <Foundation/Foundation.h>
#import "MallsViewController.h"

@interface DataSourceCategory : NSObject <UITableViewDataSource> {
     NSMutableArray *data;
    UITableView *tableView;
}
@property (nonatomic, retain) NSMutableArray *data;

- (id)initWithData:(NSMutableArray *)d;

@end

then, anywhere in your code, compose data you wish to be in tableView and set dataSource:

NSMutableArray *dt = [[NSMutableArray alloc] init];
                for (int i = 0; i < [categories count]; i++) {
                    NSMutableArray *names = [[NSMutableArray alloc] init];
                    [names addObject:[[categories objectAtIndex:i] objectAtIndex:0]];
                    for (int j = 1; j < [[categories objectAtIndex:i] count]; j++) {
                        NSRange match = [[[categories objectAtIndex:i] objectAtIndex:j] rangeOfString:[params objectAtIndex:0]];
                        if (match.length != 0) {
                            [names addObject:[[categories objectAtIndex:i] objectAtIndex:j]];
                        }
                    }
                    [dt addObject:names];
                    [names release];
                }
                dsCategory = [[DataSourceCategory alloc] init];
                dsCategory = [dsCategory initWithData:dt];
                [dt release];
SentineL
  • 4,682
  • 5
  • 19
  • 38
0

You should create your arrays in the App delegate and then call them and change them however you want from there..

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];

once you have this line you can connect between two classes and call any methods on the delegate for example:

[appDelegate populateMyArray];

you don't add "new cells" like you described it, you use the same cells and populate them with different data.

Dor Bashan
  • 136
  • 2