UITableViewCell staying empty - ios

I am trying to fill a UITableView with the objects of an NSMutableArray which is filled from a table in Parse. The array is definitely being filled (I checked its contents with an NSLog), but the table is staying empty. I have tried A LOT of different ways including the following:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
PFObject *postsObject = [postsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [postsObject objectForKey:#"message"];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
if (!cell) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == table) {
cell.textLabel.text = [postsArray objectAtIndex:indexPath.row];
}
return cell;
}
and the much simpler
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
cell.messageLabel.text = [NSString stringWithFormat:[postsArray objectAtIndex:[indexPath row]]];
return cell;
}
Does anyone have any ideas?
Thanks in advance :)
EDIT:
My dataSource methods are:
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return postsArray.count;
}
EDIT #2:
My code I'm using to fill my array and reload my tableView
PFQuery *findData = [PFQuery queryWithClassName:#"AllPosts"];
[findData setLimit:1000];
[findData findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error == nil) {
for (PFObject *object in objects) {
PFObject *post = object[#"content"];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:post, nil];
postsArray = array;
[table reloadData];
}
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Unable To Load" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];

You are doing it wrong , you are replacing the array with new data whenever you are finding a PFObject in objects, your code should be like this:-
//first alloc your array
postsArray=[[NSMutableArray alloc] init];
//Logic goes here
PFQuery *findData = [PFQuery queryWithClassName:#"AllPosts"];
[findData setLimit:1000];
[findData findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error == nil) {
for (PFObject *object in objects) {
PFObject *post = object[#"content"];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:post, nil];
[postsArray addObject:array];
[table reloadData];
}
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Unable To Load" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];

You also need to check if the cell object is actually returned. Also put in the following code after the call to dequeueReusableCellWithIdentifier:
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
This would solve your problem

Related

Uneven data loading to UITableViewController from Parse when not using PFQueryTableView

I'm using UITableViewController for displaying data from Parse. It runs perfectly on my Xcode Simulator as i think there's no latency in network. But whenever i'm uploading the code to AppStore for Testing. The very first time i run the app it has to load a couple of restaurant's from Parse and display in UITableViewController. Upon clicking a row the first rows data is being loaded into the 3rd row and 4th row data loading in 6th row data irregularly. Why is the data being loaded very unevenly ? Here's my
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = #"restaurantIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
PFObject *tempObject = [self.objectArray objectAtIndex:indexPath.row];
PFFile *imageFile = [tempObject objectForKey:#"RestaurantIcon"];
PFImageView *imageView = [[PFImageView alloc] init];
imageView.file = imageFile;
[imageView loadInBackground:^(UIImage *img,NSError *error){
if(!error){
cell.imageCell.image = imageView.image;
}
}];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 4;
cell.imageView.frame = self.view.bounds;
cell.cellLabel.text = [tempObject objectForKey:#"RestaurantName"];
[self.hotelNamesArray addObject:[tempObject objectForKey:#"RestaurantName"]];
cell.cellLabel.lineBreakMode = NSLineBreakByWordWrapping;
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_restaurantName = [self.hotelNamesArray objectAtIndex:indexPath.row];
self.restaurantMenuNameArray = [[NSMutableArray alloc] init];
PFQuery *query = [PFQuery queryWithClassName:[self.hotelNamesArray objectAtIndex:indexPath.row]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *obj in objects) {
if (![_restaurantMenuNameArray containsObject:[obj objectForKey:#"RestaurantMenuName"]]) {
NSLog(#"restaurantmenunames are %#",[obj objectForKey:#"RestaurantMenuName"]);
if ([obj objectForKey:#"RestaurantMenuName"] ==nil) {
[self performSegueWithIdentifier:#"restaurantDetail" sender:self];
return;
}else {
[_restaurantMenuNameArray addObject: [obj objectForKey:#"RestaurantMenuName"]];
}
}
}
}else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
[self.tableView reloadData];
NSLog(#"restaurantMenuNames is %#",_restaurantMenuNameArray);
[self performSegueWithIdentifier:#"restaurantDetail" sender:self];
}];
}
Thanks in advance.
If you mean the images get in the wrong cell, you have to consider that cells are recycled when you scroll, and that if the image loading takes a bit too long, you may get the result after the cell has been reused.
You need to check that the cell is still for the item/row you want (you could store the row in the cell's tag and check it before setting the image in the completion handler, for instance).
If it's other data that is mixed up, then you'll need to show us the code that loads that data.

Display alert if no recipients are selected iOS

I have a page where we select recipients (who are friends) to send an image to. but if no recipients are selected we can still send the message. i want it so that if no recipients are selected we can show a UIAlertView. for me its not working when i try to display an alert please see my code below.
.h
#property (nonatomic, strong) NSArray *friends;
#property (nonatomic, strong) PFRelation *friendsRelation;
#property (nonatomic, strong) NSMutableArray *recipients;
- (IBAction)send:(id)sender;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.recipients = [[NSMutableArray alloc] init];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.friendsRelation = [[PFUser currentUser] objectForKey:#"friendsRelation"];
PFQuery *query = [self.friendsRelation query];
[query orderByAscending:#"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"Error %# %#", error, [error userInfo]);
}
else {
self.friends = objects;
[self.tableView reloadData];
}
}];
//display camera modally etc......
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *user = [self.friends objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;
if ([self.recipients containsObject:user.objectId]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
PFUser *user = [self.friends objectAtIndex:indexPath.row];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId];
}
NSLog(#"%#", self.recipients);
}
here is the part where i try to display my alert
- (IBAction)send:(id)sender {
if (!self.recipients) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"Select some friends" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
} else {
[self uploadMessage];
//we upload the message in method to parse.com
}
}
it does not seem to show for some reason so we can send messages to no one. how can i show the alert view?
Try this. Check objects of recipients is equal to 0 or not.
- (IBAction)send:(id)sender {
if ([self.recipients count]==0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"Select some friends" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
} else {
[self uploadMessage];
//we upload the message in method to parse.com
[self.tabBarController setSelectedIndex:1];
}
}
The problem with your code is the condition that you are checking::
if (!self.recipients)
here self.recipients will always give true value as it will look for memory address of this object, which you have assigned to it in your viewDidLoad method.
You have to check for the count of the array in your scenario.

Deleted row in tableview is reappearing when navigating back

Have friendsviewcontroller in which have uibarbuttonItem to edit friends list and other uibarbuttonitem to create groups for group chatrooms.
Have multiple segue for switching view controllers.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Groups";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New" style:UIBarButtonItemStylePlain target:self
action:#selector(actionNew)];
self.tableView.separatorInset = UIEdgeInsetsZero;
chatrooms = [[NSMutableArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([PFUser currentUser] != nil)
{
[self refreshTable];
}
else LoginUser(self);
}
#pragma mark - User actions
- (void)actionNew
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Create New Group" message:nil delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex=
{
if (buttonIndex != alertView.cancelButtonIndex)
{
UITextField *textField = [alertView textFieldAtIndex:0];
if ([textField.text isEqualToString:#""] == NO)
{
PFObject *object = [PFObject objectWithClassName:PF_CHATROOMS_CLASS_NAME];
object[PF_CHATROOMS_NAME] = textField.text;
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (error == nil)
{
[self refreshTable];
}
else [ProgressHUD showError:#"Network error."];
}];
}
}
}
- (void)refreshTable
{
[ProgressHUD show:nil];
PFQuery *query = [PFQuery queryWithClassName:PF_CHATROOMS_CLASS_NAME];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (error == nil)
{
[chatrooms removeAllObjects];
for (PFObject *object in objects)
{
[chatrooms addObject:object];
}
[ProgressHUD dismiss];
[self.tableView reloadData];
}
else [ProgressHUD showError:#"Network error."];
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [chatrooms count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
PFObject *chatroom = chatrooms[indexPath.row];
cell.textLabel.text = chatroom[PF_CHATROOMS_NAME];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
[chatrooms removeObjectAtIndex:chatroom];
//[chatrooms removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
PFObject *chatroom = chatrooms[indexPath.row];
NSString *roomId = chatroom.objectId;
CreateMessageItem([PFUser currentUser], roomId, chatroom[PF_CHATROOMS_NAME]);
ChatView *chatView = [[ChatView alloc] initWith:roomId];
chatView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:chatView animated:YES];
}
Deleted row in table view reappears when navigate back to the TableView
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
[chatrooms removeObjectAtIndex:chatroom];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
Im unable to locate what piece of code i m missing or what exactly i m doing wrong.
If anyone can please point out to that.
Will appreciate so much.
Thanks.
When committing deletion, your code removes objects from chatrooms which is data source of the table view in your case, but this happens in your app's memory, the source from which chatrooms is populated does not change. Thus, speaking in MVC, the app's model state is not updated after the view's state is changed.
Every time the table view is showed up, your code populates chatrooms in refreshTable, if the model's state hasn't been changed, the code gets same list as before, thus the table view doesn't change.
EDIT: Instead of using another approach to refresh the table view, you need to think about what does your app do in this table view. If user can delete stuff in the table view, should your app update model (This model can be a local or remote database, a property list file, etc.) too? If yes, then update model when user inserts or deletes rows in the table view; well, if not, then you are asking a question that is not a problem, or maybe the table view should turn off editing.
EDIT1:
If you do need to update data, based on your code, you may need to do something like this:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
[chatrooms removeObjectAtIndex:indexPath.row];
PFQuery *query = [PFQuery queryWithClassName:PF_CHATROOMS_CLASS_NAME];
[query deleteChatroom:chatroom];
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
That is, you may need to implement method deleteChatroom: of class PFQuery.

UiTableview values not getting update upon the array value changed

I am fetching data from a web service which returns an array and I am displaying that array in the table view. I am facing the issue that table values are getting updated but after some time as first it is showing the old value and upon scrolling it shows the new value.
Below is my code.
NSURLRequest *request=[NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:8.0];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSMutableDictionary *ResultdicNew = [[NSMutableDictionary alloc]initWithDictionary:(NSMutableDictionary *)responseObject];
NSString *message=[ResultdicNew objectForKey:#"status"];
NSString *message1=#"";
if([message isEqualToString:#"success"])
{
NSMutableDictionary *datadic=[ResultdicNew objectForKey:#"data"];
NSMutableDictionary *listDataDic=[datadic objectForKey:#"competitors"];
affArray=[listDataDic valueForKey:#"affiliation"];
countArray=[listDataDic valueForKey:#"total_count"];
_lblEventTitle.text=[datadic valueForKey:#"group_name"];
NSLog(#"affArray here is %#",affArray);
NSLog(#"countArray.count here is %ld",countArray.count);
[self revealCompetitorsView];
}
else
{
message1=[Resultdic objectForKey:#"data"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message1
message:message
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}
[SVProgressHUD dismiss];
// NSLog(#"JSON Here is :%lu",(unsigned long)Resultdic.count);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[SVProgressHUD dismiss];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Data"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
#pragma mark - UITableView Datasource, Delegate Methods
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [countArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"menuCellNew";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RAMenuCellNew" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
UILabel *lbl=(UILabel*)[cell viewWithTag:420];
UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
lbl.text=[affArray objectAtIndex:indexPath.row];
lbl1.text=[countArray objectAtIndex:indexPath.row];
}
[self.sideTable reloadData];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%ld",(long)indexPath.row);
}
Your problem is in your cellForRowAtIndexPath - you are not updating the cell when you are given an existing cell for reuse.
You need to move the updating of the cell fields outside of the if (cell==nil)... block -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"menuCellNew";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RAMenuCellNew" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
UILabel *lbl=(UILabel*)[cell viewWithTag:420];
UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
lbl.text=[affArray objectAtIndex:indexPath.row];
lbl1.text=[countArray objectAtIndex:indexPath.row];
return cell;
}
Also, you can register your nib file for your cell in viewDidLoad and then you can use dequeueReusableCellWithIdentifer:forIndexPath which will automatically allocate a new cell if required and you can skip the whole if (cell==nil... bit.
You should also consider creating a UITableViewCell subclass and then you can assign your text fields to IBOutlet properties instead of searching by tag.
Replace this code on cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"menuCellNew";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RAMenuCellNew" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
UILabel *lbl=(UILabel*)[cell viewWithTag:420];
UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
lbl.text=[affArray objectAtIndex:indexPath.row];
lbl1.text=[countArray objectAtIndex:indexPath.row];
return cell;
}

Retrieving objects from parse into table view cell

I have created an object and saved it to the backend named NewCar and there are 4 strings attached to which are year, make, model, horsepower I'm having troubles pulling the data from parse and placing it into my table here is a few pieces of my code.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
PFQuery *query = [PFQuery queryWithClassName:#"NewCar"];
[query whereKey:#"year" equalTo:[[PFUser currentUser] objectId]];
[query orderByAscending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
} else {
self.cars = objects;
[self.tableView reloadData];
}
}];
}
#pragma mark - Table view data source
- (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 [self.cars count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"garageCell";
garageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[garageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.usernameDisplay.text = [[PFUser currentUser] objectForKey:#"username"];
//I need to display the year, make and model below here.
cell.carYearLabel.text = [[PFObject objectWithClassName:#"NewCar"] objectForKey:#"year"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 236;
}
I am able to display the username in my custom cell, but can't figure out how to display the year, make and model values. Any help would be much appreciated.
I have been looking through the documentation in Parse and can't seem to figure it out just in case you are wondering if I tried there.
I also want to add in the code where I added the object to Parse this might help in figuring this out as well.
- (IBAction)save:(id)sender {
NSString *year = self.carYear.text;
NSString *make = self.carMake.text;
NSString *model = self.carModel.text;
NSString *horsepower = self.carHorsepower.text;
if ([year length] == 0 || [make length] == 0 || [model length] == 0 || [horsepower length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:#"You might have missed a field" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
} else {
PFObject *newCar = [PFObject objectWithClassName:#"NewCar"];
newCar[#"year"] = year;
newCar[#"make"] = make;
newCar[#"model"] = model;
newCar[#"horsepower"] = horsepower;
[newCar saveInBackground];
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
Okay so instead of using
cell.carYearLabel.text = [[PFObject objectWithClassName:#"NewCar"] objectForKey:#"year"];
You can use the following
cell.carYearLabel.text = [self.cars objectForKey:#"NewCar"] valueForKey:#"year"];

Resources