初始化表时通过参数 [英] Pass parameter when initializing table

查看:47
本文介绍了初始化表时通过参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我完全被困在这里.

Okay, I'm totally stumped here.

这在CouponListViewController.m中起作用:

This works in CouponListViewController.m:

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

这在CouponDetailViewController.m中有效:

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//
    }
}

但是当我从中更改CouponDatabase.h时(适用于上面的情况):

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;

...对此(如果我在方法内部手动设置'selectedCategory'的值,则可以使用)

...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;

,然后将CouponListViewController.m更改为此:

and then change CouponListViewController.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    }

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

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

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

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

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.

提前谢谢!

添加CouponListViewController.h:

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

推荐答案

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

In you original code for CouponDatabase, you are changing the definition:

- (NSArray *)couponList;

为此:

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

尽管如此,您仍使用该返回值作为列表视图控制器的数据源,因此.在这里,您有一个不匹配的地方应该解决.如何修复它取决于您应用程序的语义.您要如何处理-(CouponList *)couponList:(int)selectedCategory; ?真正返回此选择器的是什么? CouponList 的接口是什么?可能您应该更改以下行:

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];

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

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.

这篇关于初始化表时通过参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