初始化表时传递参数

时间:2011-06-06 08:52:59

标签: iphone uitableview

好的,我完全被这里难过了。

这适用于CouponListViewController.m:

- (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     self.couponList = [CouponDatabase database].couponList;
     self.title = @"Coupon List";
}

这适用于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//
    }
}

但是,当我从这里更改CouponDatabase.h(适用于上述内容)时:

@class CouponDetails;

@interface CouponDatabase : NSObject {
    sqlite3 *_database;
}

+ (CouponDatabase *)database;
- (NSArray *)couponList;
- (CouponDetails *)couponDetails:(int) uniqueId;

...到此(如果我在方法中手动设置'selectedCategory'的值,则有效):

@class CouponList;
@class CouponDetails;

@interface CouponDatabase : NSObject {
    sqlite3 *_database;
}

+ (CouponDatabase *)database;
- (CouponList *)couponList:(int) selectedCategory;
- (CouponDetails *)couponDetails:(int) uniqueId;

然后将CouponListViewController.m更改为:

1    - (void)viewWillAppear:(BOOL)animated {
2       [super viewWillAppear:animated];
3       self.couponList = [[CouponDatabase database] couponList:_selectedCategory];
4       self.title = @"Coupon List";
5    }

我在上面第3行收到此错误:

warning: incompatible Objective-C types 'struct CouponList *', 
expected 'struct NSArray *' when passing argument 1 of 'setCouponList:' 
from distinct Objective-C type

问题:'self.couponlist'行的正确格式是什么,以便我可以将整数传递给CouponDatabase以在couponList方法中使用?

编辑:我知道couponDetails现在是一个类而不是数组 - 我只是不知道如何格式化行以初始化表数据。

我希望这是有道理的 - 对此的任何帮助都将非常感激。

提前致谢!

添加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

2 个答案:

答案 0 :(得分:0)

尝试将CouponListViewController.h更改为:

#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

哎呀我把我的回复放在我自己的原帖中,应该把它放在这里:

编辑:好的,我按照罗伯特的建议对CouponListViewController.h进行了更改,并添加了 @class CouponList; ,如下所示:

#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

我仍然在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];
}

错误1:在非结构或联合的情况下请求成员'couponList'

警告1:'CouponList'可能无法响应'-count'

警告2:return从没有强制转换的指针生成整数

警告3:'CouponList'可能无法响应'-objectAtIndex:'

答案 1 :(得分:0)

在CouponDatabase的原始代码中,您要更改定义:

- (NSArray *)couponList;

这个:

- (CouponList *)couponList:(int) selectedCategory;

然而,您将该返回值用作列表视图控制器的数据源,因此。在这里你应该修复不匹配。如何解决它取决于您的应用程序的语义。您尝试使用- (CouponList *)couponList:(int) selectedCategory;做什么?真正归还这个选择器的是什么?接口CouponList是什么?可能你应该改变这一行:

 self.couponList = [[CouponDatabase database] couponList:_selectedCategory];

以便它从CouponList返回NSArray构建。但我不确定你的对象的语义,所以情况可能并非如此。