How to Update UIScrollView when an index of array is Deleted - iphone

i am new to IPhone Development. can some one tell me how to update the content of UIScrollview. i have an NSMutable Array that contains different quotes so if i delete one of the quotes from the scrollview how it can be remove from view and page should move to the next page if the middle one is deleted and should go to the previous one if the last index of an array is deleted. Please Help!
Thanks in Advance

The simplest approach is to create two methods - for adding and removing objects from the NSMutableArray and inside them change the contents of the UIScrollView. For example:
-(void)addObjectToArray:(id)obj
{
[self.myArray addObject:obj];
//Create and initialize a view with a quote for example and add it in the UIScrollView
[self.scrollView addSubView:someView];
}
-(void)removeObjectFromArray:(id)obj
{
[self.myArray removeObject:obj];
//Find the view you need to remove, either by tag or your array is actually from views,
//and remove it from the UIScrollView
[someView removeFromSuperview];
}
Then when inserting or deleting objects from the array use those methods
[self addObjectToArray:someObject];
[self removeObjectFromArray:someObject];
There are some other more elegant approaches using KVO, but need to read some info about that..

after deleting image remove that data from array
[dataarray removeObjectAtIndex:selectedindex];
now again call the scrollview method

Related

Access UITextField with tag on a static Table

So I have a static TableView setup in the interface builder with multiple sections, labels, and text fields.
I'd like to be able to something along the lines of
[self.view viewWithTag:kNameTextfield]
and get the UITextField with that tag that I've set.
For some reason I cannot access the UITextField and it comes up Nil. I imagine because it's somewhat deeply nested? I thought about creating an Outlet collection of all the text fields and other than iterating through them every single time I need to change something seems like a waste?
What I'm trying to do is on initial load, it populates the values of the textfield by values in a dictionary. So it's useful to be able to target a specific textfield.
Try iterating through the subviews:
for (UIView* viewsLevel1 in [cell.contentView subviews])
{
for (UIView* viewsLevel2 in viewsLevel1)
{
UITextField *textField = [viewsLevel2 viewWithTag:kNameTextField];
}
}
Go as far deep as you need to go

Display current index of iCarousel in a label which is not a subView in ICarousel

