Programmatically highlight first row in tableview after first load - iphone

Im trying to highlight the first row in my table upon first loading the data and just cannot get it to work!
I have tried lots of ways with no joy, the latest attempt being this:
NSUInteger indexArr[] = {0,1};
NSIndexPath *n = [NSIndexPath indexPathWithIndexes:indexArr length:2];
[self.tableView scrollToRowAtIndexPath:n atScrollPosition:UITableViewScrollPositionTop animated:NO];

try
[self.tableView selectRowAtIndexPath:n animated:YES scrollPosition:UITableViewScrollPositionTop];
you should probably do this after your view has appeared.

Related

Change an image in a UITableViewCell without reloading cell

I'm writing an iPhone app with a UITableView as the primary user interface. Each section consists of two rows, a header and the body. When the user clicks on the header, I remove the second row by changing the numberOfRowsInSection value:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
cbwComponent *comp = [_componentController objectInListAtIndex:section];
if([comp hasContentsView] && !comp.contentsHidden){
return 2;
}else
return 1;
}
When the user selects the header, I'm using the following code:
comp.contentsHidden = YES;
[self.tableView beginUpdates];
NSArray *deleteIndexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:1 inSection:indexPath.section], nil];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
It's working great, with a nice smooth fade effect. The problem is, I'm trying to add an indicator in the header cell (row 0) that changes when it's clicked on. To change that image I have to refresh the top row as well as the second row, which makes the transition look bad (well, not nearly as smooth). Is there a way to change the image in a UITableViewCell without refreshing the cell?
Thanks
EDIT: I figured it out! You can maintain the smooth transition as long as you reload that first row before you make the change to the second row. It has to be called inside of [tableView beginUpdates];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:0 inSection:indexPath.section], nil] withRowAnimation:UITableViewRowAnimationNone];
...
[self.tableView endUpdates];
Did the trick.
You could also subclass a tableview cell and implement a view transition in it that can be called from your view controller. You could then call that without having to reload the cell.
[(YourCustomCell*)[tableView cellForRowAtIndexPath:indexPathOfYourCell] fadeInIndicator];

scrolling uitableview dynamically

I want to make a 'UiTableView' which scolls its cells dynamically to the down direction or up direction with and add the next cells to the tableview from the array.
Please suggest me good example over this.
Thanks Neha
here you go:
- (IBAction)clickAction:(id)sender
{
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
[tableView reloadData];
}
or :
// (change n to whatever you like)
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:n inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
if you need something more complex let us know.
In this snippet you can scroll and select row table even if rows has not been loaded.
- (void)viewDidAppear:(BOOL)animated
{
NSIndexPath *index = [NSIndexPath indexPathForRow:idx inSection:0];
[_table selectRowAtIndexPath:index animated:YES
scrollPosition:UITableViewScrollPositionMiddle];
}

Cannot get deleteRowsAtIndexPaths to work

I have a UITableView and a scheduler running in the background deleting records from the underlying data object.
Every time an object is deleted I want the table view to update accordingly.
But i keep getting this annoying error message when trying to delete a row from the table view.
* Terminating app due to uncaught exception NSInternalInconsistencyException, reason: "Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible."
The code:
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:0];
NSUInteger row = [indexPath row];
NSMutableArray* files = [[NSMutableArray alloc] initWithArray:fileNames];
[files removeObjectAtIndex:[indexPath row]];
[fileNames dealloc];
fileNames = [NSArray arrayWithArray:files];
//[self.fileNames removeObjectAtIndex:row];
[self.tableDraft deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
PS:
I have also tried [tableView reloadData]; but that has undesired side effects, erasing the selected index.
Have you considered following the message's advice? Your index path does not contain both a row and a section component. Use +[NSIndexPath indexPathForRow:inSection:] to create the index path object, so that it has the information the table view requires:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
You need to use this:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex
inSection:sectionIndex];
Use like this
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag-1 inSection:0];
CGRect rectOfCellInTableView = [objTableView rectForRowAtIndexPath:indexPath];
CGRect rectOfCellInSuperview = [objTableView convertRect:rectOfCellInTableView toView:[objTableView superview]];

Is there a way that a table view pops up when i select a row in a table?

Is there a way that a UITableView pops up when i select a row in a table?
thanks in advance.
In your UITableView delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.view addSubview:myView];
}
Here, myView is the view which you have IBOutlet.
like
in .h file
IBOutlet UIView *myView;
Let me know in case of any difficulty.
Cheers.
Yes there is a way on row selection method you have to fire up a method which creates auiview smaller in size as desired and in that view table view as subview.
Plz specify what type or kind of pop-up you want to appear.
OK, I understand now.
You should use table view's updating methods:
[myTableView beginUpdates];
NSArray *indexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:0 inSection:1],
[NSIndexPath indexPathForRow:1 inSection:1],
[NSIndexPath indexPathForRow:2 inSection:1],
[NSIndexPath indexPathForRow:3 inSection:1],nil];
[myTableView insertRowsAtIndexPaths:indexPaths withRowAnation:UITableViewRowAnimationTop];
[myTableView endUpdates];

Make UITableView go to top line

I sometimes reload table data and I need to redraw the table so that I see the data from the first row again. What I do now is the following:
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionTop ];
[self.tableView deselectRowAtIndexPath:ip animated:NO];
This is not ideal because there is an annoying flash when row 0 is selected and immediately deselected.
How can I fix this?
You can use UITableView's scrollToRowAtIndexPath:atScrollPosition:animated: method.
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
If you have a header that is in view by default, and want to compensate if the action is called when the list is in motion use:
[tableview setContentOffset:CGPointMake(0.0, 0.0) animated:NO];
Short, simple, effective.
You can set contentoffset of tableview to (0.,0.).