Show data in tableView from NSDictionary in NSMutableArray - iphone

I am trying store records in UITableView each cell on reload data from a form. For this when user press add button so so multiple labels should be added to 1st indexPath then on reclicking add button again multiple cells must be added to second cell retaining first.
I am looking to store my data in
MyArray ({
firstindex: 1st record
firstindex: 2nd record
}
{
secondindex : 1st record
secondindex : 2nd record
})
Records are comming from UITextView and saving into table on each Add button click on each row with multiple entries
- (IBAction) Add
{
[myDict setObject:countryTxt.text forKey:#"Country"];
[myDict setObject:cityTxt.text forKey:#"City"];
[myDict setObject:EscortsNumTxt1.text forKey:#"people"];
[myDict setObject:fromDate.text forKey:#"Datetogo"];
[myDict setObject:toDate.text forKey:#"Datetoreach"];
[moreArr addObject:myDict];
NSLog(#"moreDict %#",moreArr);
numberOfrows++;
[moreTable reloadData];
}
I have implemented through this way but showing repeated data on TableView Cell, I am not including frame of UILabel code, as only facing problem in fetchng
- (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] autorelease];
}
countrylbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"Country"];
citylbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"City"];
people.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"people"];
toDateLbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"Datetogo"];
fromDteLbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"Datetoreach"];
}

Try to retrieve the array once like in your methos
- (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] autorelease];
}
NSArray *results = [[NSArray alloc] initWithArray:[moreArr objectAtIndex:indexPath.row]];
NSLog("%#",results); //Check that everything is in the right place
// DO STUFF
[results release];
}

