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!
Related
Whenever I click on my table view row to insert a large row below it (370px) it freezes, but only if the row I am clicking on is near the bottom of the screen. The CPU usage goes up to 100% and memory continues to increase but no error.
I have already viewed this question and this didn't help UITableView insertRowsAtIndexPaths:WithRowAnimation without freeze UI
here is my code -- self.sectBool basically tracks whether or not we have inserted the row
else if(indexPath.row == 0 && indexPath.section == 4){
self.sectBool = !self.sectBool;
NSMutableArray *indexPaths = [NSMutableArray array];
[indexPaths addObject:[NSIndexPath indexPathForRow:1 inSection:4]];
if (!self.sectBool) {
dispatch_async(dispatch_get_main_queue(), ^{
//[self.tableview reloadData];
[self.tableview beginUpdates];
[self.tableview insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:1 inSection:4]]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableview endUpdates];
//[self.tableview reloadData];
});
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
//[self.tableview reloadData];
[self.tableview beginUpdates];
[self.tableview deleteRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:1 inSection:4]]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableview endUpdates];
//[self.tableview reloadData];
});
}
}
any help is much appreciated!
I need to add a new row under the selected row. I'm using this code in didSelectRowAtIndexPath:
[_dataSource insertObject:newDataObject atIndex:indexPath.row + 1];
NSMutableArray *indexPaths = [NSMutableArray array];
NSInteger currentCount = indexPath.row;
[indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+1 inSection:0]];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
It adds the new row in the correct position, but it also deletes the upper cell separator of the selected row. What am I doing wrong?
Seems like UITableViewCell has no separator in selected state. UITableViewCellSelectionStyleNone just disables highlighting, but cell is selected anyway. adding
[tableView beginUpdates];
// insert/delete manipulations
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[tableView endUpdates];
fixes the problem
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];
}
I have a problem, I want to delete row in UITableView but don't use swipe button delete. I want call function removeCell after condition. My problem is I cannot delete first row in UITableView because I don't know get indexPath of row to delete.
If I swipe Delete button, it work perfect. But I want don't use Delete Button.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
deleteindexPath = indexPath;
/*[_tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[_tableView endUpdates];*/
[self removeCell];
}
removeCell():
-(void) removeCell
{
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deleteIndexPath] withRowAnimation:UITableViewRowAnimationFade];
// }
[_tableView endUpdates];
}
I try set deleteIndexPath = [_tableView indexPathForCell:cell]; in cellForRowAtIndexPath() but not work, crash.
Can you have suggestion?
Just try this
-(void) removeCell
{
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deleteIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//Now remove data from your tableArray also..
[_tableView reloadData];
}
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];
}