iOS static table view not so static - objective-c

I'm developing a static grouped table view with storyboard. Anyway, sometimes I'd like not to display some of the rows. As long as the rows I want to hide are in the middle, I can set the height of that specific cell to 0 and the trick works :)
Anyway, when I want to hide the last cell of a group (I'm using a grouped table view), my trick obviously does not work. That's because the previous cell won't get the round corners typical of the last cell. Is there another trick that can help me with this issue?

I solved my problem implementing the method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
even if I'm using a static table view.

The proper way to remove a cell from a table view after it has been rendered is to use the UITableView method called deleteRowsAtIndexPaths:withRowAnimation: . Here is a code sample:
[tableView deleteRowsAtIndexPaths:#[myCellIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

Related

how to remove accessory from cellForRowAtIndexPath

i have the indexPath number... i need to remove the cell accessory from that cell based on the number in the variable i have?
NSInteger *removeAccessoryFrom = 3;
[studentTableView cellForRowAtIndexPath:removeAccessoryFrom].accessoryType = UITableViewCellAccessoryNone;
It doesn't work like that. You need to reload the table data (either for the whole table or for just this row) and, inside cellForRowAtIndexPath, set the accessoryType to UITableViewCellAccessoryNone conditionally. In other words, you keep a list, or have some other way of knowing, at every moment, and for the entire table, exactly what cells get exactly what accessory. That way, any time cellForRowAtIndexPath is called, you can configure this cell for this index path correctly.

Save temporary values deleted by reloadData in a UITableView

I have a simple project with UITableViewCell, inside the Cell I have a UITextField, In my case I am creating rows of a table dynamically, ie:
When I call function 'AddField' There is a counter that is responsible for storing the number of rows that the table must have.
-(void)AddField{
numberOfRows++;
[tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return numberOfRows;
}
So, if I type something in the first text field and then call the function to add more rows in the table, the row is added, another text field appear (leaving two), but the text entered in the first text field disappears due the command reloadData.
I Need help find the best way around this problem, any suggestions?
Have a Mutable array to store all the typed values.
[self.myMutableArray addObject:cell.textField.text];
In cell for Row at index path, get the text and set it back to the text field.
cell.textField.text = self.myMutableArray[indexPath.row]

how to identify which cell in tableview clicked

I created tableview that read from url (any cell has name of book) and when clicked any cell gone detailView.
in detail view exist UIScroll that read any image from url and I create it.
but my problem this place. I don't know what read images in UIScroll related any cell.
this is URL : ".../mamal/book.php?p=%d&b=%d"
(p and b display number page and number book)
I want when clicked first cell (book1) compiler gone next page and display images related book1
and etc.
You have to use UITableView protocols <UITableViewDataSource,UITableViewDelegate> in your .h file then use below delegate method which you give which cell is clicked
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Clicked cell-> %d", indexPath.row);
}
You can also identify the currently selected row using the property indexPathForSelectedRow.
i.e if you want to know the index path of currently selected row at any point of time without using the delegate methods (not saving it during selection) you can use
tableView.indexPathForSelectedRow
If you want to pass around the value between multiple view controller you can declare an instance variable in the view controller you want the value to be passed and store tableView.indexPathForSelectedRow in this variable during initialization. A quick search on so should help you with that.

How to detect how many cells are visible in a UITableView

My table view covers part of the view. If I have 20 objects, I am displaying all of them in a tableview. But I want to know that how many cell are loaded that are visible to the user.
(i.e. first 5 cells data is visible for me: when I scroll down, the remaining cells will load. Here I want to know without scrolling how many cell are loaded.)
Is this possible?
You can use [tableView visibleCells] count], which returns numbers of the table cells that are visible, if I understand correctly what you want.
The below code will give you the array of indexpath of cells currently visible
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
after getting the index path you can easily access the cell at a particular indexpath
Its usually : (tableView's height / cell row height ) + 1
Can you take the size of the visible part of the table, and then the size of one cell, and divide them to know how many of them fit in the screen?

Can I programmatically modify which UITableViewCell is selected?

Is there a way to move the highlighted tableviewcell programatically. If I wanted to highlight cell number 3 and then highlight cell number 4 instead, based on something the user did in the detail view of my split view controller, can I do this?
Is there a way I can get the indexpath for the row two below one that I have?
Thanks!
You can do this via the UITableView selectRowAtIndexPath:animated:scrollPosition: method.
In terms of getting the "next" row from the indexPath, you can simply use the indexPath.row property (as supplied from your UITableViewDelegate's tableView:didDeselectRowAtIndexPath: method) as the basis for the selected row.