iOS Removing Object From Array - ios

I am loading an array of items from a plist. A user can select any item and add it to a new array of items which is shown on screen.
I am doing this but a user can also delete a player from any row. My problem is when a user deletes the object it shuffles the list up. Where as I want to keep that row blank.
My deletion code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *user_id = [prefs objectForKey:#"user_id"];
NSString *fixture_id = [prefs objectForKey:#"fixture_id"];
// Get path to documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
[self.usersSelectionArray removeObjectAtIndex:indexPath.row];
if (self.usersSelectionArray == nil) {
// disable swipe to delete?
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([paths count] > 0)
{
// Path to save array data
NSString *arrayPath = [[paths objectAtIndex:0]
stringByAppendingPathComponent:[NSString stringWithFormat:#"%#-%#",user_id, fixture_id]];
// Write array
[self.usersSelectionArray writeToFile:arrayPath atomically:YES];
}
// If blank array delete the file as not required
if (self.usersSelectionArray.count == 0) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#-%#", user_id, fixture_id]];
NSError *error;
if(![[NSFileManager defaultManager] removeItemAtPath:path error:&error])
{
//TODO: Handle/Log error
}
}
[self.tableView reloadData];
}
It also checks if the count is 0 and deletes the plist, re-creates on load if required.

So you want to remove an object from your data array, but keep an empty cell for it in the table?
In that case I recommend replacing the removed object with an NSNull instance.
[myMutableArray replaceObjectAtIndex:index withObject:[NSNull null]];
You can then check if the object for a cell is NSNull and return an empty cell in the cellForRowAtIndexPath: data source method.
id object = // get the correct object from your data array, depending on the index
BOOL isEmpty = [object isKindOfClass:[NSNull class]];
if (isEmpty) {
// return empty table cell
else {
// return a normal cell
}

Instead of [self.usersSelectionArray removeObjectAtIndex:indexPath.row];, try [self.usersSelectionArray replaceObjectAtIndex:indexPath.row withObject:[NSNull null]];. That will keep the row but it will contain practically nothing

Related

Populating TableView with Plist Documents Directory in iOS

I'm a newbie to Objective-C so any help is greatly appreciated.
I have a TableView that is successfully pulling back the list from my Data.plist file within my Xcode project but I need it to pull from the Documents Directory.
I have seen many posts about this but can't seem to get it to work for me.
Here is my .m file below. Like I said, it pulls back the data but I need it to be dynamically changed based on my Documents plist copy.
Thanks in advance!
#synthesize content = _content;
- (NSArray *)content
{
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"]];
}
return _content;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:#"Name"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:#"Score"];
return cell;
}
Try this:
Step 1: In .h File declare a global variable
#property (nonatomic,retain) NSMutableDictionary *contents;
Step 2: In .m file set synthesis.
#synthesize contents;
Step 3: Move plist file from mainbudle into documents directory
-(void) createPlistDocuments
{
// Get path to documents directory
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Finds the contained Documents directory
NSString *documentsDirectory = [arrayPaths objectAtIndex:0];
NSError *error;
// Create an object that we will later use to look for a file and return a boolean value on whether or not it exists
NSFileManager *manager = [NSFileManager defaultManager];
// File we want to move, stored in original top level directory
NSString *demoFile = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
// Define where we want it moved to and name it
NSString *demoFileMoved = [NSString stringWithFormat:#"%#/data.plist", documentsDirectory];
// Attempt the copy
if ([manager copyItemAtPath:demoFile toPath:demoFileMoved error:&error] != YES)
NSLog(#"Unable to move file: %#", [error localizedDescription]);
}
Step 4: Read plist data from document directory
- (void)viewDidLoad
{
[super viewDidLoad];
[self createPlistDocuments];
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:#"%#/data.plist",documentsDirectoryPath];
contents = [NSArray arrayWithContentsOfFile:path];
NSLog(#"%d", contents.count);
}
For brief explanation follow this sample... TechDevMobile(IOS-Message-Chat)

Add Input from UITextField to Array

I'll try to preface this as clearly as possible. I have a simple .plist stored in the Documents folder with two different keys. I'm using an AlertView UITextField to receive user input which should be added to the correct key. Using the setObject:forKey returns NSMutableArray may not respond to setObject:forKey. With my current code below I can write to my .plist but the problem is it's changed from a Dictionary to a simple Array. How would I implement a writeToFile for a specific key with this setup? Or on the flip side, am I approaching this from the wrong direction?
In my h
#interface RootViewController: UIViewController <UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate> {
UINavigationBar *navBar;
UITableView *myTable;
NSMutableArray *info;
}
#property(nonatomic, retain) NSMutableArray *info;
#end
And in my m. Yes this is being done without the use of IB completely in code
//************* Add New Cell To TableView When OK is Chosen
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
UITextField *newDevice = [alertView textFieldAtIndex:0];
NSLog(#"newDevice - %#",newDevice.text);
if (alertView.tag == 1 && buttonIndex == 1){
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"stuff.plist"];
[self.info writeToFile:path atomically:YES];
[myTable reloadData]
UPDATE
A little insight on how I've managed to put myself into this situation
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"stuff.plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
self.info = [dict objectForKey:#"Area"];
Above code is how I've managed to populate my TableView so I'm possibly going about this wrong. NSMutableDictionary Doesn't respond to removeObjectAtIndexPath and I'd like to be able to delete a cell from my TableView.
I'm assuming you want to update the key Area in the property list, but keep other properties unchanged.
To accomplish this, keep the dictionary representation around, e.g.,
NSMutableArray *info;
NSMutableDictionary *dict; // add instance variable
and
dict = [NSMutableDictionary dictionaryWithContentsOfFile:path]; // remove local variable
Then, when writing:
[dict setObject:info forKey:#"Area"];
[dict writeToFile:path atomically:YES];
(You could also make dict a property, and you could remove the info variable, instead implementing its accessors to work on the key in the dictionary.)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"stuff.plist"];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
UITextField *newDevice = [alertView textFieldAtIndex:0];
NSLog(#"newDevice - %#",newDevice.text);
if (alertView.tag == 1 && buttonIndex == 1){
NSMutableArray *existing = dictionary[#"Area"];
[existing addObject:newDevice.text];
[dictionary setObject:existing forKey:#"Area"];
[dictionary writeToFile:path atomically:YES];
[self.info addObject:newDevice.text];
[myTable reloadData];

Objective-C: How to refresh a UITableView?

I have some code that displays file name in my uitableview. however once deleting a file and refreshing i receive an error. Here is my code to display my file name, my delete button actions and the error:
Firstly when a button is pressed i run this code which works when adding a file to the table:
-(IBAction)refresh{
[[self mytable] reloadData];
}
Secondly I have this code to get and display the values the table is going to display. This works fine until the deletion and update occurs:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy];
mytable.dataSource = self;
mytable.delegate = self;
}
-
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(!filePathsArray)
{
return 0;
}
if ([filePathsArray count] > 0)
return [filePathsArray count];
else
return 0;
}
-
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
NSString *currentFileName = filePathsArray[indexPath.row];
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:currentFileName];
fileURL = [NSURL fileURLWithPath:filePath];
NSLog(#"urlstring %#",fileURL);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:filePath error:NULL];
NSLog(#"successfully deleted");
}
}
-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
NSLog(#"urlstring %#",[filePathsArray objectAtIndex:indexPath.row]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];
filePathsArray = [[NSArray alloc] initWithArray: [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy]];
return cell;
}
-
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
The error i receive is this:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** First throw call stack:
(0x2049012 0x1473e7e 0x1ffeb44 0x3c3a 0x69b8fb 0x69b9cf 0x6841bb 0x694b4b 0x6312dd 0x14876b0 0x2c84fc0 0x2c7933c 0x2c79150 0x2bf70bc 0x2bf8227 0x2bf88e2 0x2011afe 0x2011a3d 0x1fef7c2 0x1feef44 0x1feee1b 0x23dc7e3 0x23dc668 0x5e0ffc 0x2212 0x2145)
libc++abi.dylib: terminate called throwing an exception
(lldb)
I receive this error due to:
NSLog(#"urlstring %#",[filePathsArray objectAtIndex:indexPath.row]);
but once removed it says the problem is with the next line. Can anybody help?
Because of the way you're setting data in filePathsArray, your tableView is rendering one extra cell more than needed.
After you delete a file, update your filePathsArray.
What happens is that in your commitEditingStyle method you delete a file, but you do not update the filePathsArray object to remove the subpath for that file.
The only place where you update filePathsArray is in cellForRowAtIndexPath which is called after numberOfRowsInSection
So basically:
E.g. your filePathsArray contains subpaths for 6 files.
You delete a file in commitEditingStyle but you don't update filePathsArray (which still contains 6 subpaths).
You press the button to reloadData
numberOfRowsInSection is called to get the number of cells to be displayed on your UITableView... This returns [filePathsArray count] which is still 6.
cellForRowAtIndexPath is now called for every row.
Inside the above method, you call filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]; so now your filePathsArray has 5 objects because of the deleted file.
But guess what.... Your UITableView has already started rendering 6 rows for the old filePathsArray count.
Now when it renders cell number 6 (indexPath.row = 5), it calls cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent]; and the last index of your array is now 4. This causes the crash.
Simply do filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]; in your commitEditingStyle and the error should stop.
Also, try not modifying your data source object in cellForRowAtIndexPath. Take the filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]; call out of there as your array is now being updated in commitEditingStyle.
In tableView:cellForRowAtIndexPath:, remove this code: filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
In tableView:commitEditingStyle:,add this code:
[filePathsArray removeObjectAtIndex:indexPath.row];
This should maintain the integrity of the data you're using to populate the table view (so you don't change the number of items when the table doesn't know about it and you do change it when the table thinks you have).

ios - delete single files that are downloaded into the documents directory

Firstly = I apologize because I have already tried to ask this once before here
I am really struggling with this:
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source.
[_mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
By using the code above I know it is possible to delete an entry from an array that is displayed in a UITableView. However I am wanting to delete files form my Documents Directory that are downloaded by the user and no longer required.
Now I in the mean time and after more searching this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *file = [[NSString alloc] init];
for(int i=0; i<[paths count]; i++)
{
file = [documentsDirectoryPath stringByAppendingFormat:#"/%#",_fileArray];
NSLog(#"%#", file);
}
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:file error:NULL];
Allows me to list all the files that are in the array in my console and this code:
- (void)removeFile:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.mp3", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(#"image removed");
}
together with: [self removeFile: _filename]; allows me to delete a specific file.
so I am making head way. But I am really stuck when it comes to a user being able to swipe and delete a file. Of Course I do not know what file is going to be in the directory.
Secondly - How do I handle being able to load the tableView once all the files are deleted?
If I do that using this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0)
{
NSLog(#"Path: %#", [paths objectAtIndex:0]);
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
// Remove Documents directory and all the files
BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&error];
}
I get the an termination error - I think that is because the directory has also been removed.
I know there is a bit of code here, but I really hope someone and guide me through this:-)
If i correctly understood, you want step by step guide.
Lets consider that you have a working UITableView (mainTableView) that take data from NSMutableArray (mainArray).
This will return document directory path.
-(NSString*)filePath{
NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSLog(#"%#",path);
return path;
}
Somewhere we need to init array ,i did it on viewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
mainDataArray=[[NSMutableArray alloc]init];
[self loadContentsOfDirectory:[self filePath]];
}
//here i am adding names of files from documents directory
-(void)loadContentsOfDirectory:(NSString*)path
{
NSError *error=nil;
NSArray *pathArray=[[NSFileManager defaultManager]contentsOfDirectoryAtPath:path error:&error];
if (error) {
NSLog(#"ERROR: %#",error);
}else{
if (pathArray) {
[mainDataArray removeAllObjects];
[mainDataArray addObjectsFromArray:pathArray];
}
}
}
#pragma mark UITableView_Methods
//delete file then if file is deleted , remove filename from array and remove cell
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSString *fileName=[mainDataArray objectAtIndex:indexPath.row];
NSError *error=nil;
NSString *pathToDelete=[[self filePath]stringByAppendingPathComponent:fileName];
BOOL succes=[[NSFileManager defaultManager]removeItemAtPath:pathToDelete error:&error];
if (error) {
NSLog(#"ERROR: %#",error);
}
// if file is succes deleted
if (succes) {
//remove this item from array
[mainDataArray removeObject:fileName];
//and remove cell
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}else{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:#"Alert!" message:#"File can not be deleted" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [mainDataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reusableCell=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reusableCell];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableCell];
}
cell.textLabel.text=[mainDataArray objectAtIndex:indexPath.row];
return cell;
}
I think your error is that you do not remove something maybe cell, object in array.
Hope this will help.
If you want to delete selected files from your Documents directory then.
First you have to get all files from Documents directory of your app like this
-(NSArray *)listFileAtPath:(NSString *)path
{
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
return directoryContent;
}
Then use return array from above function to display files to UITableView
NSArray *filesArray = [self listFileAtPath:documentDirectoryPath];
And finally when you delete selected files then you have to remove that file from Documents directory and also from filesArray for managing UITableView something like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source.
[_mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Remove file from document directory
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&error];
// Remove file entry from filesArray
[filesArray removeObjectAtIndex:indexPath.row];
}
}
Hope will help you if you want to do something like this.

Load Plist in UITableView for only 10 row

Actually this is to follow up my question before
UITableView show only first row.
Now my problem is that I only want to view the 10 list in my plist. If there are 11 items, the first item will be replace by the second and so on so my list only 10 items.
And this is my code to save to the plist:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == array) {
array = [[NSMutableArray alloc] init];
}
NSMutableArray *list = [[NSMutableArray alloc]init];
[list addObject:resi.text];
[array addObject:list];
[array writeToFile:plistPath atomically: TRUE];
And this is the whole code for my table view that have been modified
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *list = (NSArray *)[self.array objectAtIndex:indexPath.row];
if(list && [list count] > 0) { //to check and avoid any crash
cell.textLabel.text = [list objectAtIndex:0];
}
// Configure the cell...
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
array = [NSMutableArray arrayWithContentsOfFile:plistPath];
[myHistoryTable reloadData];
}
if ([array count] > 10) {
array = [array subarrayWithRange:NSMakeRange([array count] - 10, 10)];
}
if you don't want to overwrite the original array create a second one which serves as the dataSource of the tableView:
array = /* load from plist */;
if ([array count] > 10) {
self.dataSourceArray = [array subarrayWithRange:NSMakeRange([array count] - 10, 10)];
}
else {
self.dataSourceArray = array;
}

Resources