I am using iCarousel in my application,I need to get the current index of the iCarousel and display that index in a label(subView of self.view) which is not a subView of iCarousel.
I am able to get the current index With
int index=iCarousel.currentIndex;
How can i update the index in label.text that every time when the carousel is Scrolled.
In which method i have to write the code to update label.
Whenever you scroll the following delegate will called, so you can update your label here
- (void)carouselCurrentItemIndexUpdated:(iCarousel *)carousel1
{
int index=carousel1.currentIndex;
yourlabel.text = [NSString stringWithFormat:#"%d",index];
}
For using the iCarousel, it is better to study its documentation thoroughly before going into its implementation from iCarousel Documentation.
So for the issue you are facing , there are 2 methods provided in the documentation -
- (NSInteger)indexOfItemView:(UIView *)view;
The index for a given item view in the carousel. Works for item views
and placeholder views, however placeholder view indexes do not match
the ones used by the dataSource and may be negative (see
indexesForVisibleItems property above for more details). This method
only works for visible item views and will return NSNotFound for views
that are not currently loaded. For a list of all currently loaded
views, use the visibleItemViews property.
- (NSInteger)indexOfItemViewOrSubview:(UIView *)view
This method gives you the item index of either the view passed or the
view containing the view passed as a parameter. It works by walking up
the view hierarchy starting with the view passed until it finds an
item view and returns its index within the carousel. If no
currently-loaded item view is found, it returns NSNotFound. This
method is extremely useful for handling events on controls embedded
within an item view. This allows you to bind all your item controls to
a single action method on your view controller, and then work out
which item the control that triggered the action was related to. You
can see an example of this technique in the Controls Demo example
project.
Hope this helps !! :)
A bit late, but for others, the following delegate method works for me (with multiple carousels)
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
NSLog(#"%s", __FUNCTION__);
if (carousel == carousel2) {
NSLog(#"here");
long (or int) currentIndex = carousel.currentItemIndex;
}
}

Paging in uitableview

I am using a UITableview.In that there are nearly 100 records.I want to show 10 records at a time after that i will click on a button or UIPageControl which will show the next 10 records.So,please suggest me how can i do it?
If you just wanna create a table like that you could follow the below outlines.
In your button click Append or change the data in your data source array of your tableview. (Depending upon your requirement)
[self.myDataSourceArray addObjects:#"obj", #"obj2", nil];
Reload the table
[self.tableView reloadData];
UPDATE FOR THE COMMENT:
OK let me add some code,
//Consider this method is called when you click the next button
- (void)nextButtonAction
{
//Remove the older objects
[self.myDataSourceArray removeAllObjects];
//Add your new objects to the array
[self.myDataSourceArray addObjects:#"obj", #"obj2", nil];
//Reload the table
[self.tableView reloadData];
}
You could have a forward and backward button in the navigation bar with the page number
Define a rect would you like to scroll and then call method scrollRectToVisible:newRect for UITableView
CGRect newRect = CGRectMake(0, 0, tableView.frame.size.width, tableViewCell.height*10);
[tableView scrollRectToVisible:newRect animated:YES];
I am not sure if it is a good thing to introduce a pagination mechanism in your case. Because a UITableView should be fine for handling around 100 records. Just think of your Addressbook which can easily contain more than 100 records.
If you have a big number of records in a UITableView filtering and the index on the right with the letters of the Alphabet can help you to quickly navigate to the desired entry.
However, if you do decide to add more records to one table a common practize is to add a "Load more entries" cell at the very bottom of the table view.
For Example: Check out the Top50 Apps in the AppStore-App.
This really goes against Apple's User Interface Guidelines. TableViews are designed with built in highly efficient pagination (reusable cells). I have TableViews with 4000 records and when the data model behind the TV is done right, it can be very responsive.
If you are dead set on this, you could use a button on the NavBar, Toolbar, etc and use that to call scrollToRowAtIndexPath.
you can keep a count of the number of times the button is pressed
int i=0;
-(void)buttonPressed{
i=i+10;
[table reloadData];
}
then in
cellForRowAtIndexPath
{
cell.textLabel.text = [NSString stringWithFormat:#"%#",[Arr objectAtIndex:i+indexPath.row]];
}

Save data from 1 page and add that data to a tableView

Hey guys i have 2 views the first has a UITableView. The Second Has a textField and when the user presses a "Save" button on the second page, i want the textFields text to be added to the tableView. Here is the code i'm using
- (IBAction)saveButton:(id)sender {
CheckListPracticeViewController * obj = [[CheckListPracticeViewController alloc]init];
[obj.cells insertObject:textField.text atIndex:0];
[self dismissModalViewControllerAnimated:YES];
NSLog(#"%# "[cells objectAtIndex:0]);
[obj.myTableView reloadData];}
For some reason the data isnt being added to the table View does anybody know whats wrong?? Also the NSLog doesnt Work in this method. Thanks a lot Guys :D
That is because you are creating a new instance of CheckListPracticeViewController and updating it rather than the current one which has presented this view controller modally.
Change the first line to,
CheckListPracticeViewController * obj = (CheckListPracticeViewController *)self.parentViewController;
EDIT
First of all be consistent with your data model. If you are loading an array of dictionaries from the plist and later adding strings into that array then you have a serious problem. I will suggest that you create a dictionary object with name and other stuff and add that to the array. I would say doing [obj.cells insertObject:[NSDictionary dictionaryWithObject:textField.text forKey:#"name"] atIndex:0]; instead of [obj.cells insertObject:textField.text atIndex:0]; will fix this current error but I doubt that will fix your problem.
You are each time the button is pressed you alloc/init a new obj, try adding the new data into your actual container.
It's definitely one of these.
Check if array cells is allocated. Most likely it is not. That is why it is not being added to the tableView. You can add it to the init method so that you initialize the array before adding to the array.
Try NSlogging the contents of the array at various points. This will let you know if its exactly what's going on. NSLog the array in IBAction of Add-method, viewWillAppear
You need to make sure that your array is allocated before adding data to it.
NSLog the content of textField.text before adding it to the array. I am guessing that it is not a property, and it is simply null.
Make sure you had assigned self.myTableView = tableView at the end of your cellForRowAtIndexPath
You are creating another instance of CheckListPracticeViewController. I assume thats where your table is. If you are going back to CheckListPracticeViewController (I assume you do since you use [self dismissModalViewControllerAnimated:YES]) you will have to pass your view controller as a week reference or use NSNotification or use NSUserDefaults to store and retrieve this object in the CheckListPracticeViewController.
EDIT
Pass the CheckListPracticeViewController by weak reference to the view that has UITextField.
example:
in the .h of your UITextField class create a controller reference.
#property(nonatomi,assign)CheckListPracticeViewController *controller;
then when you create your new UITextField controller class pass the reference to its creator throught controller instance.
//in CheckListPracticeViewController.m file
myEdiotr.controller = self;
Later use controller instance to save the text from the UITextField.
- (IBAction)saveButton:(id)sender {
[controller.cells insertObject:textField.text atIndex:0];
[controller.myTableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}

Add data to a table View [duplicate]

Hey guys i have 2 views the first has a UITableView. The Second Has a textField and when the user presses a "Save" button on the second page, i want the textFields text to be added to the tableView. Here is the code i'm using
- (IBAction)saveButton:(id)sender {
CheckListPracticeViewController * obj = [[CheckListPracticeViewController alloc]init];
[obj.cells insertObject:textField.text atIndex:0];
[self dismissModalViewControllerAnimated:YES];
NSLog(#"%# "[cells objectAtIndex:0]);
[obj.myTableView reloadData];}
For some reason the data isnt being added to the table View does anybody know whats wrong?? Also the NSLog doesnt Work in this method. Thanks a lot Guys :D
That is because you are creating a new instance of CheckListPracticeViewController and updating it rather than the current one which has presented this view controller modally.
Change the first line to,
CheckListPracticeViewController * obj = (CheckListPracticeViewController *)self.parentViewController;
EDIT
First of all be consistent with your data model. If you are loading an array of dictionaries from the plist and later adding strings into that array then you have a serious problem. I will suggest that you create a dictionary object with name and other stuff and add that to the array. I would say doing [obj.cells insertObject:[NSDictionary dictionaryWithObject:textField.text forKey:#"name"] atIndex:0]; instead of [obj.cells insertObject:textField.text atIndex:0]; will fix this current error but I doubt that will fix your problem.
You are each time the button is pressed you alloc/init a new obj, try adding the new data into your actual container.
It's definitely one of these.
Check if array cells is allocated. Most likely it is not. That is why it is not being added to the tableView. You can add it to the init method so that you initialize the array before adding to the array.
Try NSlogging the contents of the array at various points. This will let you know if its exactly what's going on. NSLog the array in IBAction of Add-method, viewWillAppear
You need to make sure that your array is allocated before adding data to it.
NSLog the content of textField.text before adding it to the array. I am guessing that it is not a property, and it is simply null.
Make sure you had assigned self.myTableView = tableView at the end of your cellForRowAtIndexPath
You are creating another instance of CheckListPracticeViewController. I assume thats where your table is. If you are going back to CheckListPracticeViewController (I assume you do since you use [self dismissModalViewControllerAnimated:YES]) you will have to pass your view controller as a week reference or use NSNotification or use NSUserDefaults to store and retrieve this object in the CheckListPracticeViewController.
EDIT
Pass the CheckListPracticeViewController by weak reference to the view that has UITextField.
example:
in the .h of your UITextField class create a controller reference.
#property(nonatomi,assign)CheckListPracticeViewController *controller;
then when you create your new UITextField controller class pass the reference to its creator throught controller instance.
//in CheckListPracticeViewController.m file
myEdiotr.controller = self;
Later use controller instance to save the text from the UITextField.
- (IBAction)saveButton:(id)sender {
[controller.cells insertObject:textField.text atIndex:0];
[controller.myTableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}