Passing JSON objects from one table view to another table view

user3283997

This is the structure of my JSON data

[
{
$id: "1",
Speeches: [],
Id: 1,
Year: "1999"
},
{
$id: "18",
Speeches: [],
Id: 2,
Year: "2000"
},
{
$id: "49",
Speeches: [],
Id: 3,
Year: "2001"
},

The structure of the speech object is as follows

Speeches: [
{
$id: "2",
Year: {
$ref: "1"
},
Id: 4,
YearId: 1,
Title: "الكلمة السامية بمناسبة زيارة قيادة الحرس الوطني تاريخ 30 مارس 1999",
HeaderOne: "بسم الله الرحمن الرحيم",
HeaderTwo: null,
Body: "أيها الأخوة والزملاء . . يسرني في هذا اليوم أن ألتقي بكم ، ولقد أردت بهذه الزيارة أن أرد لكم التحية على ما قمتم به .. وأظهرتموه في هذا الظرف الدقيق من تحاب وتكاتف كأسرة واحدة متراصة تسهر على سلامة الوطن وسلامة أهله والمقيميـن فيه في ظل دولة المؤسسات والقانون ..    وأن أشكر لكم مشاعركم تجاهنا في فقد والد الجميع أميرنا وقائدنا الراحل المغفور له سيدي صاحب السمو الشيخ عيسى بـن سلمان آل خليفة طيّب الله ثراه . . الذي كان بالفعل والد الجميع . . والذي جمعنا على الوئام والمحبة ، وعلى البذل والعطاء لهذا الوطن بلا حدود . . وفي ظل هذه المسيرة ، وتحت هذه الراية ومن هذا الموقع يطيب لي أن أوجه باسمكم جميعاً ، التقدير والتحية إلى الوالد العم العزيز صاحب
ImageName: "img787.png"
},
{
$id: "3",
Year: {
$ref: "1"
},
Id: 5,
YearId: 1,
Title: "الكلمة السامية الأولى بعد تولي مقاليد الحكم في البلاد تاريخ 13 مارس 1999م",
HeaderOne: "بسم الله الرحمن الرحيم",
HeaderTwo: null,
Body: "شعبنـا العزيـز …  السلام عليكم ورحمة الله وبركاته ،  قال تعالى : " من المؤمنين رجالٌ صدقوا ما عاهدوا الله عليه ، فمنهم من قضى نحبه ، ومنهم من ينتظر ، وما بدلوا تبديلا " صدق الله العظيم .   أما بعد :-  ففي هذا الموقف التاريخي يجمعنا واياكم المصاب الجلل في فقيدنا العظيم ووالدنا القائد ،    وتوحّدنا واياكم ، في الوقت ذاته ، مسؤولية التطلع بثـقة للمستقبل لمواصلة المسيرة على النهج الذي رسمه لنا من واسع حكمته وإخلاصه وسماحته ، هذا مع الاستعداد لمواجهة 
ImageName: "img002.png"
},

I need to display the years in one table view followed by the title of the speeches in the respective year in a second table view. I've modeled two data objects Year and Speech as follows:

@interface Year : NSObject

@property (nonatomic, strong) NSString *yearName;
@property (nonatomic, strong) NSArray *speeches;

@end

and

@interface Speech : NSObject

@property (nonatomic, strong) NSString *title;
@property(nonatomic,strong) NSString *header;
@property(nonatomic,strong) NSString *body;
@property(nonatomic,strong) NSString *image;

@end

Retrieving the data and displaying the works fine. This is how I'm doing it.

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *dataApi = [NSURL URLWithString:@"http://vbahrain.azurewebsites.net/api/yearapi"];

    NSData *jsonData =[NSData dataWithContentsOfURL:dataApi];

//    NSLog(@"%@", jsonData);

    NSError *error = nil;



    self.years = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    self.yearBucket = [NSMutableArray array];


    for (NSDictionary * dict in self.years) {

        Year *year = [[Year alloc ]init];

        year.yearName =[dict objectForKey:@"Year"];
        year.speeches = [dict objectForKey:@"Speeches"];

        [self.yearBucket addObject:year];
    }

}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    Year *year = [self.yearBucket objectAtIndex:indexPath.row];


    cell.textLabel.text = year.yearName;


    cell.detailTextLabel.text = [NSString stringWithFormat:@"%lu", [year.speeches count]];

    return cell;
}

Now I'm trying to pass the data to the next View controller using the prepare for segue method as described below

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].

    TitleViewController *tvc = [segue destinationViewController];

    // Pass the selected object to the new view controller.

    NSIndexPath *path = [self.tableView indexPathForSelectedRow];

    Year *selectedYear = self.yearBucket[path.row];


    for (NSDictionary *dict in selectedYear.speeches){


        Speech *speech = [[Speech alloc ]init];

        speech.title =[dict objectForKey:@"Title"];
        speech.header = [dict objectForKey:@"HeaderOne"];
        speech.body = [dict objectForKey:@"Body"];

      [tvc.speechesForSelectedYear addObject:speech];
}