Solution as NSMutableDictionary is overriding the content I dont know why so I allocated it within the function
moreTableDict = [[NSMutableDictionary alloc] init];
[myDict setObject:countryTxt.text forKey:#"Country"];
[myDict setObject:cityTxt.text forKey:#"City"];
[myDict setObject:EscortsNumTxt1.text forKey:#"people"];
[myDict setObject:fromDate.text forKey:#"Datetogo"];
[myDict setObject:toDate.text forKey:#"Datetoreach"];
[moreArr addObject:myDict];
This works for me

Related

Correct Approach to Populating a Table View Using An Array?

I'm currently trying to implement a Table View on my iPhone app that grabs an array from one table view/class and using this array to populate a table in another seperate view. Here is the method I use that adds an exercise to an array if it is clicked/tapped:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
NSUInteger *index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
[array insertObject:str atIndex:index];
self.workout = array;
[array release];
}
Once the save button is pressed, this array will be stored in an array of workouts (arrays) that I want to be populated in another view. Am I taking the correct approach this?
Here is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SectionsTableIdentifier ] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
you probably do not want to re-initialize self.workout with a new array each time.
-(void) viewDidLoad
{
self.workout = [[NSMutableArray alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
[self.workout insertObject:str atIndex:0];
}

How can I display all the rows from a database in a tableview

I am using this code to display all the rows from database, but when the tableview loads only the last row shows up.. what am I doing wrong. Please help!!
- (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] autorelease];
}
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Event" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchResults)
{
NSLog(#"Cant Fetch");
}
//compare fetched data with user input abd login if match
for(Event *ev in mutableFetchResults)
{
NSString *cellValue=[ev VidField];
NSString *cellValue1=[ev ImgField];
NSLog(#"%#",cellValue);
NSLog(#"%#",cellValue1);
[cell.textLabel setText:cellValue];
cell.image = [UIImage imageNamed:cellValue1];
}
return cell;
}
you should do like this;
in .h file;
NSMutableArray *myFetchResults;
in .m file;
-(void)getAllDataFromDatabase{ // call it to get data
myFetchResults = [[NSMutableArray alloc] init];
//database connection codes
myFetchResults = mutableFetchResults; // now you have an array with all objects.
}
in UITableView cellForRowAtIndexPath delegate method:
{
Event *ev = (Event*)[myFetchResults objectAtindex:indexPath.row];
cell.textLabel.Text = [ev VidField];
cell.image = [UIImage imageNamed:[ev ImgField]];
}
i think this will work.
This code repeatedly sets different values to the same cell instance, so it will end up set to whatever the last event in your fetch results is. Your cellForRowAtIndexPath method should be using the indexPath.row to set the values for a cell from the specific event at that point in your mutableFetchResults - if this is an array, it would be something like:
Event *ev = [mutableFetchResults objectAtIndex:indexPath.row];
NSString *cellValue=[ev VidField];
NSString *cellValue1=[ev ImgField];
NSLog(#"%#",cellValue);
NSLog(#"%#",cellValue1);
[cell.textLabel setText:cellValue];
cell.image = [UIImage imageNamed:cellValue1];
return cell;
It is hard to give any more details without seeing more code, but this should set you on the right path.
You're just overwriting the value in cell.textLabel. If your for loop were unfurled it would look something like this:
cell.textLabel = row1Text
cell.textLabel = row2Text
...
cell.textLabel = rowNText
You should set your cell text in – tableView:cellForRowAtIndexPath: using the indexPath.row to select which record you want.
You should use if(Condition) for it. Because, for() iterate until last record will be get and set record's data.
i think at first take your mutableFetchResults in viewdidload then retain the [mutableFetchResults retain].
then number of rows of table will be [mutableFetchResults count]
then in make this code...
- (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] autorelease];
}
NSString *cellValue=[indexpath.row VidField];
NSString *cellValue1=[indexpath.row ImgField];
NSLog(#"%#",cellValue);
NSLog(#"%#",cellValue1);
[cell.textLabel setText:cellValue];
cell.image = [UIImage imageNamed:cellValue1];
return cell;
}
if any code you do not understand then tell me...because i faced same problem...if you want to see more the see my solution....What will be the numberOfRowsInSection return type for uitableview when XML data is retrieved?

Not able to access array with ids in another view controller in iphone

these is some code from my project..
for (index=0; index<[feed count]; index++) {
//NSLog(#"........%#",[feed objectAtIndex:2]);
NSString* forName=[feed objectAtIndex:index];
NSString *nameString = [forName valueForKey:#"name"];
NSLog(#"--------%#",nameString);
[nameArray addObject:nameString];
NSString *idString = [forName valueForKey:#"id"];
NSLog(#"--------%#",idString);
[idArray addObject:idString];
}
MyFriends *detailViewController = [[MyFriends alloc] initWithNibName:#"MyFriends" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailViewController.fbNames=nameArray;
detailViewController.fbIds=idArray;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
My problem is in another controller when I am accessing array with names which is fbNames in another class it is showing me proper names,But when I am accessing fbIds which is the array with Ids ...they are showing me null..
how to correct this?
here...in this class ...fbNames are working fbIds are not..
- (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 [fbNames count];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell...
cell.textLabel.text=[fbNames objectAtIndex:indexPath.row];
NSLog(#"%#",cell.textLabel.text);
return cell;
}
NSString* forName=[feed objectAtIndex:index];
NSString *nameString = [forName valueForKey:#"name"];
I don't think NSString has valueForKey:

data loading in UITableView like in iBooks search table

How can I tell UITableView to re-render itself when I add some new data?
I am executing this code ib loadView method.
[NSThread detachNewThreadSelector:#selector(addSomeData) toTarget:self withObject:nil];
This is addSomeData method:
-(void)addSomeData{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i = 1; i <= 100; i++) {
[myArray addObject:[NSString stringWithFormat:#"Page %i",i]];
NSDate *d2 = [NSDate dateWithTimeIntervalSinceNow:0.2f];
[NSThread sleepUntilDate:d2];
}
[pool release];
}
- (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] autorelease];
}
NSString *string = [myArray objectAtIndex:indexPath.row];
cell.textLabel.text = string;
// Configure the cell...
return cell;
}
myArray ist is NSArray which contains string.
SO the main question is - how to dynamically update tableview when I add new NSString to myArray?
[self.tableView reloadData] method is one solution that completly reload the table and [self.tableView insertRowAtIndexPath:withAnimation] is another that only insert the new row and animates the change.
Try : [self.tableView reloadData];

I can't fill the cells of my UITableView with a NSArray (Normally it always works, but not in the case)

I have a UITableView with Cells that are filled with the Objects of an NSArray. I´m using the MGTwitterEngine to get Tweets from users. I want to show this tweets in the TableView. Here is the method that is called when the MGTwitterEngine has received all tweets ("namen" and "nachricht" are NSArrays):
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
{ NSLog(#"Got statuses for %#:\r%#", connectionIdentifier, statuses);
NSDictionary *userDict = [statuses valueForKey:#"user"];
namen = [[NSArray alloc] initWithObjects: [userDict valueForKey:#"name"], nil];
nachricht = [statuses valueForKey:#"text"];
NSLog(#"%#", namen);
NSLog(#"%#", nachricht);
[table reloadData];
}
Then I did this to fill the cells with the names of the Twitter-users:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [nachricht count];
}
- (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];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
return cell;
}
Unfortunately, the app crashes whenever the table is reloaded: [table reloadData];. I know that the NSArray got all the right values, because NSLog shows me that. I've tried also to create a other NSArray, like that:
arryData = [[NSArray alloc] initWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
And I written this into the cellforRowAtIndexPath-methode and and everything worked well. Why does it not work with the "namen"-NSArray?
The NSArray is probably being released - have you examined what's at its memory address?