how to show dynamically added cell selected in uitableview - uitableview

I'm adding cell into uitableview on some action event. The cell is added at the top position in uitableview. I want the newly added cell to be selected by default.

Just call the method selectRowAtIndexPath:animated:scrollPosition: of the table view after the cell is added, assuming the cell is added in index 0 of section 0:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewRowAnimationTop]

Related

How to change specific element in row without reloading row in Objective-C?

I want to update a UiSwitch in a row in UITableView. I want to make it on and off with animation programmatically.
NSIndexPath *indexPath = [[NSIndexPath indexPathForRow:0 inSection:lastSelectedCardSectionNumber] retain];
newCell *cell = (newCell *)[self.myTableView dequeueReusableCellWithIdentifier:#"newCell" forIndexPath:indexPath];
[cell.switch1 setOn:YES animated:YES];
but the above line not changes the state and I have to reload row which doesn't make UiSwitch state change with animation.
There is a view on top of the switch which will user taps on it to change the state of switch. but it should be change just in some situations. Therefore I don't allow user to change it I want to change it programmatically.
The problem is with how you are retrieving the cell. It should be:
newCell *cell = (newCell *)[self.tableView cellForRowAtIndexPath:indexPath];
not this:
newCell *cell = (newCell *)[self.myTableView dequeueReusableCellWithIdentifier:#"newCell" forIndexPath:indexPath];
The former gets the already existing instance of cell but the later one creates the new instance of the cell.

Load more to last position in UITableView

I try to Build UITableView That load every time a 5 object and when I notice that the scroll table in the last position I reload table data and this open the table from the top again.
how I can save the position and when table reload back to last cell I see ?
Use the below code before you reload the table data
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRowsInLastSection - 1) inSection:(numberOfSections - 1)];
By the above line you will get the position of last row of table.
Use this code after reloading the tableView
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
This will scroll to already saved position.
I find a solution in this post - UITableView , Scroll to bottom on reload?
[postTableView reloadData];
NSIndexPath* ipath = [NSIndexPath indexPathForRow: oldSkip -1 inSection: 1];
[postTableView scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionTop animated:YES];

iOS7: Tableview doesn't load accessory changes

So I got this method that I call on didSelectRowAtIndexPath:
- (void)selectTableViewRow:(int)row {
[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] setAccessoryType:UITableViewCellAccessoryNone];
[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]] setAccessoryType:UITableViewCellAccessoryNone];
[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]] setAccessoryType:UITableViewCellAccessoryNone];
[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:1]] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
(It's only three cells so no for-loop)
It works perfectly fine. When a cell gets selected, there appears a checkmark and the others get removed. But when I call this method in viewDidLoad nothing changes graphicly.
Do I have to reload the table view after setAccessoryType?
This is because your tableView will reload its data after -viewDidLoad is completed. This means that your table view will call -tableView:cellForRowAtIndexPath: in your table view data source, and it will use whatever cell accessory is specified there.
You should specify your cell accessories in tableView:cellForRowAtIndexPath:
At viewDidLoad time, your cells won't get created until they're needed. You have to be careful here, as cellForRowAtIndexPath, if done properly can generate entirely new cells, and throw out ones you already created. You should move your code for setAccessoryType into your cellForRowAnIndexPath, and have it check to see if each cell should be selected based on a variable which is set to the currently selected row and compare it to the indexPath.row.
The issue is that iOS can toss and reuse cells. If you set cell 1's accessory view in your routine, it might get scrolled off the end, and reused elsewhere, say now as cell 7. So cell 7 has the checked accessory view, and cell 1 is off screen.
In other words, cellForRowAtIndexPath feeds cells to the tableView. It's up to you to set their contents correctly based on the indexPath you get called with, especially if you reuse the cell - you have to clear the accessory view or set it for every cell that's called.

Handling Custom TableViewCell's(created in .xib with autolayout) Selections

I have custom UITableViewCell which has button that when clicked reveals a checked box.
Now I am creating this in xib and using a tableView to load it.
My problem is I need to set the selection state of the cell to true.
But when I do that reused cells lose selection property.
Is there any way to force the tableView to accept the cells selection?
Try this:
NSIndexPath *idxPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:idxPath animated:YES scrollPosition:UITableViewScrollPositionNone];

iOS scroll to a section in a UITable View

does anyone know how to programatically scroll a table view to a section at index path. what i'm trying to do is have tableview with 2 sections and an if statement for example
if loading = true scroll the table view up to hide the first section
when finished loading scroll table view back down to show first section
Here is the snippet:
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

Resources