Now this is the method I'm using to display this data passed in the speechesForSelectedYear Mutable array. Here's first the header of the other table view

@interface TitleViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *speechesForSelectedYear;

@end


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

    // Configure the cell...

    Speech *speech = [self.speechesForSelectedYear objectAtIndex:indexPath.row];

    cell.textLabel.text = speech.title;


    return cell;
}

However the second table view is displaying nothing. Where am I going wrong?

drolando

Try to add in the class of your second table:

@synthesize speechesForSelectedYear=_speechesForSelectedYear;

-(NSMutableArray*)speechesForSelectedYear
{
    if (!_speechesForSelectedYear)
        _speechesForSelectedYear = [[ NSMutableArray alloc] init];
    return _speechesForSelectedYear;
}

The problem may be that the speechesForSelectedYear array is not allocated when you call [tvc.speechesForSelectedYear addObject:speech]. With this function you override the getter of the property so you're sure that wherever you use it the array exists.

In Objective-c if the array is nil you will not get an error or an exception but nothing'll happen.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing data from one Backbone view to another

From Dev

Passing argument from view of one resource to create method of another with Laravel

From Dev

passing a string from a table view controller to a new view controller

From Dev

Passing a table from one lua state to another

From Dev

passing product info from one view to another

From Dev

How to SUM from another table in SQL in one view?

From Dev

Passing values from one view controller to another in Swift

From Dev

Passing a file from one view to another in Django using sessions

From Dev

MVC Charts - Passing data from one view to another cshtml

From Dev

Passing a "User" object from one view controller to another

From Dev

Passing temp table from one execution to another

From Dev

Passing Core Data objects from DetailViewController to another View Controller

From Dev

Passing object from one view to another in django via HttpResponseRedirect

From Dev

One Table View from Two API's

From Dev

Passing ID from one view to another and storing it in another table

From Dev

How to pass data from one table view to another table view in Swift?

From Dev

Passing a table from one lua state to another

From Dev

Passing data in segue from plist. Table view to Details View

From Dev

Retrieving objects from parse into table view cell

From Dev

Can't get one a table view data from another view controller

From Dev

Passing joined table to razor view

From Dev

How to access cells of a table view of a view from another view in Swift?

From Dev

Passing Variable From One View to Another View - Laravel 5.1

From Dev

Passing Core Data objects from UITableViewCell to another View Controller

From Dev

How do I pass information from a table view cell in one view controller to another view controller?

From Dev

How to refresh a table view from another view controller iOS?

From Dev

Passing Context From One Django View To Another

From Dev

One cell table view

From Dev

Passing data from table view to view controller

Related Related

  1. 1

    Passing data from one Backbone view to another

  2. 2

    Passing argument from view of one resource to create method of another with Laravel

  3. 3

    passing a string from a table view controller to a new view controller

  4. 4

    Passing a table from one lua state to another

  5. 5

    passing product info from one view to another

  6. 6

    How to SUM from another table in SQL in one view?

  7. 7

    Passing values from one view controller to another in Swift

  8. 8

    Passing a file from one view to another in Django using sessions

  9. 9

    MVC Charts - Passing data from one view to another cshtml

  10. 10

    Passing a "User" object from one view controller to another

  11. 11

    Passing temp table from one execution to another

  12. 12

    Passing Core Data objects from DetailViewController to another View Controller

  13. 13

    Passing object from one view to another in django via HttpResponseRedirect

  14. 14

    One Table View from Two API's

  15. 15

    Passing ID from one view to another and storing it in another table

  16. 16

    How to pass data from one table view to another table view in Swift?

  17. 17

    Passing a table from one lua state to another

  18. 18

    Passing data in segue from plist. Table view to Details View

  19. 19

    Retrieving objects from parse into table view cell

  20. 20

    Can't get one a table view data from another view controller

  21. 21

    Passing joined table to razor view

  22. 22

    How to access cells of a table view of a view from another view in Swift?

  23. 23

    Passing Variable From One View to Another View - Laravel 5.1

  24. 24

    Passing Core Data objects from UITableViewCell to another View Controller

  25. 25

    How do I pass information from a table view cell in one view controller to another view controller?

  26. 26

    How to refresh a table view from another view controller iOS?

  27. 27

    Passing Context From One Django View To Another

  28. 28

    One cell table view

  29. 29

    Passing data from table view to view controller

HotTag

Archive