Pass parameter when initializing table
Okay, I'm totally stumped here.
This works in CouponListViewController.m:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.couponList = [CouponDatabase database].couponList;
self.title = @"Coupon List";
}
And this works in CouponDetailViewController.m:
- (void)viewWillAppear:(BOOL)animated {
CouponDetails *details = [[CouponDatabase database] couponDetails:_uniqueId];
if (details != nil) {
[_merchantNameLabel setText:details.merchantName];
[_longDealLine1Label setText:details.longDealLine1];
//....blah...blah//
}
}
But when I change the CouponDatabase.h from this (which works with the above):
@class CouponDetails;
@interface CouponDatabase : NSObject {
sqlite3 *_database;
}
+ (CouponDatabase *)database;
- (NSArray *)couponList;
- (CouponDetails *)couponDetails:(int) uniqueId;
...to this (which works if I manually set the value of 'selectedCategory' inside the method):
@class CouponList;
@class CouponDetails;
@interface CouponDatabase : NSObject {
sqlite3 *_database;
}
+ (CouponDatabase *)database;
- (CouponList *)couponList:(int) selectedCategory;
- (CouponDetails *)couponDetails:(int) uniqueId;
and then change CouponListViewController开发者_如何学Python.m to this:
1 - (void)viewWillAppear:(BOOL)animated {
2 [super viewWillAppear:animated];
3 self.couponList = [[CouponDatabase database] couponList:_selectedCategory];
4 self.title = @"Coupon List";
5 }
I get this error on line 3 above:
warning: incompatible Objective-C types 'struct CouponList *',
expected 'struct NSArray *' when passing argument 1 of 'setCouponList:'
from distinct Objective-C type
Question: What is the proper formatting of the 'self.couponlist' line so that I can pass an integer to the CouponDatabase for use in the couponList method?
EDIT: I'm aware that couponDetails is now a class instead of an array - I just don't know know how to format the line to initialize the table data.
I hope this makes sense - any help on this would be very greatly appreciated.
Thanks in advance!
Adding CouponListViewController.h:
#import <UIKit/UIKit.h>
@class CouponDetailsViewController;
@interface CouponListViewController : UITableViewController {
NSArray *_couponList;
CouponDetailsViewController *_couponDetails;
int _selectedCategory;
}
@property (nonatomic, retain) NSArray *couponList;
@property (nonatomic, retain) CouponDetailsViewController *couponDetails;
@property(nonatomic, assign) int selectedCategory;
@end
Try changing your CouponListViewController.h to this:
#import <UIKit/UIKit.h>
@class CouponDetailsViewController;
@interface CouponListViewController : UITableViewController {
CouponList *_couponList;
CouponDetailsViewController *_couponDetails;
int _selectedCategory;
}
@property (nonatomic, retain) CouponList *couponList;
@property (nonatomic, retain) CouponDetailsViewController *couponDetails;
@property(nonatomic, assign) int selectedCategory;
@end
Oops I put my response in my own original post and should have put it here:
Edit: Okay, I made changes to CouponListViewController.h as recommended by Robert, plus added @class CouponList; as follows:
#import <UIKit/UIKit.h>
@class CouponList;
@class CouponDetailsViewController;
@interface CouponListViewController : UITableViewController {
CouponList *_couponList;
CouponDetailsViewController *_couponDetails;
int _selectedCategory;
}
@property (nonatomic, retain) CouponList *couponList;
@property (nonatomic, retain) CouponDetailsViewController *couponDetails;
@property(nonatomic, assign) int selectedCategory;
@end
I'm still getting errors in CouponListViewController.m:
#import "CouponListViewController.h"
#import "CouponDatabase.h"
#import "CouponList.h"
#import "CouponDetailsViewController.h"
@implementation CouponListViewController
@synthesize couponList = _couponList;
@synthesize couponDetails = _couponDetails;
@synthesize selectedCategory = _selectedCategory;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.couponList = [CouponDatabase database].couponList; // <--- ERROR 1
self.title = @"Coupon List";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_couponList count]; // <--- WARNINGS 1 AND 2
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.couponDetails == nil) {
self.couponDetails = [[[CouponDetailsViewController alloc] initWithNibName:@"CouponDetailsViewController" bundle:nil] autorelease];
}
CouponList *info = [_couponList objectAtIndex:indexPath.row]; // <--- WARNING 3
NSLog(@"%@", info.uniqueId);
_couponDetails.uniqueId = info.uniqueId;
[self.navigationController pushViewController:_couponDetails animated:YES];
}
ERROR 1: request for member 'couponList' in something not a structure or union
WARNING 1: 'CouponList' may not respond to '-count'
WARNING 2: return makes integer from pointer without a cast
WARNING 3: 'CouponList' may not respond to '-objectAtIndex:'
In you original code for CouponDatabase, you are changing the definition:
- (NSArray *)couponList;
for this one:
- (CouponList *)couponList:(int) selectedCategory;
Nevertheless, you use that return value as datasource for a list view controller, so . Here you have a mismatch you should fix. How to fix it depends on the semantics of your application. What are your trying to do with - (CouponList *)couponList:(int) selectedCategory;
? What does really return this selector? What is the interface CouponList
? Possibly you should change the line:
self.couponList = [[CouponDatabase database] couponList:_selectedCategory];
so that it returns an NSArray build from a CouponList
. But I am not sure of the semantics of your objects, so this might not be the case.
精彩评论