我有一个名为 Product 的类,其中包含一些属性,我想让我的类成为名为 Products 的产品列表的基础。这个列表可以在 UITableView 中访问,以填充 Products 的内容。
此外,每个产品的内容将由 Web 服务填充。
我的代码是:
@interface Product : NSObject
{
int identifier;
NSString* title;
NSString* quantity;
float price;
UIImage* image;
}
@property (nonatomic, assign) int identifier;
@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) NSString* quantity;
@property (nonatomic, assign) float price;
@property (nonatomic, retain) UIImage *image;
-(id)initWithProduct:(int) identifier withTitle:(NSString*)title numberUses:(NSString*)uses withPrice:(float)price withImage:(UIImage*)image;
@end
用他的.m
@implementation Product
@synthesize identifier = _identifier;
@synthesize title = _title;
@synthesize price = _price;
@synthesize quantity = _quantity;
@synthesize image = _image;
- (void)dealloc {
NSLog(@"article dealloc \n");
[self.title release];
[self.quantity release];
[self.image release];
[super dealloc];
}
- (id)init {
self = [super init];
if(self) {
self.title = [[[NSMutableString alloc] init] autorelease];
self.identifier = 0;
self.price = 45.0;
self.quantity = [[[NSMutableString alloc] init] autorelease];
self.image = [[[UIImage alloc] init] autorelease];
}
return self;
}
-(id)initWithProduct:(int) inIdentifier withTitle:(NSString*)inTitle numberUses:(NSString*)inQuantity withPrice:(float)inPrice withImage:(UIImage*)inImage
{
self = [super init];
if (self) {
if (title!= nil) {
self.title = inTitle;
}
if (quantity!= nil) {
self.quantity = inTitle;
}
if (image!= nil) {
self.title = inTitle;
}
self.price = inPrice;
self.identifier = inIdentifier;
}
return self;
}
@end
我的 UITableView 标题是:
@interface TableView : UIViewController
< UITableViewDataSource
, UITableViewDelegate
>{
NSMutableArray *products;
}
在他们中。我有:
编辑 现在我的单元格的标题显示为(空)
- (void)viewDidLoad
{
[super viewDidLoad];
products = [[NSMutableArray alloc] init];
[products addObject:[[Product alloc] initWithProduct:1 withTitle:@"df" numberUses:@"dsf" withPrice:12.3 withImage:[UIImage imageNamed:@"btn_invite"]]];
[products addObject:[[Product alloc] initWithProduct:1 withTitle:@"2" numberUses:@"dsf" withPrice:12.3 withImage:[UIImage imageNamed:@"btn_invite"]]];
[products addObject:[[Product alloc] initWithProduct:1 withTitle:@"4" numberUses:@"dsf" withPrice:12.3 withImage:[UIImage imageNamed:@"btn_invite"]]];
[products addObject:[[Product alloc] initWithProduct:1 withTitle:@"4" numberUses:@"dsf" withPrice:12.3 withImage:[UIImage imageNamed:@"btn_invite"]]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Controller *cell = (Controller*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSString* nameNib = UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM() ? @"Controller" : @"ControllerIph";
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nameNib owner:self options:nil];
cell = [nib objectAtIndex:0];
}
Product* obj = [products objectAtIndex:indexPath.row];
cell.title.text = [NSString stringWithFormat:@"%@", obj.title];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
提前致谢!