how to delete data from same named but stored in different NSMutablerArray by referencing its name? - iphone

i have created one table view in which i have provided searching & deleting facility. My problem is that when i am not in searching mode the data is going to be deleted properly & shows the table properly after deleting data. but my problem is that i have two NSMutableArrays one is original and second one is searched NSMutableArray so when i delete data from searched Array at that time i want to delete data from original array also.
NSString *str = [searchedName objectAtIndex:indexPath.row];
NSLog(#"str:=%#",str);
[searchedName removeObjectAtIndex:indexPath.row];
[arrName removeObjectIdenticalTo:str];
so here my point is that i can't delete original data with use of removeObjectAtIndex:index.path because the searched array count will be different from original array count so from original array it will delete another value witch i have not deleted from searched value.
did you get my point? please mention me if you don't get my point.
how can i implement like this? please guide me.

You can first search the index from the original array
int index = [arrName indexOfObject:str];

If you have unique name in your array then delete that object directly like below...
[arrName removeObject:str];

Related

How can i save the order of an NSMutableArray

I have an NSMutableArray and I change the order of the contents of it. How can I save the order of it in core data without using a relation ship, or should I use NSUserDefaults?
Why not have a second array that is a copy of the first one at the time that you want to preserve its order? It need not be a mutable array - use [myArray immutableCopy]
If you don't want to copy all the items in the array for memory reasons, perhaps store the location as an NSInteger? Have the first array contain the items, and the second array contains indexes for the first array.

CoreData fetch entities with same values

I have a CoreData entity called Item with two values (well, two values relevant to this question).
Item
---------------
id - NSString
name - NSString
Every item has a unique ID and SHOULD have a unique name.
(BTW, the id is not used for CoreData it is used for communicating with the server).
There are a couple of items which appear to have duplicate names and I'm trying to find a query that returns all items that have an item in the table with a duplicate name.
Is this possible?
If so, can someone provide an NSPredicate (or method) to do this.
I do not think that it is possible to fetch exactly the items with duplicate names with a Core Data fetch request. (I think there was a similar question some time ago here on SO, but I cannot find it right now.)
You could fetch all items sorted by the name attribute. Then the duplicates can be found with a single loop over the result array.

duplicates data while adding into table

In my app when i insert data by clicking on add button data will be inserted into database and table.record will be inserted perfectly but when i add that data into table it duplicates the database data..here is the code:
NSString *insertData=[NSString stringWithFormat:#"insert into tbl_Bills(Amount,Note,Due_On) values ('%f','%#','%#')",addAmountInDouble,val,due];
[database executeQuery:insertData];
NSLog(#"inert query: %#",insertData);
NSLog(#"value inserted");
NSString *countQuery = [NSString stringWithFormat:#"Select Bill_Id,Entry_Date,Amount,Note,Due_On from tbl_Bills"];
NSArray *countArray1 = [database executeQuery:countQuery];
NSLog(#"Query:%#",countArray1);
[BillTableArray addObjectsFromArray:countArray1];
NSLog(#"Count array %#",countQuery);
NSLog(#"table array:%#",BillTableArray);
[database close];
[table reloadData];
count array is fine but table array duplicates the value..i also initialize table array in viewDidLoad() method.
first of all you need to clear your table array before adding new objects :
[BillTableArray removeAllObjects];
after add new updated objects :
[BillTableArray addObjectsFromArray:countArray1];
This is because you are not clearing data out of BillTableArray before adding the new data into it. You are rerunning your query across the entire table - thus retrieving all of your records again, including the ones you've already set up in BillTableArray on the initial view load.
The problem is this line:
[BillTableArray addObjectsFromArray:countArray1];
BillTableArray is already loaded with data from your initial viewDidLoad call, but the query you executed retrieves all of this data from the database again, and this line is adding all of those objects to the array for a second time. So you are probably seeing duplicates of everything except the newly added items.
You probably just need to do:
BillTableArray = [countArray1 retain];
or something similar (don't forget to retain/release objects appropriately to ensure you don't lose them or leak them, not sure how BillTableArray is declared.
BillTableArray presumably contains all the data you already had in the table. When you re-query the database to get your added value, you will be returning everything, then adding it all again.
So, either
Specifically add only the new objects to BillTableArray
Change your countQuery to only return the new object
Replace all the contents of BillTableArray with countArray1
[BillTableArray addObjectsFromArray:countArray1]; adds all the objects from array countArray1. So I guess the existing objects in BillTableArray are not removed before adding the new objects. This creates duplicates. To remove duplicates, just remove all the objects of BillTableArray and then add the objects from countArray1.
Just a small note, please make sure you name the variables starting with a lower case alphabet.

Shifting rows in a UITableView

I have looked at several forums and read different threads, but I can't seem to find an answer. I'd really like some help. I have an array that stores Labels whose contents I display in the UITableView's row. The second row contains fresh data added to the same array and so on. My question is, everytime the labels are displayed in the UITableView, I want it to be displayed on the top row. In essence, the older ones keeps shifting one row down. I understand I need to use: "tableView:moveRowAtIndexPath:toIndexPath:" but it only seems to work with arrays whose contents are strings.
Just to clarify, I'm not displaying each element of the array on a new line. I'm displaying all the elements of the array on a single row, then changing the elements and displaying that as the next row. Except I want the second row to be above the first and so on.
The array you're using is an ordered collection of object, so the easiest way to perform what you're looking fro would be to add the new object in the first position of your array. You can achieve that using the method insertObject:atIndex: of NSMutableArray
ex:
[myArray insertObject:newObject atIndex:0];
This way you don't have to worry about moving your tableView cells ordering.
Hope this helps,
Vincent
First of all you don't need to keep the label in some array, you can keep only the string. And when add an new object to the array add it on position 0 and the do and reload of table view.

iPhone UITableView populating variable rows sections from flat array

I thought that would be very common and easy iPhone App. In the main app I connect to database, retrieve values from database (NSDate converted to NSString)n and put into single array. Then in one of the views I populate UITableView with elements from the array. UITableView is grouped (sections). I step through array to discover number of sections (change section if new day). How do I retrieve correct element of array in cellForRowAtIndexPath? IndexPath.section and IndexPath.row seem useless as row starts count from zero for each section. If number of rows in each section was the same it would have been easy:
[arryData objectAtIndex:(indexPath.row)+indexPath.section*[tblMatchesView numberOfRowsInSection:indexPath.section]];
But number of rows in each section varies... :-)
Separate your data into arrays of arrays (based on the number of different days) once you get it from the database, that'd be the simplest solution...
How about storing different date sections in different arrays? For example, you can have an array A of array. You can loop through the original arrays yourself, if you found a new day, you just create a new array and put it into the array A. And then, when you loop over the cell, you can get the section number to get the correct array and based on the row number to get the correct elemenet in the array
It doesn't have absolute cursor but you can try utilizing
UILocalizedIndexedCollation class which is used to ease the "sectioning" of your data and proving the tableView delegate functions the required data such as the index titles, etc
Here's apple documentation link for it:
https://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalizedIndexedCollation_Class/UILocalizedIndexedCollation.html#//apple_ref/occ/cl/UILocalizedIndexedCollation