I have a snippet of code of expanding the rows of cells of section. The error occurs at the xtable updates [UIViewAnimation initWithView:indexPath:endRect:endAlpha:startFraction:endFraction:curve:animateFromCurrentPosition:shouldDeleteAfterAnimation:editing:]
Snippet of code
-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)sectionOpened {
SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:sectionOpened];
sectionInfo.open = YES;
NSInteger countOfRowsToInsert = 5;
NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:sectionOpened]];
}
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
NSInteger previousOpenSectionIndex = self.openSectionIndex;
if (previousOpenSectionIndex != NSNotFound) {
SectionInfo *previousOpenSection = [self.sectionInfoArray objectAtIndex:previousOpenSectionIndex];
previousOpenSection.open = NO;
[previousOpenSection.headerView toggleOpenWithUserAction:NO];
NSInteger countOfRowsToDelete = 5;
for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenSectionIndex]];
}
}
// Style the animation so that there's a smooth flow in either direction.
UITableViewRowAnimation insertAnimation;
UITableViewRowAnimation deleteAnimation;
if (previousOpenSectionIndex == NSNotFound || sectionOpened < previousOpenSectionIndex) {
insertAnimation = UITableViewRowAnimationTop;
deleteAnimation =UITableViewRowAnimationBottom;
}
else {
insertAnimation = UITableViewRowAnimationBottom;
deleteAnimation = UITableViewRowAnimationTop;
}
// Apply the updates.
[xtable beginUpdates];
[xtable insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:insertAnimation];
[xtable deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
[xtable endUpdates];
self.openSectionIndex = sectionOpened;
}
what could possible be wrong? any ideas?
in my case helped
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0;
}
Related
I have gone through of various solutions on this but they didn't seem to work. I get consistency exception after insert and delete operations.
For the time being I have used reloadSection method as temporary solution which gives awful animation experience.
How to use insertRowsWithAnimation and deleteRowsWithAnimation which will result in correct animation in the following code :
- (void) imageTapped:(id)sender {
#try {
UIButton *button = sender;
NSInteger tag = button.tag;
GroupNode *group = [[dictGroups objectForKey:#"0"] objectAtIndex:tag];
NSMutableArray *arr = [[CommonModel shared]GetAllGroupNodesByTreeId:group.TreeId];
BOOL isVisited = NO;
GroupNode *nextNode;
for(GroupNode *gp in arr)
{
if(!isVisited)
{
if([gp.Id isEqual:group.Id])
{
isVisited = YES;
}
}
else
{
nextNode = gp;
break;
}
}
if(![group.Indent isEqual:#"0"] && [nextNode.Indent intValue] == [group.Indent intValue] + 1)
{
if ([arrExpandedGroups containsObject:group.Id]) {
[arrExpandedGroups removeObject:group.Id];
NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
NSMutableArray *arr1 = [dictGroups objectForKey:#"0"];
NSMutableArray *arrDelete = [[NSMutableArray alloc]init];
BOOL isChild = YES;
for(int i = 0; i< arr1.count;i++)
{
GroupNode *gp = [arr1 objectAtIndex:i];
if(![group.Id isEqual:gp.Id])
{
if([arr1 indexOfObject:gp] < [arr1 indexOfObject:group])
{
[arrDelete addObject:gp];
}
else
{
if(([gp.Indent intValue] > [group.Indent intValue]) && !isChild)
{
[arrDelete addObject:gp];
}
else if(([gp.Indent intValue] <= [group.Indent intValue]))
{
[arrDelete addObject:gp];
isChild = NO;
}
else
{
[deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
}
}
else
{
[arrDelete addObject:gp];
[dictExpand removeObjectForKey:[NSString stringWithFormat:#"%i", (int)button.tag]];
button.tag = [arrDelete indexOfObject:gp];
[dictExpand setObject:#"no" forKey:[NSString stringWithFormat:#"%i", (int)button.tag]];
for(NSString *key in dictExpand.allKeys)
{
if(![key isEqual:[NSString stringWithFormat:#"%i", (int)button.tag]])
{
if([group.Indent intValue] == 1)
[dictExpand setObject:#"no" forKey:key];
}
}
}
}
[tableGroup beginUpdates];
[dictGroups setObject:arrDelete forKey:#"0"];
NSRange range = NSMakeRange(0, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone];
[tableGroup endUpdates];
} else {
if([group.Indent intValue] == 1)
[arrExpandedGroups removeAllObjects];
[arrExpandedGroups addObject:group.Id];
NSMutableArray *arrAdd = [NSMutableArray new];
NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
NSMutableArray *arr1 = [dictGroups objectForKey:#"0"];
BOOL isVisited = NO;
BOOL isMainVisited = NO;
int icount = 0;
for(GroupNode *gp in arr)
{
BOOL isPresentInArr1 = NO;
for(GroupNode *g in arr1)
{
if([g.Id isEqual:gp.Id] && ![group.Indent isEqual:#"1"])
{
isPresentInArr1 = YES;
break;
}
}
if(!isVisited && (([gp.Indent intValue] == 0 || [gp.Indent intValue] == 1) || isPresentInArr1))
{
[arrAdd addObject:gp];
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
if([gp.Id isEqual:group.Id])
{
isVisited = YES;
[dictExpand removeObjectForKey:[NSString stringWithFormat:#"%i", (int)button.tag]];
button.tag = [arrAdd indexOfObject:gp];
[dictExpand setObject:#"yes" forKey:[NSString stringWithFormat:#"%i", (int)button.tag]];
for(NSString *key in dictExpand.allKeys)
{
if(![key isEqual:[NSString stringWithFormat:#"%i", (int)button.tag]])
{
if([group.Indent intValue] == 1)
[dictExpand setObject:#"no" forKey:key];
}
}
}
}
else if(isVisited)
{
if([gp.Indent intValue] > [group.Indent intValue] && ([gp.Indent intValue] == [group.Indent intValue] + 1) && !isMainVisited)
{
[arrAdd addObject:gp];
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
}
else if([group.Indent intValue] > 1 && [gp.Indent intValue] == [group.Indent intValue] && !isMainVisited)
{
[arrAdd addObject:gp];
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
}
else if([gp.Indent intValue] <= 1 && !isMainVisited)
{
[arrAdd addObject:gp];
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
isMainVisited = YES;
}
else if([gp.Indent intValue] <= 1 && isMainVisited)
{
[arrAdd addObject:gp];
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
}
else
{
for(GroupNode *gn in arr1)
{
if([gn.Id isEqual:gp.Id])
[deleteIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
}
}
}
icount ++;
}
[dictGroups setObject:arrAdd forKey:#"0"];
[tableGroup beginUpdates];
NSRange range = NSMakeRange(0, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone];
[tableGroup endUpdates];
}
}
}
#catch (NSException *exception) {
}
}
I am aware this is a complex logic but this is how it will work.
arr - All database records array
arr1 - All displayed records
numberOfRowsInSection :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = (int)[[dictGroups objectForKey:#"0"] count];
return count;
}
This number is always same as the number of records in array in imageTapped function.
Insert row in table view as well when you are adding it in array. you are at once reloading the whole section... may be that might be a problem!(m not sure!!!)
Add this when you are updating index path while adding row:
[tableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
and this when deleting the index:
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
may be this can help and if not then try changing following line from your code:
[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone];
to:
[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
At first delete that object from tableview array, then use
[table deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
[table reloadData];
I'm having serious troubles with the animation of a UICollectionView, similar to the problem mentioned here: UICollectionView crashes when rearranging items between sections
Let's say I have a UICollectionView with 5 different sections, each with 10 cells.
I would like to update the CollectionView with an animation, so the cells will be reordered into 10 sections. No cells with new content will be added nor existing cells removed.
Therefore I'm performing a batch update:
[_resultCollectionView performBatchUpdates:^{
[_resultCollectionView insertSections:insertSections];
[_resultCollectionView deleteSections:deleteSections];
//[_resultCollectionView insertItemsAtIndexPaths:insertIndexPaths];
//[_resultCollectionView deleteItemsAtIndexPaths:deleteIndexPaths];
for (all items which need to be moved...) {
[_resultCollectionView moveItemAtIndexPath:sourcePath toIndexPath:destinationPath];
}
} completion:^(BOOL finished) {
//[_resultCollectionView reloadData];
nil;
}];
If I perform insertSections, deleteSections, insertItemsAtIndexPath, deleteItemsAtIndexPath and reloadData once the block was performed everything works fine which means my dataSource and delegates work properly in theory. Except it's not animated yet ...
If I perform insertSections, deleteSections and moveItemAtIndexPath (which should work since the cells only get rearranged) i get this little bug:
cannot move a row into a newly inserted section (5)
If I perform insertSections, deleteSections and moveItemAtIndexPath but exclude any movement into a newly inserted section I obviously get invalid number of items in the sections.
Does anybody have a solution for this problem?
I followed john1034's link and figured out a quite tricky way to do it. You have to split up the work in three steps:
Adding new sections if necessary. The new sections will be empty first.
Adding, removing and moving cells within the new and existing sections.
Deleting sections if necessary.
For sure there are many lines which can be further improved. Especially the search functions are quite inefficient...
static BOOL addingSections = NO;
static BOOL updatingCollectionView = NO;
static BOOL removingSections = NO;
- (void)reloadResultCollectionView
//IndexUnloadedArray: a link to my CollectionViewData before the update
//IndexArray: a link to my CollectionViewData after the update
//start only if something changed
if (![IndexArray isEqual:IndexUnloadedArray]) {
NSMutableIndexSet *deleteSections = [[NSMutableIndexSet alloc] init];
NSMutableIndexSet *insertSections = [[NSMutableIndexSet alloc] init];
NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
//step 1 - add collectionView sections
for (int i = 0; i < IndexArray.count; i++) {
if (i >= IndexUnloadedArray.count) {
[insertSections addIndex:i];
}
}
NSLog(#"insert sections:%#", insertSections);
_sectionAmount = (int)_distanceUnloadedArray.count + (int)insertSections.count;
addingSections = YES;
[_resultCollectionView performBatchUpdates:^{
[_resultCollectionView insertSections:insertSections];
} completion:^(BOOL finished) {
nil;
}];
addingSections = NO;
//step 2 - update collectionView
//adding cells if there are not enough
for (int i = 0; i < IndexArray.count; i++) {
for (int j = 0; j < (int)[[IndexArray objectAtIndex:i] count]; j++) {
NSNumber *searchIndex = [[IndexArray objectAtIndex:i] objectAtIndex:j];
bool found = NO;
for (int k = 0; k < IndexUnloadedArray.count; k++) {
if ([[IndexUnloadedArray objectAtIndex:k] containsObject:searchIndex]) {
found = YES;
k = (int)IndexUnloadedArray.count;
}
}
if (!found) {
[insertIndexPaths addObject:[NSIndexPath indexPathForRow:j inSection:i]];
}
}
}
NSLog(#"insert cells:%#", insertIndexPaths);
//deleting cells if there are too many
for (int i = 0; i < IndexUnloadedArray.count; i++) {
if (![deleteSections containsIndex:i]) {
for (int j = 0; j < (int)[[IndexUnloadedArray objectAtIndex:i] count]; j++) {
NSNumber *searchIndex = [[IndexUnloadedArray objectAtIndex:i] objectAtIndex:j];
bool found = NO;
for (int k = 0; k < IndexArray.count; k++) {
if ([[IndexArray objectAtIndex:k] containsObject:searchIndex]) {
found = YES;
k = (int)IndexArray.count;
}
}
if (!found) {
[deleteIndexPaths addObject:[NSIndexPath indexPathForRow:j inSection:i]];
}
}
}
}
NSLog(#"deleting cells:%#", deleteIndexPaths);
updatingCollectionView = YES;
[_resultCollectionView performBatchUpdates:^{
[_resultCollectionView insertItemsAtIndexPaths:insertIndexPaths];
[_resultCollectionView deleteItemsAtIndexPaths:deleteIndexPaths];
for (int i = 0; i < IndexUnloadedArray.count; i++) {
for (int j = 0; j < [[IndexUnloadedArray objectAtIndex:i] count]; j++) {
NSIndexPath *sourcePath = [NSIndexPath indexPathForRow:(j) inSection:i];
NSNumber *searchIndex = [[IndexUnloadedArray objectAtIndex:i] objectAtIndex:j];
NSIndexPath *destinationPath;
for (int k = 0; k < IndexArray.count; k++) {
if ([[IndexArray objectAtIndex:k] containsObject:searchIndex]) {
NSInteger *row = [[IndexArray objectAtIndex:k] indexOfObject:searchIndex];
destinationPath = [NSIndexPath indexPathForItem:row inSection:k];
if (sourcePath != destinationPath) {
NSLog(#"moving cell from %ld.%ld to %ld.%ld (%#)", (long)sourcePath.section, (long)sourcePath.row, (long)destinationPath.section, (long)destinationPath.row);
[_resultCollectionView moveItemAtIndexPath:sourcePath toIndexPath:destinationPath];
} else {
NSLog(#"object %ld.%ld stays in position", (long)sourcePath.section, (long)sourcePath.row);
}
}
}
}
}
} completion:^(BOOL finished) {
nil;
}];
updatingCollectionView = NO;
//step 3 - deleting sections if there are too many
for (int i = 0; i < IndexUnloadedArray.count; i++) {
if (i >= IndexArray.count) {
[deleteSections addIndex:i];
}
}
NSLog(#"delete sections:%#", deleteSections);
_sectionAmount = (int)_distanceArray.count;
removingSections = YES;
[_resultCollectionView performBatchUpdates:^{
[_resultCollectionView deleteSections:deleteSections];
} completion:^(BOOL finished) {
//update table header and footer
[_resultCollectionView reloadData];
}];
removingSections = NO;
}
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return _sectionAmount;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
if (addingSections) {
if (section < _distanceUnloadedArray.count) {
return [[_distanceUnloadedArray objectAtIndex:section] count];
} else {
return 0;
}
}
if (updatingCollectionView) {
if (section < _distanceArray.count) {
return [[_distanceArray objectAtIndex:section] count];
} else {
return 0;
}
}
if (removingSections) {
return [[_distanceArray objectAtIndex:section] count];
}
return [[_distanceArray objectAtIndex:section] count];
}
I have this code and I am trying to understand it. This has an int value 'currentExpandIndex' ,I couldnt figure out why it is changing because I dont find proper initiation of it. The int is first given the value -1,but later in code the int value changes according to the indexpath. I coldnt find the relation between the int and the indexpath declared in the code.Kindly tell me,why
the code is:
#interface AccordionTableViewController : UITableViewController {
NSArray *topItems;
NSMutableArray *subItems; // array of arrays
int currentExpandedIndex;
}
#end
//.m file
- (id)init {
self = [super init];
if (self) {
topItems = [[NSArray alloc] initWithArray:[self topLevelItems]];
subItems = [NSMutableArray new];
currentExpandedIndex = -1;
NSLog(#"currenyt index -init is %d",currentExpandedIndex);
for (int i = 0; i < [topItems count]; i++) {
[subItems addObject:[self subItems]];
}
}
return self;
}
#pragma mark - Data generators
- (NSArray *)topLevelItems {
NSMutableArray *items = [NSMutableArray array];
for (int i = 0; i < NUM_TOP_ITEMS; i++) {
[items addObject:[NSString stringWithFormat:#"Item %d", i + 1]];
}
return items;
}
- (NSArray *)subItems {
NSMutableArray *items = [NSMutableArray array];
int numItems = arc4random() % NUM_SUBITEMS + 2;
for (int i = 0; i < numItems; i++) {
[items addObject:[NSString stringWithFormat:#"SubItem %d", i + 1]];
}
return items;
}
#pragma mark - View management
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"currenyt index -view did load is %d",currentExpandedIndex);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"currenyt index -no of rows in section is %d",currentExpandedIndex);
return [topItems count] + ((currentExpandedIndex > -1) ? [[subItems objectAtIndex:currentExpandedIndex] count] : 0);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ParentCellIdentifier = #"ParentCell";
static NSString *ChildCellIdentifier = #"ChildCell";
NSLog(#"currenyt index-cell for row at index is %d",currentExpandedIndex);
BOOL isChild =
currentExpandedIndex > -1
&& indexPath.row > currentExpandedIndex
&& indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];
UITableViewCell *cell;
if (isChild) {
cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
}
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ParentCellIdentifier] autorelease];
}
if (isChild) {
cell.detailTextLabel.text = [[subItems objectAtIndex:currentExpandedIndex] objectAtIndex:indexPath.row - currentExpandedIndex - 1];
}
else {
int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex)
? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count]
: indexPath.row;
cell.textLabel.text = [topItems objectAtIndex:topIndex];
cell.detailTextLabel.text = #"";
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL isChild =
currentExpandedIndex > -1
&& indexPath.row > currentExpandedIndex
&& indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];
if (isChild) {
NSLog(#"A child was tapped, do what you will with it");
NSLog(#"currenyt index -did select is %d",currentExpandedIndex);
return;
}
NSLog(#"currenyt index -did select out is %d",currentExpandedIndex);
[self.tableView beginUpdates];
if (currentExpandedIndex == indexPath.row) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
currentExpandedIndex = -1;
}
else {
BOOL shouldCollapse = currentExpandedIndex > -1;
if (shouldCollapse) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
}
currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;
[self expandItemAtIndex:currentExpandedIndex];
}
[self.tableView endUpdates];
}
- (void)expandItemAtIndex:(int)index {
NSMutableArray *indexPaths = [NSMutableArray new];
NSArray *currentSubItems = [subItems objectAtIndex:index];
int insertPos = index + 1;
for (int i = 0; i < [currentSubItems count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
//[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[indexPaths release];
}
- (void)collapseSubItemsAtIndex:(int)index {
NSMutableArray *indexPaths = [NSMutableArray new];
for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[indexPaths release];
}
In didSelectRowAtIndexPath: you have this line:
currentExpandedIndex =
(shouldCollapse && indexPath.row > currentExpandedIndex) ?
indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] :
indexPath.row;
This line assign indexPath.row or indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] to currentExpandedIndex.
currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;
On each didSelectRow it will change.
First, set a breakpoint in -init. Depending on how you create this controller, it might not be called, in which case it will be 0. Try -initWithCoder: instead if this is the case. Otherwise, I only see this set in one place: -tableView:didSelectRowAtIndexPath:, where it can be set to the row or indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count]. Depending on what else you are doing with the table, this might be a good job for sections or expandable individual rows to simplify.
Good day! My default tableview sectionHeaderHeight is 56. I want to update it when sectionOpened method gets called.This is the following code:
- (void) sectionOpened : (NSInteger) section
{
sectionHeight = #"YES";
getsection = section;
NSLog(#"Section Clicked: %ld",(long)getsection);
SectionInfo *array = [self.sectionInfoArray objectAtIndex:section];
array.open = YES;
NSInteger count = [array.category.menulist count];
NSMutableArray *indexPathToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i<count;i++)
{
[indexPathToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
NSInteger previousOpenIndex = self.openSectionIndex;
if (previousOpenIndex != NSNotFound)
{
SectionInfo *sectionArray = [self.sectionInfoArray objectAtIndex:previousOpenIndex];
sectionArray.open = NO;
NSInteger counts = [sectionArray.category.menulist count];
[sectionArray.sectionView toggleButtonPressed:FALSE];
for (NSInteger i = 0; i<counts; i++)
{
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenIndex]];
}
}
UITableViewRowAnimation insertAnimation;
UITableViewRowAnimation deleteAnimation;
if (previousOpenIndex == NSNotFound || section < previousOpenIndex)
{
insertAnimation = UITableViewRowAnimationTop;
deleteAnimation = UITableViewRowAnimationBottom;
}
else
{
insertAnimation = UITableViewRowAnimationBottom;
deleteAnimation = UITableViewRowAnimationTop;
}
[menulistTable beginUpdates];
if(section==3 || section==4 || section==5) // this section has cells like submenu of a menu
{
menulistTable.sectionHeaderHeight = 20.0;
[menulistTable reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
//[menulistTable reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
}
[menulistTable insertRowsAtIndexPaths:indexPathToInsert withRowAnimation:insertAnimation];
[menulistTable deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
[menulistTable endUpdates];
self.openSectionIndex = section;
}
But nothing happening. I also used indexSetWithIndex:0. Whats wrong in the code?
Try like this to change Height:
[menulistTable beginUpdates];
menulistTable.sectionHeaderHeight = 20.0;
[menulistTable endUpdates];
Edited:
Take variable to set SectionHeaderHeight. Then place this code:
[menulistTable beginUpdates];
secHeaderHeight = 20.0;
[menulistTable endUpdates];
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return secHeaderHeight;
}
Hope it will work for you.
I have the following code.
NSMutableArray *aList = [[NSMutableArray alloc] init];
for (NSIndexPath *indexPath in tableview1.indexPathsForSelectedRows) {
NSString *r = [NSString stringWithFormat:#"%i",indexPath.row];
[aList addObject:r];
}
So what I get is a list of UITableView rows that are selected. Is there a simple way of getting a list of rows that are NOT selected?
Thank you for your help.
There may be elegant solutions but this will work.
NSMutableArray *allIndexPaths = [#[]mutableCopy];
NSInteger nSections = [self.tableView numberOfSections];
for (int j=0; j<nSections; j++) {
NSInteger nRows = [self.tableView numberOfRowsInSection:j];
for (int i=0; i<nRows; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
[allIndexPaths addObject:indexPath];
}
}
NSArray *selectedIndexPaths = self.tableView.indexPathsForSelectedRows;
for (NSIndexPath *indexPath in selectedIndexPaths) {
[allIndexPaths removeObject:indexPath];
}
NSArray *unselectedIndexPaths = [NSArray arrayWithArray:allIndexPaths];
EDIT : As per Rikkles suggestion
NSMutableArray *unselectedIndexPaths = [#[]mutableCopy];
NSArray *selectedIndexPaths = self.tableView.indexPathsForSelectedRows;
NSInteger nSections = [self.tableView numberOfSections];
for (int j=0; j<nSections; j++) {
NSInteger nRows = [self.tableView numberOfRowsInSection:j];
for (int i=0; i<nRows; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
if (![selectedIndexPaths containsObject:indexPath]) {
[unselectedIndexPaths addObject:indexPath];
}
}
}
NSMutableArray *a = [NSMutableArray array]; // that's all unselected indexPaths
NSArray *l = [self.tableView indexPathsForSelectedRows];
NSIndexPath *idx;
for (NSUInteger sec=0; sec<[self.tableView numberOfSections]; sec++) {
for (NSUInteger r=0; r<[self.tableView numberOfRowsInSection:sec]; r++) {
idx = [NSIndexPath indexPathForRow:r inSection:sec];
if(![l containsObject:idx]){
[a addObject:idx];
}
}
}
You can use the numberOfRows return value minus the total selected rows.
I've added several additional lines to Anupdas' code. He certainly deserves the entire credit.
NSMutableArray *allIndexPaths = [#[]mutableCopy];
NSInteger nSections = [tableview1 numberOfSections];
for (int j=0; j < nSections; j++) {
NSInteger nRows = [tableview1 numberOfRowsInSection:j];
for (int i=0; i<nRows; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
[allIndexPaths addObject:indexPath];
}
}
NSArray *selectedIndexPaths = tableview1.indexPathsForSelectedRows;
for (NSIndexPath *indexPath in selectedIndexPaths) {
[allIndexPaths removeObject:indexPath];
}
NSMutableArray *bList = [[NSMutableArray alloc] init];
for (NSInteger k = 0; k < allIndexPaths.count; k++) {
NSString *r = [NSString stringWithFormat:#"%i",[[allIndexPaths objectAtIndex:k] row]];
[bList addObject:r];
}
NSMutableArray * yourNewArr = [[NSMutableArray alloc ] init];
for(NSUInteger i = 0 ; i < [yourArr count] ; i++){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
NSArray *selIndexs = self.tableView.indexPathsForSelectedRows;
if(![selIndexs containsObject:indexPath]){
[yourNewArr addObject:indexPath];
}
}
NSLog(#"%#", yourNewArr); // this will contain unselected index paths.