I'm trying the iCarousel, there is a function that deletes the view's index and the array's index,
NSInteger index = carousel.currentItemIndex;
[carousel removeItemAtIndex:index animated:YES];
[imagesArray removeObjectAtIndex:index];
I'm deleting it until one item remains, then if one item remains I want to insert the duplicate of it. I tried this:
insertItemAtindex:carousel.currentItemIndex but it's inserting the last View.
But what I wanted is to insert carousel's/imagesArray last object/index in my view. How can I implement it, or how can I determine an NSMutableArray's last object that is left in the view?
carousel.lastObject !!!!!!!!!!
You can check for last object using -
int count = [yourArray count];
[yourArray objectAtIndex:count - 1]; //this will always return you last object
Related
i have a picker view which contains values from an NSMutableArray
NSMutableArray array=[[NSMutableArray alloc]init];
for(int t=-20 ;t<70;t++)
{
NSString *string=[NSString stringWithFormat:#"%d",t];
[array addObject:string];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row{
return [array objectAtIndex:row];
}
I select a value from the picker and save it in database. Afterwards when the page is loaded picker should be loaded with the saved value.
How can I achieve it?
I have seen accessing selected row by passing index value like
[pickerview selectRow:20 inComponent:0 animated:NO];
but in my case is it possible to get the values' index ?or is there any other way?
please help
As you model will change so you can send indexOfObject message to array to get index. Try This
NSInteger index=[array indexOfObject:#"ObjectYouHave"];
I have a UITableViewController that is displaying rows from my NSFetchedResultsController. My has several sections, with the rows being ordered within each section, using an 'order' field.
I'm using fairly bog-standard code to handle the moving of rows. The rows moves are restricted to the section they are contained within. When a row rearrange takes place, I use the following code..
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
self.fetchedResultsController.delegate = nil;
NSMutableArray *myImages = [[self.fetchedResultsController fetchedObjects] mutableCopy];
// Grab the item we're moving.
NSManagedObject *myImage = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];
// Remove the object we're moving from the array.
[myImages removeObject:myImage];
// Now re-insert it at the destination.....here lies the problem???
[myImages insertObject:myImage atIndex [destinationIndexPath row]];
// All of the objects are now in their correct order. Update each
// object's displayOrder field by iterating through the array.
int i = 0;
for (MyImage *myImage in myImages)
{
myImage.order = [NSNumber numberWithInt:i];
i++;
}
// Save the data here!
self.fetchedResultsController.delegate = self;
}
(Some variable names have been changed to protect the innocent!!!)
This works quite well but doesn't always perform as expected. The reason being (I think) is I'm removing the object from the array successfully, but insertion again is wrong because it uses the absolute row index of the whole array, without allowing for the fact that this may be in (say) the second or third section of the UITableView/NSFetchedResultsController.
So, how can I put an object back into my mutable array (which is linear) into my the correct index, given the destination indexPath is two-dimensional? (I could probably count previous rows/section) but wonder if there's a prettier way.)
Thanks
The table starts off with "collapsed" parent cells (the array representing this is what is first passed to the function). Each cell is represented in the array by an NSDictionary. Each parent cell has an array of child cells, and each child cell their array of child cells, etc. If a cell has no children, the "children" key in the dictionary is set to an empty NSMutableArray.
The goal of this function is to take an NSMutableArray representing the tableView with no expanded cells and expand each cell and it's children recursively (think a comment thread on Reddit) while adding the expanded cells to the original array accordingly. It works but, the cells aren't expanded correctly for some reason. Some are expanded and some aren't, it's odd. Any ideas?
- (void)expandCells:(NSMutableArray *)comments
{
for (NSDictionary *thread in comments)
{
// recursive loop using each child in the root's child array
NSMutableArray *children = [thread objectForKey:#"children"];
if (children && [children count] > 0)
{
[self expandCells:children];
// indexPath of root of current thread
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[comments indexOfObjectIdenticalTo:thread] inSection:0];
NSUInteger count = indexPath.row + 1;
NSMutableArray *newCellIndexes = [NSMutableArray array];
for (NSDictionary *child in children)
{
//[newCellIndexes addObject:[NSIndexPath indexPathForRow:count inSection:0]];
// a copy of self.tableCells first passed to this function,
// since you cannot modify an array while iterating over it
[self.tableCells insertObject:child atIndex:count++];
}
//[self.tableView insertRowsAtIndexPaths:newCellIndexes withRowAnimation:UITableViewRowAnimationFade];
}
}
}
Well the problem is exactly described in the error. You are mutating the array while iterating through it. You say that in the first iteration, you pass in self.tableCells. so self.tableCells == comments, and while enumerating comments, you add objects to self.tableCells, thus mutating it. You cannot add objects to an array that you are enumerating through. consider storing the cells in another data structure.
After using [UITableView deleteSections:withRowAnimation:] on a section which is out of view - the section header remains visible.
On this image, we see the visible part of the tableview
On the next image, we see the whole tableview - AISLE 2 is hidden until the user scrolls down, it contains only one row:
When I scroll down and delete the last row, AISLE 2 section header remains visible, even though I used deleteSections. if I delete a row from AISLE 1, the section header remains on the same place, and by scrolling down I can still see it.
Furthermore, when trying to scroll down so that AISLE 2 header is in the view, the UI acts as AISLE2 is NOT part of the tableview, and immediately scrolls me back up. Which means - this is a garbage view that is obviously not part of the table, since I removed it. for some reason, iOS doesn't remove this view, but de-associates it from the table.
Any ideas?
Try [tableview reload] with making numberofsections as 1.
Where is your data coming from and how do you know how many sections there are at the start? I think your problem can easily be resolved by explaining this.
It looks like you may have a multidimentional nsmutablearray where each index is an aisle and each object contains the products for that isle? Or you may have a different array for each aisle?
When you delete a cell, simply check how many cells are left, and call [self.tableView reloadData];
For (hypothetical) example;
if you have arrays of your Aisles:
NSMutableArray *aisleOne = [[NSMutableArray alloc] initWithObjects:#"Product1", #"Product2", nil];
NSMutableArray *aisleTwo = [[NSMutableArray alloc] initWithObjects:#"Product1", #"Product2", nil];
NSMutableArray *aisleThree = [[NSMutableArray alloc] initWithObjects:#"Product1", #"Product2", nil];
NSMutableArray *aisleFour = [[NSMutableArray alloc] initWithObjects:#"Product1", #"Product2", nil];
and you add them to one array:
NSMutableArray *aisleArray = [[NSMutableArray alloc] initWithObjects:aisleOne, aisleTwo, aisleThree, aisleFour, nil];
then, call this code when you delete a cell. It will remove all empty Aisles from the aisleArray (which needs to be globally defined):
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:aisleArray];
for (int i=0; i<[tempArray count]; i++) {
if ([[tempArray objectAtIndex:i] count] == 0) {
[aisleArray removeObjectAtIndex:i];
}
}
[self.tableView reloadData];
For this to work, these two methods should be:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [aisleArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[aisleArray objectAtIndex:section] count];
}
(untested)
Just get the table haderview for that section and remove it from it's superview and tableView is not managing it.
UIView *mysteriousView = [tableView headerViewForSection:indexPath.section];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
[mysteriousView removeFromSuperview];
Hope this helps!
Few yers later I'm facing same issue... Deleting last section doesn't remove last section header from table view.
I noticed however that the remaining header has frame exactly just below normal table view content.
So the workaround (in swift but you can easily translate it to Objective C) that worked for me is something like this:
DispatchQueue.main.async {
let unwantedViews = self.tableView.subviews.filter{ $0.frame.minY >= self.tableView.contentSize.height}
unwantedViews.forEach{ $0.isHidden = true }
}
My purpose is when the user click on a button in view1 (to go to the view2), i will send all the selected cells from UITableView just checked in view1 to the view2.
So before passing the table to next view, i wanted to test if i am on the right track, but seems not. This is my code:
-(IBAction)goToView2{
//get all section selectioned items into an array
themesChosed=[tView indexPathsForSelectedRows];//tView is the outlet of my UITableView and themesChosed is an NSArray declared in the .h file
for (int i=0; i<=[themesChosed count]; i++) {
NSLog(#"%i",[[themesChosed objectAtIndex:i]integerValue]);
}
}
This always returning me a single 0.
2012-01-13 15:52:06.472 MyApp[876:11603] 0
Although i selected several items on the table.
The objects in themesChosed are NSIndexPath objects. You'll want to access the row and section properties like so:
-(IBAction)goToView2{
//get all section selectioned items into an array
themesChosed=[tView indexPathsForSelectedRows];//tView is the outlet of my UITableView and themesChosed is an NSArray declared in the .h file
for (int i=0; i<=[themesChosed count]; i++) {
NSIndexPath *thisPath = [themesChosed objectAtIndex:i];
NSLog(#"row = %i, section = %i", thisPath.row, thisPath.section);
}
}
First NSIndexPath is not an integer, it is an object with row and section methods, so that is probably why you get a zero. Are you sure you have allowsMultipleSelection set to YES?
If you are not getting index values then best approach wound be as below to access index number:
NSArray *indexes = [self.tableView indexPathsForSelectedRows];
for (NSIndexPath *path in indexes) {
NSUInteger index = [path indexAtPosition:[path length] - 1];
[selectedOptionsArray addObject:[optionHolderArray objectAtIndex:index]];
}