I have a static tableview that shows a date picker when the appropriate cell is selected. The didSelectRowAtIndexPath triggers the show/hide of the date picker cell; however, it's not until the second selection that scrollToRow is executed and the picker cell is hidden.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
if (cell == self.dateCell){
self.dateOpen = !self.isdateOpen;
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
[self.tableView reloadData];
[self.tableView endUpdates];
}
Any Ideas of where I've gone astray??
Thank You!!
EDIT: I forgot to add, it's the last row that's expanding with the date cell... I'm wondering how that would affect it. Although, I did just copy/paste a few cells to make it not the last row and it didn't help.
Related
I have a UITableView which consist of Chapters and Subchapters. On selecting Chapters the respective Subchapter will expand in UITableView. When I click the last Chapter the subchapter is not visible in the screen. Have to scroll below automatically when the subchapters appear.
You can either use
[self.table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
Or use
CGPoint bottomOffset = CGPointMake(0, yourTableView.contentSize.height - yourTableView.frame.size.height);
[table setContentOffset:bottomOffset]
This will scroll the tableView to the bottom when a new cell will be inserted to your tableView
On didSelectRowAtIndexPath method you need something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
CGRect sectionRect = [tableView rectForSection:indexPath.section];
[tableView scrollRectToVisible:sectionRect animated:YES];
...
}
Hope it helps.
I would like to make something like expandable cells. Result I'm trying to achieve is:
Actually I have:
Here is my code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
DoctorCell *cell = self.prototypeCell;
DoctorModel *doctor = [self.fetchController objectAtIndexPath:frcIndexPath];
// depending on isActive property I add (or do not add) some content.
[cell cellForDoctor:doctor isActiveCell:[self.activeCellIndexPath isEqual:indexPath]];
[cell setNeedsUpdateConstraints];
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height + 1;
}
Here is my cell selection method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSIndexPath *oldIndexPath = self.activeCellIndexPath;
if ([indexPath isEqual:self.activeCellIndexPath]) {
self.activeCellIndexPath = nil;
oldIndexPath = nil;
} else {
self.activeCellIndexPath = indexPath;
}
[self.doctorTableView beginUpdates];
[self.doctorTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, oldIndexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.doctorTableView endUpdates];
[self.doctorTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
As you can see, my animation looks weird (kind of moving to wrong position, jumps, etc). What I'm doing wrong? Or do I need to pick another approach for expanding cells?
You can use UITableViews animation automation like this:
Call [tableView beginUpdates];
Change the data source.
Insert or delete rows and sections, or reload them
This should cause new heights to be calculated, etc.
Make sure you use something appropriate for the withRowAnimation parameter of these methods
Call [tableView endUpdates];
Your animation should now perform as expected.
Maybe your problem is that you selected UITableViewRowAnimationNone for the row animation.
I have a property that is an image in a cell expands,when the pulse in the cell assigned a selectedIndexPath you change the height in the corresponding method , and I want that image to change when the cell is expanded, the problem is that when I expand the cell changes but the image is returned to the previous one that happens when I throw animation to expand the cell is repainted.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
IACCell *cell = (IACCell *) [tableView cellForRowAtIndexPath:indexPath];
if(!self.selectedIndexPath||![self.selectedIndexPath isEqual:indexPath]){
self.selectedIndexPath=indexPath;
tableView.scrollEnabled = NO;
cell.imageView.image =[UIImage imageNamed:#"image"];
}else{
self.selectedIndexPath=nil;
tableView.scrollEnabled = YES;
}
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
the cell.imageView.Image changes briefly and the problem I think is in [tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];,
anyone can help me please?
Thanks!
I am desperately trying to achieve the following effect: I have got a tableview where I would like to expand the cells on selection and show some additional information on it. I reuse table cells, just FYI. In the past I was able to achieve such an effect in another application, but this just does not seem to work for me at all. All the methods that are vital for this to happen are called and the row expands beautifully, but the additionalInfo table in the showExpandedContents method just will not appear!
If I create the additionalInfo table in the initialisation of the cell, the table will appear but when I then try to load the data (and by that, also the cells) it just won't reload the table.
But the method that is responsible for this (showExpandedContents) table is definitely called.
If I initialise AND set up the table in the initialisation code of the cell everything works fine.
So here is some code for you:
Here is the cell setup:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = #"MyCustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
((MyCustomCell*)cell).data = [arrayToDisplay objectAtIndex:indexPath.row];
return cell;
}
Here is the selection method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//setting the current cell as the expandedRow, so that I can collapse it later
expandedRow = [tableView cellForRowAtIndexPath:indexPath];
[((MyCustomCell*)expandedRow) showExpandedContents];
[tableView beginUpdates];
[self changeRowHeightAtIndex:indexPath.row height:330];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
Here is the -showExpandedContents method in MyCustomCell where I would like to display the additional information:
-(void)showExpandedContents{
additionalInfo = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, self.frame.size.width, 250)];
[additionalInfo loadContent];
additionalInfo.backgroundColor = UIColorFromRGB(0xf9f9f9, 1.0);
additionalInfo.layer.zPosition = MAXFLOAT;
[self.contentView addSubview:additionalInfo];
}
Looks like your
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
is giving you a fresh cell, different than the one that had showExpandedContents called.
Removing this line should fix the problem.
Can someone tell me how to move a row in UITableView automatically when selected.
To be more clearer my tableview has a number of items. When the users selects a row that row has to be moved to the bottom most row in UITableView.
Any code snippets or pointers would be appreciated.
Thanks
Use a method from UITableViewDelegate protocol
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger index = indexPath.row;
NSUInteger lastIndex = tableDataArray.count - 1;
if (index == lastIndex) {
return;
}
id obj = [[tableDataArray objectAtIndex:index] retain];
[tableDataArray removeObjectAtIndex:index];
[tableDataArray addObject:obj];
[obj release];
//// without animation
[tableView reloadData];
//// with animation
// [tableView beginUpdates];
// [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
// [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastIndex inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
// [tableView endUpdates];
}
You can use the table view delegate methods to perform this.
To obtain the selected row u can use
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This will help you to get an idea on moving rows.
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/ManageReorderRow/ManageReorderRow.html
when the row is selected modify your datasouce ,may be your array and reload table view.
ie
in your
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
get you indexpath and remove that data from your datasource array and place that value in your last of your data source array and finally reload tableview using
[tableview reloadData];