显示计数字符串(数组?)从分析到UIView UIViewController
问题描述:
我有一个应用程序内置Xcode使用Objective-C保存数据到Parse.com。用户将记忆发送到parse.com数据库。显示计数字符串(数组?)从分析到UIView UIViewController
我想显示当前用户发送的记忆数量(计数)。
我想要显示的故事板是UIViewController,并且页面底部有足够的空间容纳单行文本。所以,我从Xcode对象库中插入了一个Table View,然后插入一个带有1个原型Cell并具有“cellCount”重用标识符的Table View Cell。
我已经设置了ViewController作为UITableView的委托和数据源,并且我已经将单元格链接到ViewController头文件。
我现在完全卡住了,我只需要一些帮助。我今天工作了大约12个小时,所以请让我怜悯。看看我的代码中是否有一些明显的错误。谢谢!!!
进口 “MemoriesViewController.h”
MemoriesViewController.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AppDelegate.h"
@interface MemoriesViewController : UIViewController <UITableViewDelegate, UITextViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) PFQuery *query;
@property (nonatomic, strong) NSArray *mainArray;
@property (strong, nonatomic) IBOutlet UITableViewCell *cellCount;
@property (strong, nonatomic) IBOutlet UITextView *memoriesField;
- (IBAction)sendMemories:(UIButton *)sender;
- (IBAction)logOut:(id)sender;
@end
MemoriesViewController.m
#import "MemoriesViewController.h"
@interface MemoriesViewController()
@end
@implementation MemoriesViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController.navigationBar setHidden:NO];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
NSLog(@"Current user: %@", currentUser.username);
}
else {
[self performSegueWithIdentifier:@"showLogin" sender:self];
}
self.memoriesField.delegate = self;
_mainArray = [[NSArray alloc] initWithObjects:@"one", nil];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfSectionsInTableView:(NSInteger)section {
return [_mainArray count];
}
- (UITableViewCell *)tableView:(UITableViewCell *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
PFQuery *query = [PFQuery queryWithClassName:@"Memories"];
[query whereKey:@"senderName" equalTo:[[PFUser currentUser] username]];
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
if (!error)
NSLog (@"You have %d Memories Saved", count);
return cell;
}
#pragma mark - IB Actions
- (IBAction)sendMemories:(UIButton *)sender {
PFObject *memories = [PFObject objectWithClassName:@"Memories"];
memories[@"Memories"] = self.memoriesField.text;
[memories setObject:[[PFUser currentUser] username] forKey:@"senderName"];
[memories saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded){
NSLog(@"succeeded");
self.memoriesField.text = nil;
}
else{
NSLog(@"problem");
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Our Apologies" message:@"Your Memory was not Recieved" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction: defaultAction];
}
NSString *memories = [self.memoriesField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.view endEditing:YES];
if ([memories length] == 0)
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Memory Recieved!" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction: defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
else {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
}
- (IBAction)logOut:(id)sender {
[PFUser logOut];
[self performSegueWithIdentifier:@"showLogin" sender:self];
}
#pragma mark - UITextField Delegate Methods
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
@end
答
OK,我不知道Parse.com,但似乎您可以使用UILabel
而不是您正在使用的UITableView
,将其存储在label
的属性中。然后运行,你必须设置标签文本代码:
PFQuery *query = [PFQuery queryWithClassName:@"Memories"];
[query whereKey:@"senderName" equalTo:[[PFUser currentUser] username]];
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
if (!error) {
NSLog (@"You have %d Memories Saved", count);
self.label.text = [NSString stringWithFormat:@"You have %d Memories Saved", count];
}
+0
它工作!它的工作原理!你是个天才!!! –
错误 - “UITableViewCell的”声明选择无可见@interface“dequeueReusableCellWithIdentifier:” –
我有一种感觉,我有事情错了地方。我不确定ViewDidLoad和ViewWillAppear的排序是否正确,或者他们是否有正确的代码段。 –
为什么使用表格视图来显示单个值?为什么不UILabel,例如? https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/ – fishinear