get some Error with creating and inserting object of array
I am new in iPhone and now I am struggling to create and inserting object. I explain first
Here I have one tableview with some cell value which is getting from my array whose name is appDelegate.array1
now I can change some value in table view cell and now after that I want to insert this new cell value in same array appDelegate.array1. So how can I do this.
I tried to开发者_开发知识库 do it like this.
-(void)viewWillAppear:(BOOL)animated
{
[appDelegate.array1 addObject:indexPath];
[tableView reloadData];
}
it is correct or not if yes then why my application will terminate and if not then please give me correct method to add object in array
Note this points.
1) Check if your array is Mutable array...Then only you can add new items to array at run time 2) What is indexPath in your code, which is bit confusing..
3) To add object you use these (or more)functions
[yourArray addObject:your_object];
Or to insert into the array you can use
[yourArray insertObject:your_object atIndex:your_index];
Hope this help
Instead of reloading entire table view for adding just one item, Just add the element to the array and update the table view....
-(void) addObject : (id) inObject
{
if(inObject)
{
[appDelegate.array1 addObject:inObject];
[mTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
}
How are you declear your array?
you need to make it a NSMutableArray
.
NSMutableArray *array1;
Your method is right but there can be number of problems in ur logic.
Like you add indexPath
. (what is in indexPath
?) is it nil
or something like that.
your appDelegate
object correctly initialized or not.
There is certainly a problem in your logic.
精彩评论