How to add data to TableView on iphone? - iphone

I defined an array for tableView's listData.
NSArray *array = [[NSArray alloc] initWithObjects:#"Sleep", #"Bashful", #"Happy", nil];
self.listData = array;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
cell.text = [listData objectAtIndex:row];
Now I want to add more data to the tableview. How to connect another data to it so when I click a button the array will be #"A", "B", "C", "D", nil?

The easiest way to do this is to use the NSMutableArray with insertObjetcAtIndex or addObject:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"Sleep", #"Bashful", #"Happy", nil];
[array insertObject:#"XYZ" atIndex:3];
[array addObject:#"ABC"];

Related

NSMutableArray is not replaced object?

I want to replace an array object in NSMutableArray obect.
I am getting oldArray from _alertsArray and changing its value and want to replace oldArray with newArray . but there is no effect!!
NSArray *oldArray = (NSArray *)[_alertsArray objectAtIndex:[indexPath row]];
NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:oldArray copyItems:YES];
[newArray replaceObjectAtIndex:3 withObject:#"YES"];
[_alertsArray replaceObjectAtIndex:[indexPath row] withObject:newArray];
NSMutableArray *newArray = [_alertsArray mutableCopy];
[newArray replaceObjectAtIndex:3 withObject:#"YES"];
_alertsArray = newArray;

how can print array json values into uitableview in iphone

JSON format code looks like this:
{
"rates": {
"amc": "201",
"hyd": "500.50",
"guj": "200.10",
"afgd": "400"
}
}
After parsing JSON values the above code array returns:
array = [values valueForKey:#"rates"];
which array return
{
amc = "201";
hyd = "500.50";
guj = "200.10";
afgd = "400";
...........etc
}
But i want to print in UITableView look amc:201
How can i do this?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
values = [responseString JSONValue];
array = [[NSMutableArray alloc] init];
NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
NSMutableArray *arrValues = [[NSMutableArray alloc] init];
array =[values valueForKey:#"rates"];
NSLog(#"array values:--> %#",array);
// NSLog(#"values:--> %#",values);
// NSLog(#"Particular values:--> %#",[[values valueForKey:#"rates"] valueForKey:#"AED"]);
tempDict1 = (NSMutableDictionary *)array;
NSArray *arr;// =[[NSArray alloc]init];
arr = [[tempDict1 valueForKey:#"rates"] componentsSeparatedByString:#";"];
NSLog(#"arr-->%#",arr);
NSString *subStar = #"=";
[arrTitle removeAllObjects];
[arrValues removeAllObjects];
for (int i=0; i<[arr count]-1; i++)
{
[arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])-1]];
[arrValues addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])]];
NSLog(#"arrTitle is:--> %#",arrTitle);
}
tempDict1 = (NSMutableDictionary*)[array objectAtIndex:0];
array = [values valueForKey:#"rates"];
NSLog(#"tempDict--%#",[tempDict1 objectForKey:#"AED"]);
[array retain];
[tbl_withData reloadData];
}
try:
NSMutableArray * array2=[[NSMutableArray alloc]init];
[array addObject: [[array objectAtIndex:0] objectForKey:#"amc"]];
[array addObject: [[array objectAtIndex:0] objectForKey:#"hyd"]];
[array addObject: [[array objectAtIndex:0] objectForKey:#"guj"]];
[array addObject: [[array objectAtIndex:0] objectForKey:#"afgd"]];
now put array2 values in your table cell.
like:
cell.text=[array2 objectAtIndex:indexPath.row];
try this:-
NSDictionary *dict =[values valueForKey:#"rates"];
NSString *str=[dict valueForKey:#"amc"];
You need to do this:-
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
values = [responseString JSONValue];
NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
NSMutableArray *arrValues = [[NSMutableArray alloc] init];
NSDictionary *dict =[values valueForKey:#"rates"];
NSString *str=[dict valueForKey:#"amc"];
rest you can do according to your requirements.
Return type for [values valueForKey:#"rates"] is a dictionary not array of values.
NSDictionary *dict = [values valueForKey:#"rates"];
Now you can refer get values within dictionary using objectForKey
Or else if you want to store in array.
NSMutableArray *array = [NSMutableArray array];
for (NSString *key in [dict allKeys])
{
array = [dict objectForKey:key];
}
So finally array has all the values within dictionary.
// returns no of rows
-(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section
{
return [array count];
}
// contents of row will be
-(UITableViewCell*) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
static NSString *CellIdentifier = #"test";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSString *value = [self. array objectAtIndex:indexPath.row];
cell.textLabel.text = value;
return cell;
}

NSArray and tableview

I'm making a navigation-based application, where i have a file called DataService.m and my RootViewController
.m
in my DataService.m I'm calling
-(NSArray*)loadData
{
AlarmItem *item1 = [[[AlarmItem alloc] initWithTitle:#"TEST2"] autorelease];
AlarmItem *item2 = [[[AlarmItem alloc] initWithTitle:#"TEST3"] autorelease];
AlarmItem *item3 = [[[AlarmItem alloc] initWithTitle:#"TEST4"] autorelease];
AlarmItem *item4 = [[[AlarmItem alloc] initWithTitle:#"TEST5"] autorelease];
AlarmItem *item5 = [[[AlarmItem alloc] initWithTitle:#"TEST6"] autorelease];
NSMutableArray *items = [NSMutableArray arrayWithObjects:item1, item2, item3, item4, item5, NULL];
return items;
}
and in my RootViewCoontroller.m i have
- (IBAction)RefreshAlarmList:(id)sender
{
XMLDataService *myXML = [[XMLDataService alloc] init];
[myXML loadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:#selector(RefreshAlarmList:)];
self.navigationItem.rightBarButtonItem = refreshButton;
[refreshButton release];
}
When I run my simulator the tableView is empty.. how can i display my items ?
Dont autorelease the function after it load to the tableview then autorelease it.Then how u are displaying the data to the tableview.give action as reload data in tableview.
You can try one thing.Here in the delegate method cellForRowAtIndexPath , in last u should return the cell.It may work.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = #"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. AlarmItem *item = [_items objectAtIndex:indexPath.row]; cell.textLabel.text = item.tagName;return cell;}
[myXML loadData];
This is returning an array, which presumably you wish to use as your table's data source, but you are doing nothing with it. You need to assign this to an array, which is a retained ivar in your view controller class, _items or items judging by your other comments:
self.items = [myXML loadData];

delete an item from a NSMutableArray for a specific key

I am trying to figure out how to delete one item from this NSMutableArray that I am using for a sectioned table. This is how I build the data source:
listOfItems = [[NSMutableArray alloc] init];
NSArray *appleComputers = [NSArray arrayWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:#"Computers"];
NSArray *otherComputers = [NSArray arrayWithObjects:#"HP", #"Dell", #"Windows", nil];
NSDictionary *otherComputersDict = [NSDictionary dictionaryWithObject:otherComputers forKey:#"Computers"];
[listOfItems addObject:appleComputersDict];
[listOfItems addObject:otherComputersDict];
I am trying to figure out how to delete, say "iPod" from this array? I have tried many things, among them the one below, but nothing worked.
[listOfItems removeObjectAtIndex:indexPath.row];
(problem here is that there is no reference for the actual section...)
Thanks!
UPDATE: This is where I delete the row:
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];
[section removeObjectAtIndex:indexPath.row];
[tblSimpleTable reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// some code...
}
}
Not sure why you're using a dictionary... doesn't seem like you need one. How about just an array of arrays?
listOfItems = [[NSMutableArray alloc] init];
NSMutableArray * appleComputers = [NSMutableArray arrayWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
[listOfItems addObject:appleComputers];
NSMutableArray * otherComputers = [NSMutableArray arrayWithObjects:#"HP", #"Dell", #"Windows", nil];
[listOfItems addObject:otherComputers];
then
NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];
[section removeObjectAtIndex:indexPath.row];
Even if you do need to use the dictionaries, it's still not a big deal... just use NSMutableArray instead of NSArray for your appleComputers or otherComputers and do this:
NSDictionary * section = [listOfItems objectAtIndex:indexPath.section];
NSMutableArray * sectionComputers = [section objectForKey:#"Computers"];
[sectionComputers removeObjectAtIndex:indexPath.row];
For your example, you'd remove all 'iPod' entries like this (note changes to mutable collections):
listOfItems = [[NSMutableArray alloc] init];
NSMutableArray *appleComputers = [NSMutableArray arrayWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
NSMutableDictionary *appleComputersDict = [NSMutableDictionary dictionaryWithObject:appleComputers forKey:#"Computers"];
NSMutableArray *otherComputers = [NSMutableArray arrayWithObjects:#"HP", #"Dell", #"Windows", nil];
NSMutableDictionary *otherComputersDict = [NSMutableDictionary dictionaryWithObject:otherComputers forKey:#"Computers"];
[listOfItems addObject:appleComputersDict];
[listOfItems addObject:otherComputersDict];
...
for (NSMutableDictionary *dict in listOfItems) {
[dict objectForKey:#"Computers" removeObject:#"iPod"];
}
Have you tried [array removeObject:#"iPod"]?

Copy NSMutablearray to another

I am trying to copy NSMutableArray to another but it does not show me anything at the UITableView:
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:#"one",#"two"];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
NSMutableArray *list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];
Nothing show up! What is wrong?
It crashes my app because I do not have nil at my NSMutableArray how can I add nil to it? addobject:nil does not work it crashes the app:
static NSString * DisclosureButtonCellIdentifier =
#"DisclosureButtonCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
DisclosureButtonCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: DisclosureButtonCellIdentifier]
autorelease];
}
NSUInteger row = [indexPath row];
NSString *rowString =nil;
rowString = [list objectAtIndex:row];
cell.textLabel.text = rowString;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
[rowString release];
return cell;
Your initial call to alloc an NSMutableArray will most likely crash, since you don't have a nil terminator in your argument list.
Also, you have a local variable, list, and a property, list. Make sure you're instantiating what you think you are. You might need to do this:
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:#"one",#"two", nil];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
self.list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];
This might help you:
NSMutableArray *result = [NSMutableArray arrayWithArray:array];
or
NSMutableArray *result = [array mutableCopy]; //recommended
There are a few problems... One problem is that you're using 'initWithObjects' and adding the previous array. This seems like unwanted behaviour since you most likely want to add the strings #"one" and #"two" to the array. You most likely intended to use initWithArray or addObjectsFromArray. (Doing it the way you did, will add the NSMutableArray (not its objects) to the list)
The second problem, when you use initWithObjects, you need to list each of the objects and then terminate the list with a nil value. (docs) In other words, you need to use...
NSMutableArray *objectsToAdd = [[NSMutableArray alloc] initWithObjects:#"One", #"Two", nil];
The problem might be that the local declaration of listin line 4 conflicts with the property.