iOS UIViewAnimation for uitableview cells - ios

Currently I have a table view that if a cell is selected, the table is reloaded with a cell at the selected index path using the animation UITableTableViewRowAnimationFade. I don't like it very much as it is choppy and not smooth. I am not at all familiar with UIView animations and am looking to learn more about them. How would I animate this operation to be smoother and look nicer? Thanks.
Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (selectedCellIndexPath == indexPath.row) {
selectedCellIndexPath = -1;
[callsTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
if (selectedCellIndexPath >= 0)
{
NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedCellIndexPath inSection:0];
selectedCellIndexPath = indexPath.row;
[callsTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
}
selectedCellIndexPath = indexPath.row;
[callsTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Related

Table view cell deletion animation not working

-(void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.myArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
Changing the animation type to any of the other options does not make a difference, I seem to get the same animation every time. I've read that this could be due to an iOS 7 bug, but my deployment target is iOS 8. Any ideas?
if (editingStyle == UITableViewCellEditingStyleDelete)
{
if ([self.myArray count] >= 1)
{
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.myArray removeObjectAtIndex:[indexPath row]];
[self.tableView endUpdates];
}
}
Try this
You can do like this way,
NSMutableArray *indexPathsToDelete = [NSMutableArray new];
for (Object *object in newObjects)
{
if (![currentObjects containsObject:object]) {
int row = [newObjects indexOfObject:object];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[indexPathsToDelete addObject:indexPath];
}
}
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];

to expand or collapse rows at different sections in iOS 7

I'm working on an application where i need to expand collapse rows in different sections.
Initially I tried expanding collapsing rows with a single section.
Now I want the same to happen in multiple sections.
here is my code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//Return the number of sections.
return [mysection count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *rate=[mysection objectAtIndex:section];
return [myrow count];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (selectedSection==indexPath.section) {
if(selectedIndex==indexPath.row){
selectedSection=-1;
selectedIndex=-1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
return;
}
//user taps on different row
if(selectedIndex!=indexPath.row){
NSIndexPath *prevPath=[NSIndexPath indexPathForRow:selectedIndex inSection:selectedSection];
selectedSection=indexPath.section;
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
else if(selectedSection!=indexPath.section){
selectedSection=indexPath.section;
[tableView reloadSections:[NSIndexSet indexSetWithIndex:selectedSection] withRowAnimation:UITableViewRowAnimationFade];
if(selectedIndex==indexPath.row){
selectedSection=-1;
selectedIndex=-1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
return;
}
//user taps on different row
if(selectedIndex!=indexPath.row){
NSIndexPath *prevPath=[NSIndexPath indexPathForRow:selectedIndex inSection:selectedSection];
selectedSection=indexPath.section;
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
selectedSection=indexPath.section;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
NSLog(#"%d",selectedSection);
}
only my rows should be expanded not my sections
Please provide with some possible solutions.
thanks in advance
my table view has multiple sections and my section has many rows
tapping on row in that section should expand
In didSelectRowAtIndexPath i get the index path to a variable that can be accessed throughout the coding as below...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedCellIndexPath = indexPath;
[tableView reloadData];
}
in Heightforrowatindexpath
if(selectedCellIndexPath != nil && [selectedCellIndexPath compare:indexPath] ==NSOrderedSame)
{
return height;
}

UITableView insert row at top crashes

I'm trying to insert a row at the top of UITableView, but it crashes in the last line where I declare the endUpdating. This is the code I'm using and I'm using a button outside the table to insert row.
[comments insertObject:comment atIndex:0];
[self.commentsTableView beginUpdates];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.commentsTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.commentsTableView endUpdates]; //here crashes with "assertion failure"
I know I can just do insert in array and reload, but I want to do it with animation.
Thank you in advance.
This is my cell for row method
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CommentCell";
// Similar to UITableViewCell, but
ListingCommentTableViewCell *cell = (ListingCommentTableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[ListingCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.commenterLabel.text = [[comments objectAtIndex:indexPath.row] objectForKey:#"user"];
[cell.commenterLabel sizeToFit];
cell.commentLabel.text = [[comments objectAtIndex:indexPath.row] objectForKey:#"text"];
[cell.commentLabel sizeToFit];
cell.lineView.frame = CGRectMake(5 , cell.commentLabel.frame.origin.y + cell.commentLabel.frame.size.height+ 5, cell.frame.size.width-20, 1);
cell.dateLabel.text = [Utils formatCommentDate:[[comments objectAtIndex:indexPath.row] objectForKey:#"createdOn"]];
return cell;
}
THis is my cell height method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int height = [Utils findHeightForText:[[comments objectAtIndex:indexPath.row] objectForKey:#"teksti"] havingWidth:screenWidth - 40 andFont:[UIFont fontWithName:#"Ubuntu" size:14]];
height += 25;
return height + 5;
}
and my number of rows method
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
return [comments count];
}
This is how I insert objects to UITableView with animation.
-(void)updateMethod {
[self.commentsTableView beginUpdates];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:0];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[comments insertObjects:[NSArray arrayWithObjects:comment, nil] atIndexes:indexSet];
[self.commentsTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.commentsTableView endUpdates];
}
You should check numberOfRowsInSection and be sure it reflect your comments.count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return comments.count;
}
Also all UI updates must be done on the main thread, if you call your update method in different thread there is a change you can see that error.
You should update data source array inside animation block:
[self.commentsTableView beginUpdates];
[comments insertObject:comment atIndex:0];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.commentsTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.commentsTableView endUpdates];

UITableView cell not deleting

Basically I have an app where you can swipe a tablecell off the screen and it "deletes" the cell. My delete method is called by this: (this happens when the cell reaches a certain point on the phone)
[tableCellTrash addObject: [NSIndexPath indexPathForRow:[numbers indexOfObject:[aPanGestureRecognizer view]] inSection:0]];
UITableViewCell *cell = [numbers objectAtIndex:[numbers indexOfObject:[aPanGestureRecognizer view]]];
[self deleteCell: cell];
This is the delete cell method
-(void)deleteCell: (UITableViewCell *) cell{
[self.tableView beginUpdates];
[numbers removeObject:cell];
[self.tableView deleteRowsAtIndexPaths:tableCellTrash withRowAnimation:UITableViewRowAnimationFade];
[tableCellTrash removeObjectAtIndex:0];
[self.tableView endUpdates];
//[self.tableView reloadData];
}
I have no idea what's wrong, the cell doesn't delete. Any help would be appreciated.
tableviews have a delegate method to handle the event for delete:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView reloadData];
}

UITableView correct animation

The matter is, I want to have a grouped table view, and when I click on a cell, I want to have an another cell to appear under it, and so on. Everything is fine with workflow, but I have same problems with animations. It works very slowly and I can see all changes
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"selected section:%d selected row:%d",indexPath.section,indexPath.row);
NSIndexPath* pathToDelete;
[_tableView beginUpdates];
if (_selectedPath != nil && [_selectedPath isEqual:indexPath]){
//If the same row was selected
pathToDelete = [NSIndexPath indexPathForRow:_selectedPath.row+1 inSection:_selectedPath.section];
_selectedPath = nil;
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
} else {
//If not
if (_selectedPath != indexPath){
//delete the row that we selected last time
if (_selectedPath != nil){
pathToDelete = [NSIndexPath indexPathForRow:_selectedPath.row inSection:_selectedPath.section];
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
}
//add new row in the section we selected
_selectedPath = indexPath;
[_tableView beginUpdates];
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
}
}
[_tableView beginUpdates];
[_tableView deselectRowAtIndexPath:indexPath animated:NO];
[_tableView endUpdates];
[_tableView reloadSections:[[NSIndexSet alloc] initWithIndex:pathToDelete.section] withRowAnimation:UITableViewRowAnimationMiddle];
[_tableView reloadSections:[[NSIndexSet alloc] initWithIndex:_selectedPath.section] withRowAnimation:UITableViewRowAnimationMiddle];
[_tableView endUpdates];
}
I think the problem is with begin & endUpdates method, but I don't know where exactly. Any clue?
Thanks
You could do this much simpler, by using a single beginUpdates - endUpdates. You just make your deletions and insertions inside of it. Don`t forget to update the data source for the table, otherwise the app will crash.
Hope this helps!

Resources