(
{
"first_name" = Akash;
idprofile = 1;
iduser = 1;
"last_name" = Testing;
picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/1_Jellyfish.jpg";
"profile_picture_filepath" = "1_Jellyfish.jpg";
},
{
"first_name" = testing;
idprofile = 3;
iduser = 1;
"last_name" = tst;
picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/3_Penguins.jpg";
"profile_picture_filepath" = "3_Penguins.jpg";
},
{
"first_name" = test;
idprofile = 4;
iduser = 1;
"last_name" = test;
picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/4_Chrysanthemum.jpg";
"profile_picture_filepath" = "4_Chrysanthemum.jpg";
},
{
"first_name" = prashant1;
idprofile = 19;
iduser = 1;
"last_name" = kharade1;
picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/19_Koala.jpg";
"profile_picture_filepath" = "19_Koala.jpg";
},
{
"first_name" = Priyank;
idprofile = 68;
iduser = 1;
"last_name" = Jain;
picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/68_P.jpg";
"profile_picture_filepath" = "68_P.jpg";
},
{
"first_name" = sdasd;
idprofile = 106;
iduser = 1;
"last_name" = sdasd;
picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/";
"profile_picture_filepath" = "<null>";
}
)
this is my json response data. I want to display the six profiles(6 dictionaries) based on the "idprofile" key in a table view with first_name, last_name and the profile image. I am getting confused. Please help?
here's my code:
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *appDel = [[UIApplication sharedApplication]delegate];
navBar=[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[navBar setTintColor:[UIColor colorWithRed:0.0f/255.0f green:82.0f/255.0f blue:151.0f/255.0f alpha:1.0f]];
UIImageView *myImageView=[[UIImageView alloc] initWithFrame:CGRectMake(87, 3, 134, 33)];
myImageView.image=[UIImage imageNamed:#"sprofile.png"];
[navBar addSubview:myImageView];
//[myImageView release];
[self.view addSubview:navBar];
UIButton *logOutButton=[UIButton buttonWithType:UIButtonTypeCustom] ;
[logOutButton setFrame:CGRectMake(244, 10, 71, 32)];
//[logOutButton setTitle:#"Log out" forState:UIControlStateNormal];
[logOutButton setImage:[UIImage imageNamed:#"logout.png"] forState:UIControlStateNormal];
[logOutButton addTarget:self action:#selector(goToLoginScreen) forControlEvents:UIControlEventTouchUpInside];
[navBar addSubview:logOutButton];
UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 320, 30)];
[headerLabel setBackgroundColor:[UIColor colorWithRed:210.0f/255.0f green:221.0f/255.0f blue:228.0f/255.0f alpha:1.0f]];
[headerLabel setText:#"Currently selected profile: Please select"];
[headerLabel setTextAlignment:NSTextAlignmentCenter];
[headerLabel setFont:[UIFont fontWithName:#"helvetica" size:14.0f]];
[headerLabel setTextColor:[UIColor colorWithRed:78.0f/255.0f green:126.0f/255.0f blue:70.0f/255.0f alpha:1.0f]];
[self.view addSubview:headerLabel];
homeTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 81, 320, 367) style:UITableViewStylePlain];
[homeTable setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:237.0f/255.0f blue:240.0f/255.0f alpha:1.0f]];
homeTable.delegate=self;
homeTable.dataSource=self;
homeTable.rowHeight=100;
[self.view addSubview:homeTable];
profileArray = [[NSMutableArray alloc]init];
[self callWebService];
}
-(void)goToLoginScreen
{
AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [profileArray 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];
}
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
selectButton.frame = CGRectMake(100, 30, 63, 32);
//[selectButton setBackgroundColor:[UIColor yellowColor]];
[selectButton setBackgroundImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
//[selectButton addTarget:self action:#selector(selectButtonSelected: ) forControlEvents:UIControlEventTouchUpInside];
selectButton.tag = indexPath.row;
[cell.contentView addSubview:selectButton];
// NSDictionary *item = [profileArray objectAtIndex:[indexPath row]];
// [[cell textLabel] setText:[item objectForKey:#"first_name"]];
// Home *tempHomeObj=(Home *)[profileArray objectAtIndex:indexPath.row];
// NSString *fName = (NSString *)tempHomeObj.fName;
// NSLog(#"fnameeeee==>%#",fName);
//NSLog(#"fName==>%#",appDel.fName);
AppDelegate *appDel=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(#"profileeeee==>%#",profileArray);
// NSMutableString *tempStr = (NSMutableString *)profileArray;
cell.textLabel.text = [self.profileArray objectAtIndex:indexPath.row];
//cell.imageView.image = appDel.tempUserImage;
return cell;
}
-(void)callWebService
{
//adding the indicator view to show that data is being processed.......
indicatorView=[[UIView alloc] initWithFrame:CGRectMake(110 ,180,100 , 100)];
indicatorView.tag=3000;
[indicatorView setAlpha:0.7];
indicatorView.layer.cornerRadius=15;
indicatorView.backgroundColor=[UIColor blackColor];
[self.view addSubview:indicatorView];
//now adding the activity indicator
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//activityIndicator.frame = CGRectMake(20.0,20.0, 40.0, 40.0);
//activityIndicator.center = indicatorView.center;
activityIndicator.center=CGPointMake(50, 50);
[activityIndicator startAnimating];
[[self.view viewWithTag:3000]addSubview:activityIndicator];
// AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
NSURL *url = [NSURL URLWithString:#"http://qalina.acapglobal.com/kangatime/kangatime_api/api/user/all_profiles/format/json"];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
[request addPostValue:#"1" forKey:#"id"];
//[request addPostValue:pwdField.text forKey:#"pass"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// NSLog(#"responsestring==%#",responseString);
NSMutableArray *responseArr= [responseString JSONValue];
NSLog(#"responseArr==>%#",responseArr);
NSDictionary *tempDict = [responseArr objectAtIndex:0];
//NSLog(#"tempDict==>%#",tempDict);
if(indicatorView!=nil)
{
[indicatorView removeFromSuperview];
indicatorView=nil;
}
for(NSDictionary *dict in tempDict)
{
Home *objHome=[[Home alloc] init];
AppDelegate *appDel = [[UIApplication sharedApplication]delegate];
appDel.fName = [tempDict objectForKey:#"first_name"];
NSLog(#"appDel.fName==>%#",appDel.fName );
appDel.lName = [tempDict objectForKey:#"last_name"];
//NSLog(#"lName==>%#",lName);
NSMutableString *myStr=[NSMutableString stringWithString:appDel.fName];
[myStr appendFormat:#" "];
[myStr appendString:appDel.lName];
objHome.userName=myStr;
NSLog(#"objHome.userName==>%#",objHome.userName);
NSString *idProfile = [tempDict objectForKey:#"idprofile"];
NSLog(#"idProfile==>%#",idProfile);
// NSString *imageStr=[tempDict objectForKey:#"profile_picture_filepath"];
// NSMutableString *urlString= [NSMutableString stringWithString:#"http://qalina.acapglobal.com/kangatime/ktapp/profilepics/"];
// [urlString appendString:imageStr];
// NSURL *mainURL=[[[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]] autorelease];
// // NSLog(#"mainURL==>%#",mainURL);
//
// appDel.tempUserData = [[NSData alloc] initWithContentsOfURL:mainURL];
//NSLog(#"tempUserData==>%#",tempUserData);
// Home *objHome=[[Home alloc] init];
//appDel.tempUserImage = [[UIImage alloc] initWithData:appDel.tempUserData];
[profileArray addObject:objHome.userName];
// [profileArray addObject:appDel.tempUserImage];
NSLog(#"profileArray==>%d",[profileArray count]);
}
//[profileArray addObject:]
// [homeTable setDataSource:self];
// [homeTable setDelegate:self];
[homeTable reloadData];
//[self setProfileArray:[tempDict objectForKey:#"first_name"]];
}
first you have parse the json. to parse the jason you can use JSONKit
With the help of JSONKIT, first you have to parse response string.
You will get array or dictionary from JSON response. #SmartWork has provided you a link for JSONKIT and How to create JSON Array string given below? will help you out to retrieve array/dictionary from response.
Related
I have a UIImageView installed on my cell, it is transparent black, but when I scroll the CollectionView and I raise my UIImageView but there is always more to transprence.
See picture :
1 - I is not even scroll
2 - After a scroll
My code :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"pictureCell";
MSContestListCollectionViewCell *cell = (MSContestListCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.titleContest.adjustsFontSizeToFitWidth = YES;
cell.titleContest.minimumScaleFactor = 0.5;
cell.pictureImageView.layer.cornerRadius = 5;
cell.pictureImageView.clipsToBounds = YES;
cell.titleView.layer.cornerRadius = 5;
cell.titleView.clipsToBounds = YES;
switch (_segmentedControl.selectedSegmentIndex) {
case 0: {
NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.statusContest.text = [searchResult objectForKey:#"status"];
if ([[searchResult objectForKey:#"status"] isEqualToString:#"PAUSE"]) {
cell.titleContest.text = [NSString stringWithFormat:#"Concours en pause"];
/*
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
view.layer.cornerRadius = 5;
view.clipsToBounds = YES;
[cell.contentView addSubview:view];
*/
UIImageView *imagecellPause = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UIImage *cellImage = [UIImage imageNamed:#"cell-pause.png"];
imagecellPause.image = cellImage;
[cell.contentView addSubview:imagecellPause];
UIImageView *imagePause = [[UIImageView alloc] initWithFrame:CGRectMake(69.5, 69.5, 25, 25)];
UIImage *image = [UIImage imageNamed:#"Pause Filled-50 (1).png"];
imagePause.image = image;
[cell.contentView addSubview:imagePause];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 150, 50)];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Avenir-Book" size:12];
label.text = [searchResult objectForKey:#"description"];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
[cell.contentView addSubview:label];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 105, 150, 50)];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:#"Avenir-Black" size:15];
label2.text = #"Concours en pause";
[cell.contentView addSubview:label2];
cell.titleView.hidden = YES;
} else {
cell.titleContest.text = [searchResult objectForKey:#"description"];
}
break;
}
case 1: {
NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult2 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult2 objectForKey:#"description"];
cell.statusContest.text = [searchResult2 objectForKey:#"status"];
NSLog(#"le gagnant : %#", [searchResult2 valueForKeyPath:#"winners.name"]);
break;
}
case 2: {
NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult3 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult3 objectForKey:#"description"];
cell.statusContest.text = [searchResult3 objectForKey:#"status"];
break;
}
default:
break;
}
[_activity stopAnimating];
_label1.hidden = YES;
return cell;
}
Your CellForRowAtIndexPath should never contain addSubView, since everytime the cell is reloaded it will add the view. Thats why it keeps getting darker, it just adds view onto view.
Instead you should subclass your UICollectionViewCell and add the view in the subclass.
I have been beating my head over this issue for some time now and my last hope is Stack Overflow.
Here is the app idea. I have two entities modeled in core data. The Golfer entity has a one to many relationship with FittingSession, so each golfer is capable of having more than one fitting session. I am not able to post an image for the data models as I am a new user.
But here are the details:
ENTITY: GOLFER and FITTING SESSION
Attributes for Golfer - first_name, last_name, emailId, contactNum, picture
Relationship : NSSet * fittingSessions
Attributes for Fitting Sessions - session_number, date, location, notes.
Relationship: Golfer * whoPlayed
I am working on one view controller called ViewManager (kinda base view for all my classes) and it has 2-3 Custom UIViews inside it. I animate them in and out whenever I need them.
I am getting my Golfers list-collection in a tableview from NSFetchedResultsController and getting the Fitting Sessions attributes in a tableview by the same technique using NSFetchedResultsController. My question is: How do I get a specific fitting session for a specific golfer? What do I have to write in TableViewDidSelectRow Method of Parent( Golfer )View? How do I deal with this one to many relationship? Here's my code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView == mGolferTblView)
{
NSInteger count = [[self.fetchedResultsController sections] count];
NSLog(#"count section GOLFER TABLE VIEW=%d", count);
return count;
}
else if(tableView == mFittingTblView)
{
return 1;
}
else {
}
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == mGolferTblView)
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSLog(#"count for array ROWS for GOLFERS =%d", [sectionInfo numberOfObjects]);
return [sectionInfo numberOfObjects];
}
else if(tableView == mFittingTblView)
{
mFittingSessionArray = [mFittingSessionSet allObjects];
NSLog(#"array here is=%#", mFittingSessionArray);
NSLog(#"count for ROWS in FITTING SESSIONS table view=%d", [mFittingSessionArray count]);
return [mFittingSessionArray count];
// id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fittingFetchedResultsController sections] objectAtIndex:section];
// NSLog(#"count for array ROWS for FITTING SESSIONS =%d", [sectionInfo numberOfObjects]);
// return [sectionInfo numberOfObjects];
}
else {
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == mGolferTblView)
{
static NSString *CellIdentifier = #"Cell";
static NSInteger fullNameTag = 1;
static NSInteger imageTag = 2;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
UILabel *fakeLbl =[[UILabel alloc] initWithFrame:CGRectMake(100, 30, 100, 30)];
fakeLbl.backgroundColor = [UIColor clearColor];
fakeLbl.textColor = [UIColor grayColor];
fakeLbl.text=#"3 days ago";
[cell.contentView addSubview:fakeLbl];
[fakeLbl release];
UIImageView *btnImage =[[UIImageView alloc] initWithFrame:CGRectMake(250, 40, 25, 28)];
btnImage.image = [UIImage imageNamed:#"badge_25x28.png"];
btnImage.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:btnImage];
[btnImage release];
UILabel *fullNameLbl =[[UILabel alloc] initWithFrame:CGRectMake(100, 0, 300, 30)];
fullNameLbl.backgroundColor = [UIColor clearColor];
fullNameLbl.textColor = [UIColor whiteColor];
fullNameLbl.numberOfLines = 1;
fullNameLbl.adjustsFontSizeToFitWidth = YES;
fullNameLbl.tag = fullNameTag;
[cell.contentView addSubview:fullNameLbl];
[fullNameLbl release];
UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 71, 91)];
imageView.backgroundColor = [UIColor clearColor];
imageView.tag = imageTag;
[cell.contentView addSubview:imageView];
[imageView release];
}
mGolfer = [self.fetchedResultsController objectAtIndexPath:indexPath];
UILabel * fullNameLbl = (UILabel *) [cell.contentView viewWithTag:fullNameTag];
fullNameLbl.text = mGolfer.fullName;
UIImageView * imgView = (UIImageView *) [cell.contentView viewWithTag:imageTag];
imgView.image = mGolfer.picture;
return cell;
}
if(tableView == mFittingTblView)
{
static NSString *CellIdentifier = #"Cell";
static NSInteger locationTag = 1;
static NSInteger notesTag = 2;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
UIImageView *iconImage =[[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 11, 18)];
iconImage.image = [UIImage imageNamed:#"icon_locationmarker_11x18.png"];
iconImage.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:iconImage];
[iconImage release];
UILabel *fakeDateLbl =[[UILabel alloc] initWithFrame:CGRectMake(180, 15, 100, 20)];
fakeDateLbl.backgroundColor = [UIColor clearColor];
fakeDateLbl.textColor = [UIColor grayColor];
fakeDateLbl.text=#"apr.30.2012";
[cell.contentView addSubview:fakeDateLbl];
[fakeDateLbl release];
UIImageView *fakeImage1 =[[UIImageView alloc] initWithFrame:CGRectMake(25, 90, 43, 43)];
fakeImage1.image = [UIImage imageNamed:#"icon_catDriver_43x43.png"];
fakeImage1.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:fakeImage1];
[fakeImage1 release];
UIImageView *fakeImage2 =[[UIImageView alloc] initWithFrame:CGRectMake(70, 90, 43, 43)];
fakeImage2.image = [UIImage imageNamed:#"icon_catFairway_43x43.png"];
fakeImage2.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:fakeImage2];
[fakeImage2 release];
UIImageView *fakeImage3 =[[UIImageView alloc] initWithFrame:CGRectMake(115, 90, 43, 43)];
fakeImage3.image = [UIImage imageNamed:#"icon_catHybrid_43x43.png"];
fakeImage3.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:fakeImage3];
[fakeImage3 release];
UIImageView *fakeImage4 =[[UIImageView alloc] initWithFrame:CGRectMake(160, 90, 43, 43)];
fakeImage4.image = [UIImage imageNamed:#"icon_catIron_43x43.png"];
fakeImage4.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:fakeImage4];
[fakeImage4 release];
UIImageView *fakeImage5 =[[UIImageView alloc] initWithFrame:CGRectMake(205, 90, 43, 43)];
fakeImage5.image = [UIImage imageNamed:#"icon_catWedge_43x43.png"];
fakeImage5.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:fakeImage5];
[fakeImage5 release];
UILabel *sessionLbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 150, 30)];
sessionLbl.backgroundColor = [UIColor clearColor];
sessionLbl.textColor = [UIColor redColor];
sessionLbl.text = #"session";
sessionLbl.font = [UIFont boldSystemFontOfSize:18];
[cell.contentView addSubview:sessionLbl];
[sessionLbl release];
UILabel *locationLbl =[[UILabel alloc] initWithFrame:CGRectMake(30, 32, 270, 30)];
locationLbl.backgroundColor = [UIColor clearColor];
locationLbl.textColor = [UIColor whiteColor];
locationLbl.tag = locationTag;
[cell.contentView addSubview:locationLbl];
[locationLbl release];
UILabel *notesLbl =[[UILabel alloc] initWithFrame:CGRectMake(30, 54, 270, 30)];
notesLbl.backgroundColor = [UIColor clearColor];
notesLbl.textColor = [UIColor whiteColor];
notesLbl.tag = notesTag;
[cell.contentView addSubview:notesLbl];
[notesLbl release];
}
mFittingSessionArray = [mFittingSessionSet allObjects];
mFittingSession = [mFittingSessionArray objectAtIndex:indexPath.row];
UILabel *locationLbl = (UILabel *)[cell.contentView viewWithTag:locationTag];
locationLbl.text = mFittingSession.locationUppercase;
NSLog(#"location=%#", mFittingSession.locationUppercase);
UILabel *notesLbl = (UILabel *)[cell.contentView viewWithTag:notesTag];
notesLbl.text = mFittingSession.notesInQuotes;
return cell;
}
return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == mGolferTblView)
{
mGolfer = (Golfer *)[self.fetchedResultsController objectAtIndexPath:indexPath];
mGolferNameLbl.text = mGolfer.fullName;
mGolferHeaderPicture.image = mGolfer.picture;
NSSet * fittingSessionSet = mGolfer.fittingSessions;
mFittingSessionArray = [fittingSessionSet allObjects];
NSLog(#"count for sessions=%d", [mFittingSessionArray count]);
NSLog(#"fiting sessions for golfer %# are= %#", mGolfer.first_name, mFittingSessionArray);
}
}
Also, this is how I add a new fitting session to a particular golfer:
- (IBAction)addNewSession:(id)sender
{
AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [applicationDelegate managedObjectContext];
mFittingSession=(FittingSession*) [NSEntityDescription insertNewObjectForEntityForName:#"FittingSession" inManagedObjectContext:context];
mFittingSession.location=mLocationTextField.text;
mFittingSession.notes=mNotesTxtView.text;
**mFittingSession.whoPlayed = self.golfer;** This is setting the relationship (whoPlayed is inverse relation to golfer provided by core data)
}
Please help me in this as I am really not getting how to deal with the relationship in core data. Please provide some code or snippets so that I can know what's going on.
Thank you
Maybe I don't understand your question correctly, but to get a Golfer's Fitting Session is really easy. So in the first code block you just do the following:
mGolfer = (Golfer *)[self.fetchedResultsController objectAtIndexPath:indexPath];
NSSet *fittingSession = mGolfer.fittingSessions;
You can access the relations as you would access the other attributes.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView == mGolferTblView)
{
NSInteger count = [[self.fetchedResultsController sections] count];
NSLog(#"count section GOLFER TABLE VIEW=%d", count);
return count;
}
else if(tableView == mFittingTblView)
{
NSInteger countOfSections = 1;
NSLog(#"count section FITTING SESSION VIEW=%d", countOfSections);
return countOfSections;
}
else {
}
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == mGolferTblView)
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSLog(#"count for ROWS in GOLFERS table view =%d", [sectionInfo numberOfObjects]);
return [sectionInfo numberOfObjects];
}
else if(tableView == mFittingTblView)
{
mFittingSessionArray = [mFittingSessionSet allObjects];
NSLog(#"array here is=%#", mFittingSessionArray);
NSLog(#"count for ROWS in FITTING SESSIONS table view=%d", [mFittingSessionArray count]);
return [mFittingSessionArray count];
}
else {
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == mGolferTblView)
{
static NSString *CellIdentifier = #"Cell";
static NSInteger fullNameTag = 1;
static NSInteger imageTag = 2;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
UILabel *fullNameLbl =[[UILabel alloc] initWithFrame:CGRectMake(100, 0, 300, 30)];
fullNameLbl.backgroundColor = [UIColor clearColor];
fullNameLbl.textColor = [UIColor whiteColor];
fullNameLbl.numberOfLines = 1;
fullNameLbl.adjustsFontSizeToFitWidth = YES;
fullNameLbl.tag = fullNameTag;
[cell.contentView addSubview:fullNameLbl];
[fullNameLbl release];
UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 71, 91)];
imageView.backgroundColor = [UIColor clearColor];
imageView.tag = imageTag;
[cell.contentView addSubview:imageView];
[imageView release];
}
mGolfer = [self.fetchedResultsController objectAtIndexPath:indexPath];
UILabel * fullNameLbl = (UILabel *) [cell.contentView viewWithTag:fullNameTag];
fullNameLbl.text = mGolfer.fullName;
UIImageView * imgView = (UIImageView *) [cell.contentView viewWithTag:imageTag];
imgView.image = mGolfer.picture;
return cell;
}
else if(tableView == mFittingTblView)
{
**mFittingSessionArray = [mFittingSessionSet allObjects];** // Added this line
static NSString *CellIdentifier = #"Cell";
static NSInteger locationTag = 1;
static NSInteger notesTag = 2;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
UILabel *locationLbl =[[UILabel alloc] initWithFrame:CGRectMake(30, 32, 270, 30)];
locationLbl.backgroundColor = [UIColor clearColor];
locationLbl.textColor = [UIColor whiteColor];
locationLbl.tag = locationTag;
[cell.contentView addSubview:locationLbl];
[locationLbl release];
UILabel *notesLbl =[[UILabel alloc] initWithFrame:CGRectMake(30, 54, 270, 30)];
notesLbl.backgroundColor = [UIColor clearColor];
notesLbl.textColor = [UIColor whiteColor];
notesLbl.tag = notesTag;
[cell.contentView addSubview:notesLbl];
[notesLbl release];
}
mFittingSession = [mFittingSessionArray objectAtIndex:indexPath.row];
UILabel *locationLbl = (UILabel *)[cell.contentView viewWithTag:locationTag];
UILabel *notesLbl = (UILabel *)[cell.contentView viewWithTag:notesTag];
locationLbl.text = mFittingSession.locationUppercase;
notesLbl.text = mFittingSession.notesInQuotes;
return cell;
}
else {
}
return 0;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == mGolferTblView)
{
if(self.editing)
{
[self.golferTblView deselectRowAtIndexPath:indexPath animated:YES];
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut
animations:^ {
mEditGolfersView.hidden = NO;
mEditGolfersView.frame = CGRectMake(305, mEditGolfersView.frame.origin.y, mEditGolfersView.frame.size.width, mEditGolfersView.frame.size.height);
}
completion:NULL];
mGolfer = (Golfer *) [self.fetchedResultsController objectAtIndexPath:indexPath];
mEditFirstName.text = mGolfer.first_name;
mEditMiddleName.text = mGolfer.middle_name;
mEditLastName.text = mGolfer.last_name;
mEditEmailField.text = mGolfer.email_id;
mEditContactNum.text = mGolfer.contactNumber;
mEditPictureView.image = mGolfer.picture;
mShowDataBtn.enabled = NO;
return;
}
[self.golferTblView deselectRowAtIndexPath:indexPath animated:YES];
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut
animations:^ {
mGolfersView.frame = CGRectMake(-260, mGolfersView.frame.origin.y, mGolfersView.frame.size.width, mGolfersView.frame.size.height);
}
completion:^(BOOL finished){
mGolfersView.hidden = YES;
}];
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn
animations:^ {
mFittingSessionView.hidden = NO;
mFittingSessionView.frame = CGRectMake(-19, mFittingSessionView.frame.origin.y, mFittingSessionView.frame.size.width, mFittingSessionView.frame.size.height);
}
completion:NULL];
mGolfer = (Golfer *)[self.fetchedResultsController objectAtIndexPath:indexPath];
mGolferNameLbl.text = mGolfer.fullName;
mGolferHeaderPicture.image = mGolfer.picture;
mFittingSessionSet = mGolfer.fittingSessions;
mFittingSessionArray = [mFittingSessionSet allObjects];
NSLog(#"count for sessions=%d", [mFittingSessionArray count]);
NSLog(#"fiting sessions for golfer %# are= %#", mGolfer.first_name, mFittingSessionArray);
**[mFittingTblView reloadData];** // Added this and this reloads the data for fitting view.
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [self.LoadFile count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 4;
}
int title =0;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
for (title = 0 ; title <= section; title++)
{
NSLog(#"section - %d ",title);
NSDictionary *dict = [LoadFile objectAtIndex:title];
NSString *titlename = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"name"]];
NSLog(#"%#",titlename);
if (section == title)
return titlename;
}
}
Here i am using two arrays which is created on the page itself,in this case it will work fine but for the case when i need it to populate from DB then what should i do.
- (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];
while (i < sec)
{
dict = [LoadFile objectAtIndex:0];
NSString *addr = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"address"]];
NSString *con = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"contact"]];
NSString *sal = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"salary"]];
NSString *tec = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"tech"]];
NSArray *arr = [[NSArray alloc] initWithObjects:addr,con,sal,tec,nil];
// NSArray *arr = [[NSArray alloc] initWithObjects:i,i,i,i,nil];
//NSArray *arr2 = [[NSArray alloc] initWithObjects:#"12",#"22",#"32",#"42",nil];
NSLog(#"%#",arr);
if(indexPath.section == i)
{
cell.textLabel.text = [arr objectAtIndex:indexPath.row];
i++;
}
}
return cell;
}
for (i = 0 ; i <= sec-1; i++)
{
NSLog(#"section - %d ",title);
NSDictionary *dict = [LoadFile objectAtIndex:title];
if (indexPath.section == 0)
{
UILabel *Address = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
Address.textColor = [UIColor blueColor];
Address.backgroundColor = [UIColor clearColor];
[Address setFont:[UIFont boldSystemFontOfSize:14.0]];
[Address setTag:i];
Address.text = [dict objectForKey:#"address"];
[cell.contentView addSubview:Address];
UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
tech.textColor = [UIColor blueColor];
tech.backgroundColor = [UIColor clearColor];
[tech setFont:[UIFont boldSystemFontOfSize:14.0]];
[tech setTag:i];
tech.text = [dict objectForKey:#"tech"];
[cell.contentView addSubview:tech];
}
else if (indexPath.section == 1)
{
UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
tech.textColor = [UIColor blueColor];
tech.backgroundColor = [UIColor clearColor];
[tech setFont:[UIFont boldSystemFontOfSize:14.0]];
[tech setTag:i];
tech.text = [dict objectForKey:#"tech"];
[cell.contentView addSubview:tech];
}
}
else if (indexPath.section == 0)
{
UILabel *contact = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
contact.textColor = [UIColor blueColor];
contact.backgroundColor = [UIColor clearColor];
[contact setFont:[UIFont boldSystemFontOfSize:14.0]];
contact.text = [dict objectForKey:#"contact"];
// NSLog(#"Val-%#",[dict objectForKey:#"name"]);
[contact setTag:-4];
[cell.contentView addSubview:contact];
}
else if (indexPath.section == 0)
{
UILabel *salary = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
salary.textColor = [UIColor blueColor];
salary.backgroundColor = [UIColor clearColor];
[salary setFont:[UIFont boldSystemFontOfSize:14.0]];
salary.text = [dict objectForKey:#"salary"];
// NSLog(#"Val-%#",[dict objectForKey:#"name"]);
[salary setTag:-4];
[cell.contentView addSubview:salary];
}
else
{
UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
tech.textColor = [UIColor blueColor];
tech.backgroundColor = [UIColor clearColor];
[tech setFont:[UIFont boldSystemFontOfSize:14.0]];
tech.text = [dict objectForKey:#"tech"];
// NSLog(#"Val-%#",[dict objectForKey:#"name"]);
[tech setTag:-4];
[cell.contentView addSubview:tech];
}
I want to populate data from table in database ,dynamically
Once your array has been updated with new data, you just call [self.tableview reloadData];
ok, so this is pretty simple one, but i hope i could explain this clearly - i have a table view that i would like to inset into a container, and then have the table bounces when it reaches the top / bottom. So far, I was able to put my table in a container, but the container is fixed on the view, while the table inside the container bounces. Again, I am looking for a way to fix the table to the container, while having the container bouncing.
Here is what I was able to do, following the code:
What I want to accomplish is to have the black box bouncing rather than the table within it.
my ViewDidLoad in the view controller .m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//General View Setup
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"backgroundimage.png"]];
self.view.backgroundColor = background;
//Table View Data
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", #"Sony", #"Ivory", #"IBM", nil];
NSDictionary *otherComputersDict = [NSDictionary dictionaryWithObject:otherComputers forKey:#"Computers"];
[listOfItems addObject:appleComputersDict];
[listOfItems addObject:otherComputersDict];
self.navigationItem.title = #"Computers";
// Create a table
tblSimpleTable.delegate = self;
CGRect cgRct = CGRectMake(10, 50, 300, 300);
tblSimpleTable = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStyleGrouped]; // Initilize the table
[tblSimpleTable setBackgroundColor:[UIColor blackColor]];
tblSimpleTable.sectionHeaderHeight = 30.0;
tblSimpleTable.sectionFooterHeight = 30.0;
tblSimpleTable.delegate = self;
tblSimpleTable.dataSource = self;
[self.view addSubview:tblSimpleTable];
//Create the header
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor yellowColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
self.tblSimpleTable.tableHeaderView = containerView;
}
why don’t you use UIScrollView for that.
I had tested your code & done required changes. Hope you like it.
Code :
(this is your .h file)
#import <UIKit/UIKit.h>
#interface tableScrollViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource, UIScrollViewDelegate> {
UITableView *tblSimpleTable;
NSMutableArray *listOfItems;
NSMutableArray *appleComputers,*otherComputers;
UIScrollView *scrollView;
}
#end
(this is your .m file)
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor grayColor];
scrollView.contentSize = CGSizeMake(300, 800);
appleComputers = [[NSMutableArray alloc] init]; // I made it by my style
[appleComputers addObject: #"iPhone"];
[appleComputers addObject:#"iPod"];
[appleComputers addObject:#"MacBook"];
[appleComputers addObject:#"MacBook Pro"];
otherComputers = [[NSMutableArray alloc] init];
[otherComputers addObject: #"HP"];
[otherComputers addObject:#"Dell"];
[otherComputers addObject:#"Windows"];
[otherComputers addObject:#"Sony"];
[otherComputers addObject:#"Ivory"];
[otherComputers addObject:#"IBM"];
self.navigationItem.title = #"Computers";
// Create a table
tblSimpleTable.delegate = self;
CGRect cgRct = CGRectMake(10, 50, 300, 600);
tblSimpleTable = [[UITableView alloc] initWithFrame:cgRct
style:UITableViewStyleGrouped]; // Initilize the table
[tblSimpleTable setBackgroundColor:[UIColor blackColor]];
tblSimpleTable.sectionHeaderHeight = 30.0;
tblSimpleTable.sectionFooterHeight = 30.0;
tblSimpleTable.scrollEnabled = NO;
tblSimpleTable.delegate = self;
tblSimpleTable.dataSource = self;
[scrollView addSubview:tblSimpleTable];
self.view = scrollView;
//Create the header
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor yellowColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
tblSimpleTable.tableHeaderView = containerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return [appleComputers count];
else if(section == 1)
return [otherComputers count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 0)
cell.text = [NSString stringWithFormat:#"%#“,
[appleComputers objectAtIndex:indexPath.row]];
else if(indexPath.section == 1)
cell.text = [NSString stringWithFormat:#"%#“,
[otherComputers objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
// do whatever here
}
Guys, i have a little issue here. Im doing a search to my UITableView. I search "iPhone", it returns to me one result. Its ok. iPhone just has one result. But when i search for iPad, that has two results, it gives me 2 results, but the first row stills displaying iphone info. The second display the iPad correct information.
When i click on search bar again to search another word, it gives me the two desired rows of ipad. But when i press the cancel button, it returns to the iPhone and iPad rows.
What im doing wrong?
Thanks
Cell and Table Methods:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.listCod count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"SearchResult";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell =
[[[UITableViewCell alloc]
initWithFrame:CGRectMake(0, 0, 180, 200)
reuseIdentifier:MyIdentifier]
autorelease];
UIColor *bkgColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"SearchViewTableCellBackground.png"]];
UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ];
backgroundView.backgroundColor = bkgColor;
cell.backgroundView = backgroundView;
// Foto do produto
UIView *teste = [[UIView alloc] init];
teste.frame = CGRectMake(5, 5, 100, 100);
teste.backgroundColor = [UIColor blueColor];
[cell addSubview:teste];
// Label de Marca
UILabel *marca = [[UILabel alloc] init];
marca.frame = CGRectMake(115, 10, 195, 25);
marca.backgroundColor = [UIColor clearColor];
marca.textColor = [UIColor grayColor];
NSString *marcaString = [self.listMarca objectAtIndex:indexPath.row];
marca.font = [UIFont systemFontOfSize:13.0];
marca.text = marcaString;
[cell addSubview:marca];
[marca release];
// Label do nome do produto
UILabel *nome = [[UILabel alloc] init];
nome.frame = CGRectMake(115, 30, 195, 25);
nome.backgroundColor = [UIColor clearColor];
nome.textColor = [[UIColor alloc] initWithRed:26.0 / 255 green:177.0 / 255 blue:240.0 / 255 alpha:1.0];
nome.font = [UIFont boldSystemFontOfSize:25.0];
NSString *noemString = [self.listName objectAtIndex:indexPath.row];
nome.text = noemString;
[cell addSubview:nome];
[nome release];
//Preco
UILabel *preco = [[UILabel alloc] init];
preco.frame = CGRectMake(115, 55, 195, 25);
preco.backgroundColor = [UIColor clearColor];
preco.textColor = [UIColor grayColor];
// Manda preco e parcela pra tratar
[self priceFormat:[self.listPreco objectAtIndex:indexPath.row] :[self.listParcela objectAtIndex:indexPath.row]];
preco.font = [UIFont boldSystemFontOfSize:20.0];
preco.text = self.precoFinal;
[cell addSubview:preco];
//Parcela
UILabel *parcelaLabel = [[UILabel alloc] init];
parcelaLabel.frame = CGRectMake(115, 73, 195, 25);
parcelaLabel.backgroundColor = [UIColor clearColor];
parcelaLabel.textColor = [UIColor grayColor];
parcelaLabel.font = [UIFont systemFontOfSize:13.0];
parcelaLabel.text = self.parcelamentoFinal;
[cell addSubview:parcelaLabel];
}
return cell;
}
And the search bar methods:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.theTableView.allowsSelection = NO;
self.theTableView.scrollEnabled = NO;
[theTableView setRowHeight:110];
}
- (void)searchDisplayController:(UISearchDisplayController *)controller
willShowSearchResultsTableView:(UITableView *)tableView
{
[tableView setRowHeight:[[self theTableView] rowHeight]];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableData removeAllObjects];
[self.theTableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=#"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
return NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// Converte a String do campo de busca
NSString* buscaGet =
[searchBar.text stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
// Conecta com a URL
NSString *endereco = [[NSString alloc] initWithFormat:#"http://localhost/icomm/test.php?nome=%#", buscaGet];
// Lê o resultado
TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:endereco]] retain];
TBXMLElement * rootXMLElement = tbxml.rootXMLElement;
TBXMLElement * item = [TBXML childElementNamed:#"produto" parentElement:rootXMLElement];
listCod = [[NSMutableArray alloc] init];
listMarca = [[NSMutableArray alloc] init];
listName = [[NSMutableArray alloc] init];
listPreco = [[NSMutableArray alloc] init];
listParcela = [[NSMutableArray alloc] init];
while (item) {
// Seta a id do produto
TBXMLElement * codigo = [TBXML childElementNamed:#"item" parentElement:item];
NSString * codProd = [TBXML textForElement:codigo];
[self.listCod addObject:codProd];
// Seta a marca do produto
TBXMLElement * marca = [TBXML childElementNamed:#"marca" parentElement:item];
NSString * marcaProd = [TBXML textForElement:marca];
[self.listMarca addObject:marcaProd];
// Seta o nome do produto
TBXMLElement * nome = [TBXML childElementNamed:#"nome" parentElement:item];
NSString * nomeProd = [TBXML textForElement:nome];
[self.listName addObject:nomeProd];
// Seta o preco do produto
TBXMLElement * preco = [TBXML childElementNamed:#"preco" parentElement:item];
NSString * precoProd = [TBXML textForElement:preco];
[self.listPreco addObject:precoProd];
// Seta o parcela do produto
TBXMLElement * parc = [TBXML childElementNamed:#"parcela" parentElement:item];
NSString * parcProd = [TBXML textForElement:parc];
[self.listParcela addObject:parcProd];
// Procura o proximo produto.
item = [TBXML nextSiblingNamed:#"produto" searchFromElement:item
];
}
NSLog(#"%#", self.listName);
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
//Remove tudo da table e recarrega
[[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
[tableData removeAllObjects];
[theTableView reloadData];
}
Thanks for all.
You are only setting your labels up inside the if (cell == nil) part of your code. This only gets called the first time a cell is created; after that the cell is dequeued for reuse and you don't create a new cell. If you want to dequeue your cells like that, you need to set the text (and any other property of the label that might change) outside of that if statement. In order to get references to your labels, you will need to use tags and viewWithTag:.
Or if you want to simplify things, don't dequeue your tableCells; though this could lead to performance issues if you have a lot of cells.