How to populate a tableview with an array of objects?

I have a TableView controller that I want to populate with objects from an array. I am using StoryBoard. Also, I'm not sure if I need to put shortcuts in the cell prototype on the storyboard as a placeholder type?

My aList Mutable Array contains objects of type Homework. On each row of the table I want to display (these are the variables already set in my home model):

-ClassName

-AssignmentTitle

-DueDate

That's what i currently

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; } 
+6
source share
3 answers

I have a similar implementation.

Implementation: I created the following methods. (I edited them according to your code.)

 -(Homework*) homeworkAtIndex: (NSInteger) index { return [alist objectAtIndex:index] ; } -(NSInteger) numberOfObjectsInList { return [alist count]; } 

And in the delegate method of the UITableViewController:

 - (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 homeworkAtIndex method will allow you to populate the various objects in the array into different cells of the table.

Thus, I was able to create custom cells and format cell sizes to fit the table. Perhaps this may work for you if the data to be displayed is not that long and you really don't need to use custom cells. (As in the example I presented)

If you are interested in how to check if the selected cells are selected (since after that you can click them on another viewController), 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 use NSString here to check the class Name, because I'm not sure what the Homework object is, but you can definitely change the logic to match the Homework object, since in my opinion you can have different objects with the same className.

Hope this helps! :)

+7
source

There are many tutorials that populate tables using arrays. Take a look at these tutorials.

http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/

http://www.raywenderlich.com/1797/ios-tutorial-how-to-create-a-simple-iphone-app-part-1

you will need to implement a custom cell to place 3 shortcuts. By default, table cells usually have 1 image view and two labels.

+3
source

use custom cell

http://www.appcoda.com/customize-table-view-cells-for-uitableview/

use the cell as a view, just put your labels in the cell contents view [1] I can see that you already have an object with the data you want to display, so use a cell with the things you want to show

0
source

Source: https://habr.com/ru/post/956512/


All Articles