duplicate entries in tableview
I have implemented the table view in my iphone app. In this I am using the date picker to put date entries in table view. But some time user by mistake set to put the entry which is already existing. How should I put the validation so that duplicate 开发者_运维百科entry don't exist.
Please provide me some idea or solution.
Thanks alot.
Before adding a new entry I would just take all the entries I have so far and put them in an NSMutableSet
or NSSet
For example lets say all your elements are in the array myArray then:
NSSet* mySet;
[mySet addObjectsFromArray:myArray];
and then check the new Object with
if (![mySet containsObject: newObject]) {
//do whatever you need to
} else {
//the object is already contained
}
Before refreshing tableview to display your new entry inserted, you should check your array from which you are populating your table. Try compare there & decide wether its already exists. if its is don't add. Hope this helps.
try this one; before reloading the table put this operation in your array
NSSet *uniqueNames = [NSSet setWithArray:HistoryArray];
storyArray = [[NSMutableArray alloc] initWithArray:[uniqueNames allObjects]];
精彩评论