iOS Populate a view from an array

PlutoM4

macrumors newbie
Original poster
Apr 11, 2014
10
0
Hi all! I would ask for your help. I'm developing an app that has:

A. Item.h
Code:
@interface Items : NSObject
 
@property (nonatomic, strong) NSString *categoryName; // category's name
@property (nonatomic, strong) NSString *categoryImage; // image showed on the left of the table row
@property (nonatomic, strong) NSString *tipTextField; // text displayed in a single tip
B. MainTableViewController.m
Code:
#import "Items.h"
 
#import "CategoryTableViewCell.h"
 
{
    NSArray *categoryArray;
 
    NSArray *tipsArray;
}
 
// Initialize items of category1
    Items *tip1 = [Items new];
    tip1.tipTextField = @"text goes here";
 
//Create tips array
 
    tipsArray = [NSArray arrayWithObjects: tip1, tip2, ..., nil];
 
// Initialize the category array
    Items *category1 = [Items new];
    category1.categoryName = @"Category1";
    category1.categoryImage = @"image.png";
    category1.tipTextField = tipsArray [0];
    
 

//ADDED TEXT AFTER EDITING POST
categoryArray = [NSArray arrayWithObjects: category1, category2, category3, category4, category5, category6, category7, category8, nil];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];
        
    } else {
        return [categoryArray count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomTableCell";
    CategoryTableViewCell *cell = (CategoryTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[CategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // Display category in the table cell
    Items *item = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        item = [searchResults objectAtIndex:indexPath.row];
    } else {
        
        item = [categoryArray objectAtIndex:indexPath.row];
    }
    
    cell.nameLabel.text = item.categoryName;
    cell.thumbnailImageView.image = [UIImage imageNamed:item.categoryImage];
    
    return cell;
}

//Set row height at 52
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 52;
}

//View details of selected category
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showTipDetail"]) {
        NSIndexPath *indexPath = nil;
        Items *item = nil;
        
        if (self.searchDisplayController.active) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            item = [searchResults objectAtIndex:indexPath.row];
        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            item = [categoryArray objectAtIndex:indexPath.row];
            
        }
        
        CategoryDetailsViewController  *destViewController = segue.destinationViewController;
        destViewController.item = item;
    }
}
Simply I want that a CategoryDetailsViewController shows tipTextField. So, in CategoryDetailsViewController.h I set:
Code:
#import "Items.h"
 
@property (weak, nonatomic) IBOutlet UITextView *tipTextField;
 
@property (nonatomic, strong) Items *item;
and in CategoryDetailsViewController.m:
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
       
    tipTextField.text = self.item.tipTextField;
but when from a row I want to see detailed text of category (i.e. tip1 for Category1) my app crashes for [super viewDidLoad], with [NSObject(NSObject) doesNotRecognizeSelector:]:

I would achieve that user taps on a row (that is a category) and a DetailsViewController appears. Here, with "previous" and "forward" buttons he can navigate through items for each category.

Any help? Feel free to ask for more code if needed, thanks in advance!
 
Last edited:

DannyBres

macrumors 65816
Oct 30, 2007
1,413
6
UK
Quite confusing, seems a strange data model.

From your code. you create an NSArray of Items (which has instance variables of categoryName, categoryImage, tipTextField) with only their tipTextField populated called tipsArray.

You then create a new item called category1 set the other instance variables categoryName and categoryImage and then try and set the tipTextField instance variable of category1 to tipsArray [0];, you cannot do this as tipsArray [0] returns an instance of Items not an NSString.

Why have you set your model up like this? does an item have an image a category and tiptextfield?
 

PlutoM4

macrumors newbie
Original poster
Apr 11, 2014
10
0
Quite confusing, seems a strange data model.

From your code. you create an NSArray of Items (which has instance variables of categoryName, categoryImage, tipTextField) with only their tipTextField populated called tipsArray.

You then create a new item called category1 set the other instance variables categoryName and categoryImage and then try and set the tipTextField instance variable of category1 to tipsArray [0];, you cannot do this as tipsArray [0] returns an instance of Items not an NSString.

Why have you set your model up like this? does an item have an image a category and tiptextfield?
Hi Danny, thanks for your help.

This data model originate from a mix of tutorial, q&a and Obj-C studying.

I agree with you, it's so confusing but take it as my sandbox to test and learn.

I will study your answer and I ask for an alternative approach to question, that is:

1. several categories with name and an image for TableView;
2. several text items for each category, being displayed one-by-one in a CategoryDetailsView's text field;
3. two button (Previous and next) to navigate among items of each category.

Thanks again!:)
 

DannyBres

macrumors 65816
Oct 30, 2007
1,413
6
UK
Need to think of a scheme.

Sounds like you have

Category
NSString *Name
NSImage *Image (might be best not to sort the images in here, bad memory management practice, only load when needed)
NSArray *tips
 

PlutoM4

macrumors newbie
Original poster
Apr 11, 2014
10
0
Need to think of a scheme.

Sounds like you have

Category
NSString *Name
NSImage *Image (might be best not to sort the images in here, bad memory management practice, only load when needed)
NSArray *tips
I have just updated code of MainTableViewController.m, hope this help.

I'm becoming so confused, I hope to figure out.. :confused:
 

DannyBres

macrumors 65816
Oct 30, 2007
1,413
6
UK
How are you learning? From your Item object, i am concern you have not quite grasp some of the OOP concepts.
 

PlutoM4

macrumors newbie
Original poster
Apr 11, 2014
10
0
How are you learning? From your Item object, i am concern you have not quite grasp some of the OOP concepts.
You're right, I'm a newbie and I'm studying from tutorial and from a book called "Programming in Objective C".

I study in my spare time and from few days, so it's possible I haven't still some fundamentals.

Thanks anyway for your time and for answers, I will try to study harder.

:(