Become a MacRumors Supporter for $25/year with no ads, private forums, and more!
  • Did you order new AirTags? We've opened a dedicated AirTags forum.

moonman239

macrumors 68000
Original poster
Mar 27, 2009
1,530
22
I have a dynamic table view in a view controller that is set as both the table view's delegate and data source. I made sure to declare the subclass as a table view delegate and a table view data source. The table view is connected to the view controller's code via an outlet.

Here is the subclass's implementation code. To keep my app's code a secret, I have replaced some pieces of code with
Code:
 // removed
.
Code:
NSArray *managedObjects;
NSArray *attributes;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    attributes = @[// removed];
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:// removed inManagedObjectContext:[delegate managedObjectContext]];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        [request setEntity:entityDescription];
        NSError *error;
        managedObjects = [[delegate managedObjectContext] executeFetchRequest:request error:&error];
        if (managedObjects == nil)
        {
            NSLog([error localizedDescription]);
        }
    [[self // removed] reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [managedObjects count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[managedObjects objectAtIndex:section] valueForKey:// removed];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [managedObjects objectAtIndex:indexPath.section];
    return [managedObject valueForKey:[attributes objectAtIndex:indexPath.row]];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

EDIT: I got the error because in my tableViewCellForRow method, I have the returned variable set to something other than a table view cell. Doh!
 
Last edited:

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
Mark your thread as resolved

Since you found your problem, please mark this thread as resolved:

(See the thread Forum Feature: Resolved Thread Prefix for instructions.)

I have a dynamic table view in a view controller that is set as both the table view's delegate and data source. I made sure to declare the subclass as a table view delegate and a table view data source. The table view is connected to the view controller's code via an outlet.

Here is the subclass's implementation code. To keep my app's code a secret, I have replaced some pieces of code with
Code:
 // removed
.
Code:
NSArray *managedObjects;
NSArray *attributes;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    attributes = @[// removed];
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:// removed inManagedObjectContext:[delegate managedObjectContext]];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        [request setEntity:entityDescription];
        NSError *error;
        managedObjects = [[delegate managedObjectContext] executeFetchRequest:request error:&error];
        if (managedObjects == nil)
        {
            NSLog([error localizedDescription]);
        }
    [[self // removed] reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [managedObjects count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[managedObjects objectAtIndex:section] valueForKey:// removed];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *managedObject = [managedObjects objectAtIndex:indexPath.section];
    return [managedObject valueForKey:[attributes objectAtIndex:indexPath.row]];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

EDIT: I got the error because in my tableViewCellForRow method, I have the returned variable set to something other than a table view cell. Doh!
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.