【发布时间】:2014-08-22 17:37:41
【问题描述】:
从我的问题可以看出,我是 Objective C 的新手。我可以访问静态属性,例如 NSString,但似乎我无法访问数组甚至其他属性,一定是做错了什么。基本上,我有一个包含主要属性的模型文件。
这是我的模型文件,名为 ListingManager.h
#import <Foundation/Foundation.h>
@interface ListingManager : NSObject
@property (nonatomic, strong) NSString *listingTitle;
@property (nonatomic, strong) NSString *listingDescription;
@property (nonatomic, strong) NSString *listingPrice;
@property (nonatomic, readonly) NSString *datePosted;
@property (nonatomic, retain) NSMutableArray *lists;
@property (nonatomic) BOOL *viewed;
@end
ListingManager.m
#import "ListingManager.h"
@implementation ListingManager
@synthesize lists;
@synthesize listingPrice;
@synthesize listingDescription;
@synthesize listingTitle;
@synthesize datePosted;
@end
这是我正在使用的另一个视图控制器 ListingTableViewController.m
#import "ListingTableViewController.h"
@interface ListingTableViewController ()
@property (nonatomic, strong) ListingManager *manageListings;
@end
@implementation ListingTableViewController
@synthesize manageListings;
- (void) loadInitialData {
ListingManager *listing1 = [[ListingManager alloc] init];
listing1.listingTitle = @"Title here";
listing1.listingDescription = @"another description..Whatever it is";
listing1.listingPrice = @"100";
[self.manageListings.lists addObject:listing1];
NSLog(@"%@", listing1); //for debugging purposes
ListingManager *listing2 = [[ListingManager alloc] init];
listing2.listingTitle = @"Title here";
listing2.listingDescription = @"a long description here";
listing2.listingPrice = @"50";
[self.manageListings.lists addObject:listing2];
ListingManager *listing3 = [[ListingManager alloc] init];
listing3.listingTitle = @"Cool stuff";
listing3.listingDescription = @"Another descitpion righ there";
listing3.listingPrice = @"90";
[self.manageListings.lists addObject:listing3];
}
- (instancetype)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//self.listings= [[NSMutableArray alloc] init];
self.manageListings = [[ListingManager alloc] init];
[self.backdropTableView reloadData]; //reload table view
[self loadInitialData]; //load inital static data
}
/*
- (void)viewWillAppear:(BOOL)animated
{
[self.backdropTableView reloadData];
}
*/
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.manageListings.lists count]; //this here is not returning anything
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *custom = @"WXCustomCell";
WXTableCell *cell = (WXTableCell *)[tableView dequeueReusableCellWithIdentifier:custom];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WXCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
ListingManager *listingManager = [self.manageListings.lists objectAtIndex:indexPath.row];
cell.titleLabel.text = listingManager.listingTitle;
cell.descriptionlabel.text = listingManager.listingDescription;
cell.thumbnail.image = [UIImage imageNamed:@"nothumb.gif"];
cell.price.text = @"$50";
cell.dateLabel.text = @"N/A";
NSLog(@"%@", cell.titleLabel.text);
//use for later
/*
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"MM/dd/yyy HH:mm:ss"];
NSLog(@"%@",[DateFormatter stringFromDate:[NSDate date]]);
*/
return cell;
}
//Call segue
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"showDetail" sender:nil];
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showDetail"]) {
// Get the new view controller using [segue destinationViewController].
NSIndexPath *indexPath = [self.backdropTableView indexPathForSelectedRow];
DetailsViewController *vc = segue.destinationViewController;
// Pass the selected object to the new view controller.
ListingManager *listingManager = [self.manageListings.lists objectAtIndex:indexPath.row];
vc.titleName = listingManager.listingTitle;
vc.descriptionDetail = listingManager.listingDescription;
}
}
@end
例如,当我返回“[self.manageListings.lists count]”时,它不会返回任何内容,但如果我将其硬编码为 3,我会得到 3 个初始项目,其中标题、描述和标题分别为空值。连接我的模型和 VC 一定是我做错了什么。
【问题讨论】:
标签: objective-c model-view-controller ios7 nsmutablearray