Get the number of section that contains current row - objective-c

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];

Related

UITableViewController odd behavior

Sorry if this question look stupid but after hours of digging and searching with no success, I need to ask it here for you help.
I have a UITableViewController with 15 custom cell. Each cell has a UITextField inside.
here is the part of code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Static NSString *CellIdentifier = #"ADDFIELDCELL";
AddListContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AddListContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.myTextField setTag:[self.tagsArray objectAtIndex:indexPath.row] integerValue]];
return cell;
}
The problem is when I type something in first cell (indexPath:0) after scroll down same text appear on 13th cell also. same mapping happens with 2nd and 14th cell.
I am really out of temper and do not what should I do. Any help will be appreciated.
The cell is being reused (hence the reuse identifier).
The textfield text for each cell needs to be linked to the datasource somehow (seems to be your tags array).
When you change the text on that cell, you have to modify the datasource text.
This way when the cell at index 2 rolls around, it will fill the textfield with whatever your tag item at index 2 of your datasource array has.

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.

Is there any reason why we don't simply declare cellForRowAtIndexPath like this:

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
return cell.bounds.size.height;
}
What would be the disadvantage?
I changed that from
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.bounds.size.height;
}
As rmaddy points out, you can't use the first version because -[UITableView cellForRowAtIndexPath:] can cause the table view to send you tableView:heightForRowAtIndexPath: again, resulting in infinite recursion.
If you are using a static set of cells, with one cell preallocated for each row of the table, then the second version is fine.
If you are dynamically creating cells for rows, the second version will end up draining your table view's reuse queue and then creating another cell for every row, because tableView:cellForRowAtIndexPath: returns an autoreleased object. None of these cells will be deallocated until the end of the run loop, so in addition to the time cost of creating and destroying all of these cells, you're also using memory proportional to the number of rows in your table. If you want to do it that way, and you have a lot of rows, you might want to use an explicit autorelease pool:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
#autoreleasepool {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.bounds.size.height;
}
}
Normally you would want to get a cell from the table like you do in the 1st bit of code. But in this case you can't. If you try, you will end up with a recursive call between cellForRowAtIndexPath and heightForRowAtIndexPath.
If you must get a cell from the heightForRowAtIndexPath method, you must NOT ask the table for the cell.

Dynamic tableview cells creation

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];

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.