Dynamic tableview cells creation - objective-c

I have to make a tableview with different cells in it. I have three preferences and the table depends on them. There may be 6 different tableviews - 1 cell, 1cell and 3cell, no cell, 2 cell and 3 cell and so on, this depends on preferences
That's the best way to do this?
Maybe someone knows good example, or tutorial on this

You could make just one UITableView with different sections.
Based on your section id you may be returning different cell in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
indexPath contains row & section values.
Also you might return different number of rows in a section with:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//pass value to numper of cells;
return 4 (or) 2;
}
//use this to add cell
[tableview reloadData];

Related

How to always show tableView cell when i scroll

I need to always show cell with segmentControll, how can i do it?
There are a couple of ways to do this. Here is what I would do.
1
Don't put the segmented control as part of the table. Simply move it outside the UITableView. With that being said, if you are using a TableView Controller, you have to switch to a ViewController and add TableView to it.
2
This is a way to create a header for your table. Maybe that's what you want.
Use this method if you want a title:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Or this one if you want a custom view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
And for the height:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

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.

Get the number of section that contains current row

I my app I got an uitableview with 4 sections. In each section there are some cells that have to differ from cells in another section.
So in this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I make a variable NSInteger row = [indexPath row];
So that I get the number of a current row (cell). How can I get here the number of section that contains this row?
Its quite simple :)
NSInteger section = [indexPath section];

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

Remove unpopulated UITableViewCells?

Working with TableViews and have a two-parted question:
I have a TableView where there are only four cells with content. However the TableView continues down with about five or six more, empty, cells. Is there a way of removing these? What this would do, graphically, is remove all the lines below these 4 cells, while everything else stays the same.
Regard the following image, it has 2 populated cells and 7 extra lines below them, I want theses lines removed:
2.The four cells contain buttons. Because of this I want to remove the users ability to click/mark the entire cell. Is there such a "setting"?
Thank you, Tobias Tovedal
1.
- (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 4;
}
2.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You have to set 0 to the size of your footer to clear those "ghost" cells.
self.myTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
I am thinking of 2 ways to accomplish your first Q:
Set up TableView to be grouped and not plain (tableview will have round corners);
Make the separators color to match the background of the cells:
tableView.separatorColor = [UIColor whiteColor];// or whatever color you have
But you will have to manually add a line for each cells you are displaying.