UITableView populating help

Discussion in 'iPhone Tips, Help and Troubleshooting' started by LARRYSE, Aug 22, 2012.

  1. LARRYSE macrumors newbie

    Joined:
    Aug 22, 2012
    #1
    Hi,
    I am new at objective c and am trying to populate a tableview. I have an object class and a object controller class as well as my uitable. my object class has 1 property, my object controller class creates objects and puts them into a list. my ui gets this list a populates the table with the object property, or so the theory goes. i would like to expand this out to a data class where i can keep the data in a separate layer. currently my table does not fill.

    here is some of my code.

    any help is appreciated.

    ============ Object class ===============
    #import <Foundation/Foundation.h>

    @interface Category : NSObject
    {
    NSString *name;
    }

    @property (copy, nonatomic) NSString *name;

    -(void) print;

    @end


    #import "Category.h"

    @implementation Category

    @synthesize name;
    -(void) setName: (NSString *) theCategory
    {

    name = [[NSString alloc] initWithString: theCategory];
    }

    -(void) print
    {
    NSLog(@"Category name: %-31s",[name UTF8String]);
    }

    @end



    ================ Object controller class ==================
    #import <Foundation/Foundation.h>
    #import "Category.h"

    @interface Categories : NSObject
    {
    NSString *categoryName;
    NSMutableArray *categoryList;
    NSMutableArray *testList;
    }

    -(id) initWithName: (NSString *) name;
    -(void) addCategory: (Category *) theCategory;
    -(int) enteries;
    -(void) list;
    -(NSMutableArray *) getList;
    -(void) testList;
    //-(void) dealloc;

    @end



    #import "Categories.h"

    @implementation Categories;

    - (id) initWithName: (NSString *) name
    {
    self = [super init];
    if (self)
    {
    categoryName = [[NSString alloc ] initWithName: name ];
    categoryList = [[NSMutableArray alloc] init ];
    }

    return self;
    }

    -(void) addCategory:(Category *)theCategory
    {
    [categoryList addObject: theCategory];
    }

    -(int) enteries
    {
    return [categoryList count];
    }

    -(void) list
    {
    NSLog(@"======= Contents of: %@ =======", categoryName);

    for (Category *theCategory in categoryList)
    NSLog (@"%-20s", [theCategory.name UTF8String]);

    NSLog (@"=======================================");
    }

    -(NSMutableArray *) getList
    {
    if([categoryList count] == 0)
    {
    [self testList ];
    for (int i =0; i < [testList count]; i++)
    {
    Category *cat = [[Category alloc] init];
    [cat setName: [testList objectAtIndex: i]];
    [categoryList addObject: cat];
    }

    }
    return categoryList;

    }
    -(void) testList
    {
    testList = [[NSMutableArray alloc] init];
    [testList addObject: @"item1"];
    [testList addObject: @"item2"];
    [testList addObject: @"item3"];
    [testList addObject: @"item4"];
    [testList addObject: @"item5"];

    }
    @end



    ========================== UI screen with table view ====================
    #import <UIKit/UIKit.h>


    @interface FirstViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
    {

    NSMutableArray *categoryList;
    }

    @property (nonatomic, retain) NSMutableArray * catList;


    @end




    #pragma - Tableview Methods

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return catList.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //Step 1: Check to see if cell row can be reused
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:mad:"cell"];

    //Step 2: If there are no cells to reuse create a new one
    if (cell == nil)
    {
    cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:mad:"cell"];
    }


    //Add a detail view accessory
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    //Step 3: Set text

    // cell.textLabel.text = ([categoryList valueForKey:mad:"name"]);
    [self getList];

    cell.textLabel.text = [categoryList valueForKey:mad:"name"];
    // cell.textLabel.text = [categoryList objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:17];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

    //Step 4: return index row
    return cell;
    }

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    CGSize size = [[catList objectAtIndex:indexPath.row]
    sizeWithFont:[UIFont systemFontOfSize:17]
    constrainedToSize:CGSizeMake(200,CGFLOAT_MAX)];

    return size.height + 10;
    }

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {

    cell.backgroundColor = (indexPath.row%2)?[UIColor lightGrayColor]:[UIColor clearColor];
    }
     
  2. LARRYSE thread starter macrumors newbie

    Joined:
    Aug 22, 2012
    #2
    opps wrong area..

    i have reposted in correct area
     

Share This Page