How to populate the rootviewcontroller tableview with an NSMutableArray - objective-c

I am trying to populate the tableview with an NSMutableArray, and I am having a really hard time with it.
This should be simple I'm thinking, but I can't get it to work.
Any help would be appreciated.
Thanks

You simply have to create an object implementing the UITableViewDataSource.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.yourArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/* create your cell here */
AnObjectFromYourArray *myObject = [self.yourArray objectAtIndex:[indexPath row];
/* fill the cell with the info in my object */
return yourCell;
}
simple as that.
Greg

Related

how cellforrowatindexpath works in objective c [duplicate]

This question already has answers here:
How does cellForRowAtIndexPath work?
(3 answers)
Closed 5 years ago.
I am new to objective-c programming language.I create a table and create all method of table View .But i don't understand about CellForRowAtIndexPath.Please tell me some one how it work.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
This is delegate method of UITableView. The returned object is of type UITableViewCell. These are the objects that you see in the table's rows. NSIndexPath has two this Section and Row.
It is called if you implement the UITableViewDataSource protocol in your view controller. A simpler way would be to add a UITableViewController class. I strongly recommend this because it Apple has some code written for you to easily implement the functions that can describe a table. Anyway, if you choose to implement this protocol yourself, you need to create a UITableViewCell object and return it for whatever row. Have a look at its class reference to understand re-usablity because the cells that are displayed in the table view are reused again and again(this is a very efficient design btw).
If your implementing custom cell then I will strongly recommend you use
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
to return only empty cell not set here. use thing like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCart_Cell" forIndexPath:indexPath];
return cell;
}
and the use this delegate which will called just after cellForRow data source method
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell isKindOfClass:[CartTableViewCell class]])
{
CartTableViewCell *cell1 = (CartTableViewCell*)cell;
MyCartModel* data = [_myCartProductArray objectAtIndex:indexPath.row];
[cell1 setUpData:data];
}
}
and set data on UILabel in UITableviewcell custom class.

insertRowsAtIndexPaths inserts rows a

I'm trying to insert a row after the user has pressed return on the keyboard.
The code inside the textFieldShouldReturn method looks like this:
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:([self.tableView numberOfRowsInSection:([self.tableView numberOfSections] - 1)] - 1)
inSection:([self.tableView numberOfSections] - 1)];
[self.tableView insertRowsAtIndexPaths:#[lastIndexPath]
withRowAnimation:UITableViewRowAnimationRight];
This works fine until 10 rows are shown, after which a row is inserted at the beginning and then one and the end of the tableView.
How can I fix this?
I am pretty sure you have not updated your data. And one o both methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
of UITableViewDataSource returns wrong data.
UITableView only displays data. You should store it yourself somewhere.

tableView:cellForRowAtIndexPath: never invoked but rows and headers are > 0

A tableView in my UIViewController has as data source a NSMutableArray.
After I've added a new object to the mutable array, I invoke [tableView reloadData], which correctly invokes:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
returning the correct update amount of elements in the array. However the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
is not called afterwards. I thought when I reloadData of a table view such method was invoked.
The sections method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
always returns 1, in my code.
Thanks
What does the
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
method returns?
Have you managed it to return the count of your data source which is a NSMutableArray as you have mentioned?
In this method call :
[array count];
It was a thread issue... Ive fixed it with:
dispatch_async( dispatch_get_main_queue(), ^{
[table reloadData];
});

Dynamic Grouped Table Cells

In my storyboard, I have a UITableView and it has the following Properties
Style: Grouped
Content: Dynamic Prototypes
My question was how to make the different groups. When I run the app right now, there is no white backing (Only the default lined backing) with the text placed on top. Also, if I then created these dynamic groups, how would I set their header and footer?
Thanks
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// implement your code
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//implement your code
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/// Write basic code//
switch (indexPath.section) {
case 0:
//It belongs to first group and implement your code.
break;
case 1:
//It belongs to second group and implement your code.
break;
}
I think it will be helpful to you

How to manage creating a UITableViewCells?

Would you guyz be so kind to help me with such question:
I have to fill the tableView with cells and I also have an array with objects to fill cells with.
The question is - how to skip creating a cell if the object doesn't meet some conditions? I mean how to write something like:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(objectIsOk) {
//create cell
}
else {
//do nothing
}
return cell;
NOTE: I don't have an access to that array of objects it gives me an object per indexPath.row dynamically
I'm unsure as to what you mean by "skip creating a cell". Your table view data source is required to return a cell for each cellForRowAtIndexPath: call, which will get called for each row in each section that you have said the table will contain.
If you want to return a blank cell then why not just do something like this:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (objectIsOk) {
// Create normal cell
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"BlankCell"];
}
return cell;
}
You could also return height of zero in the heightForRowAtIndexPath: delegate method to make it appear that it's not there, like so:
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (objectIsOk) {
return 44.0f;
} else {
return 0.0f;
}
}