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.
Related
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 am trying to add new items to my collection view using insertItemsAtIndexPaths. My app crashes at performBatchupdate
- (void) addItems {
NSArray *newProducts = #[#"1",#"2",#"3",#"4"];
[self.collectionView performBatchUpdates:^{
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
for (NSInteger index = self.array.count; index < (self.array.count + newProducts.count); index++) {
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]];
}
[self.array addObjectsFromArray:newProducts];
[self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
}
completion:nil];
}
Following is the crash log:
* Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]
This Assertion happens when cell is not registered with the collectionview. I am registering my cell.
This worked for me:
If Collection view is empty reload else insertItems.
- (void)addItems {
NSArray *newProducts = #[#"1",#"2",#"3",#"4"];
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
for (NSInteger index = self.array.count; index < (self.array.count + newProducts.count); index++) {
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]];
}
if (self.array) {
[self.array addObjectsFromArray:newProducts];
[self.collectionView performBatchUpdates:^{
[self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
}
completion:nil];
}
else {
self.array = [[NSMutableArray alloc] initWithArray:newProducts];
[self.collectionView reloadData];
}
}
I am inserting and removing rows at random from a tableview. However, if the rows I want to remove are not in view, they are not actually removed in the UI when the given row does come back to view.
If I add and remove the rows only on visible rows, it works just fine. What could be causing this problem?
- (void)sectionHeaderView:(BaseSectionHeaderView *)sectionHeaderView sectionOpened:(NSInteger)sectionOpened {
LogInfo(#"opening section:%ld",sectionOpened);
[_dataSource setSectionAtIndex:sectionOpened Open:YES];
/*
Create an array containing the index paths of the rows to insert: These correspond to the rows for each quotation in the current section.
*/
NSInteger countOfRowsToInsert = [_dataSource tableView:_tableView numberOfRowsInSection:sectionOpened];
NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:sectionOpened]];
}
/*
Create an array containing the index paths of the rows to delete: These correspond to the rows for each quotation in the previously-open section, if there was one.
*/
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
if (self.openSection.openSectionHeaderView != nil) {
[_openSection.openSectionHeaderView toggleOpenWithUserAction:NO];
// NSInteger countOfRowsToDelete = [_purchaseInvoiceListTable.tableView numberOfRowsInSection:self.openSection.openSectionIndex];
NSInteger countOfRowsToDelete = [_dataSource tableView:_tableView numberOfRowsInSection:self.openSection.openSectionIndex];
[_dataSource setSectionAtIndex:self.openSection.openSectionIndex Open:NO];
for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:self.openSection.openSectionIndex]];
}
}
// style the animation so that there's a smooth flow in either direction
UITableViewRowAnimation insertAnimation;
UITableViewRowAnimation deleteAnimation;
if (self.openSection.openSectionHeaderView == nil || sectionOpened < self.openSection.openSectionIndex) {
insertAnimation = UITableViewRowAnimationTop;
deleteAnimation = UITableViewRowAnimationBottom;
}
else {
insertAnimation = UITableViewRowAnimationBottom;
deleteAnimation = UITableViewRowAnimationTop;
}
// apply the updates
[_tableView beginUpdates];
[_tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:insertAnimation];
[_tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
[_tableView endUpdates];
self.openSection.openSectionIndex = sectionOpened;
self.openSection.openSectionHeaderView = sectionHeaderView;
self.openSection.sectionHeight = [NSNumber numberWithFloat:sectionHeaderView.frame.size.height];
LogInfo(#"sectionOpened:%ld",sectionOpened);
}
i think you should reload the table view by this...
[tableView reloadData];
and if from this it will not give the expected result then remove the object from array that fill the table view's row. i think this will definitely work...
tell me is it working or not...
I know this has been asked before 50 times but I have looked through all the other answers and didn't find anything that could fix my issue.
Anyway this is my code:
NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) {
NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) {
[cellIndicesToBeDeleted addObject:p];
[self.cellArray removeObjectAtIndex:i];
}
}
[thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft];
[cellIndicesToBeDeleted release];
And when I execute this code I get this crash log:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Does anyone see what I am doing wrong?
Newish code:
NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) {
NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) {
[cellIndicesToBeDeleted addObject:p];
[mutableIndexSet addIndex:p.row];
}
}
[self.cellArray removeObjectsAtIndexes:mutableIndexSet];
[thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft];
[cellIndicesToBeDeleted release];
[mutableIndexSet release];
I figured it out it had to do with the numberOfRowsInSection. I wasn't updating my nsuserdefaults with the updated datasource so I did that now and it works perfectly. Here is my updated code by the way:
//Code to delete rows
NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
for (int i = [thetableView numberOfRowsInSection:0] - 1; i >= 0; i--) {
NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) {
[cellIndicesToBeDeleted addObject:p];
[mutableIndexSet addIndex:p.row];
}
}
[self.cellArray removeObjectsAtIndexes:mutableIndexSet];
[[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:#"cellArray"];
[thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft];
[cellIndicesToBeDeleted release];
[mutableIndexSet release];
You need to add the following before and after you remove the row:
[theTableView beginUpdates];
// your code goes here
[theTableView endUpdates];
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;
}