to expand or collapse rows at different sections in iOS 7 - ios

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;
}

Related

thread 1:EXC_BAD_ACCESS (code=1, address=0x14)

I'm trying to create the capability of deleting rows from a tableview and this is how I did it. The error is coming from the
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] withRowAnimation:UITableViewRowAnimationFade];
Any help would be very much appreciated. Thank you!
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.homeworkArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView reloadData];
}
the arrayWithObjects: method requires a nil to terminate the list, so you need to say -
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
}
or even easier, use arrayWithObject:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Or as #rmaddy points out, use array literal syntax -
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Also, you don't need the reloadData call - deleteRowsAtIndexPaths will take care of updating the relevant rows in the table view.
It might be Help full for you I know it Same As Another Answer But it is my Own answer :-
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[arrStations removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

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];

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];
}

iOS UIViewAnimation for uitableview cells

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];
}

UITableView doesn't display the cell correctly when inserting in the first row with animations

My app has an UITableViewController as the root controller and a modal view to add a row to this table. I am using CoreData, so I get the data from a NSFetchedResultsController. Anyway, the problem is in the manage of the UITableView.
Each insertion makes TableView add a new cell. Everything works fine except if the cell is added in the first row. In this case its content is not displayed. The cell is blank displayed. If I tap in the cell or I scroll the table in a way the cell must be reloaded it does show its content.
I am using animations so I do [tableView beginUpdates] and [tableView endUpdates] as Apple docs said, instead of [tableView reloadData]. If I do [tableView reloadData] all works fine.
I have checked it and same code is runned for every row. It is a problem about the way the cells are displayed.
I think the problem is about "theory" of animations in TableViews and you won't probably need it, but there is the relevant code in my UITableViewController:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Customer *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = managedObject.name;
}
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
#pragma mark - FetchedResultsController delegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
// [self.tableView reloadData]; this make all works OK but without animations
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
In your switch statement for each type of change, why not add:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
You will have to vary that code slightly depending on the type of change, but this would eliminate the need for beginUpdates and endUpdates.

Resources