I have a TableView Controller that I want to populate with Objects from an array. I am using StoryBoard. Also, I am not sure if I need to place Labels in the Cell Prototype in storyboard as a type of placeholder?
My aList Mutable Array contains objects of type Homework. In each row of the table, I want to display (These are variables already set in my Homework Model):
-ClassName
-AssignmentTitle
-DueDate
Here is what I currently have
TableViewController.h
@interface AssignmentTableController : UITableViewController
<
AssignmentDelegate
>
@property(strong,nonatomic) Assignment *vc;
@property (strong, nonatomic) NSMutableArray *alist;//array was filled with delegation
@end
TableViewController.m
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.alist count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Homework *ai = [self.alist objectAtIndex:indexPath.row];
//*******I am not sure what to do from Here******
//*******I need to start displaying 1 object per row,displaying what was stated above***
// Configure the cell...
return cell;
}
I have a similar implementation.
Implementation: I created the following methods. (I have edited them to fit your code provided.)
-(Homework*) homeworkAtIndex: (NSInteger) index
{
return [alist objectAtIndex:index] ;
}
-(NSInteger) numberOfObjectsInList
{
return [alist count];
}
And in the UITableViewController delegate method:
- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section
I used this method call
return [self numberOfObjectsInList];
In the delegate method:
- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SubtitleIdentifier] autorelease];
Homework *ai = [self homeworkAtIndex: indexPath.row];
/* your other cell configurations
cell.textLabel.text = ai.className; // eg. display name of text
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ | %@",ai.assignmentTitle,ai.dueDate]; // will show the cell with a detail text of "assignment title | due date" eg. "Lab 1 | 23 Oct 2013" appearing under the className called "Physics"
*/
}
Using the method homeworkAtIndex will allow you to populate the different objects in the array into the different cells in the table.
This way got me through having to create custom cells and formatting the cell sizes to fit the table. Maybe this can work for you if the data that is going to be shown is not that long, and does not really have to use the custom cells. (Much like the example I provided)
If you were wondering how to check for different cells selected (as you may wish to push them to a different viewController thereafter), you can do this in the delegate method:
- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath
{
UIViewController* nextViewController = nil;
NSString* cellDisplayName = [delegate homeworkAtIndex: indexPath.row].name; // can be the Homework object if you want
if( [cellDisplayName isEqualToString:[self homeworkAtIndex:0].className] )
nextViewController = [[firstViewController alloc] init];
else if( [cellDisplayName isEqualToString:[self homeworkAtIndex:1].className] )
nextViewController = [[secondViewController alloc] init];
// goes on for the check depending on the number of items in the array
}
I am using NSString here to check for the className as I'm not sure what the Homework object is, but you can definitely change the logic to fit the Homework object, as my guess is you may have different objects with the same className variable.
Hope this helps! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With