UIImageView類別已在HDScrollview.h
中定義。它不是在GoodDetailViewController.m
中導入的,但是當代碼cell = [[[NSBundle mainBundle] loadNibNamed: CellIdentifier owner: self options: nil] lastObject];
運行時,將會調用-(id)initWithCoder:(NSCoder *)aDecoder
中的HDScrollview.m
。 這是怎麼回事?任何幫助將不勝感激。initWithCoder:無需導入即可調用類別
ps。
HDScrollview.h
導入在GoodDetailViewController.m
但不是在GoodDetailViewController.h
GoodsListViewController.m
...
#import "GoodDetailViewController.h"
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == _mutableArrayGoods.count) {
return [self setLoadMoreCell];
}
static NSString *CellIdentifier = @"TableViewCellGoods";
TableViewCellGoods *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed: CellIdentifier owner: self options: nil] lastObject];
[cell initGoodsCellDefaultStyle];
}
// Configure the cell...
if (indexPath.row < _mutableArrayGoods.count) {
Good *good = [_mutableArrayGoods objectAtIndex: indexPath.row];
cell.good = good;
cell.delegate = self;
}
return cell;
}
HDScrollview.h
#import <UIKit/UIKit.h>
#import "HdPageControl.h"
@protocol HDScrollviewDelegate <NSObject>
-(void)TapView:(int)index;
@end
@interface HDScrollview : UIScrollView<UIScrollViewDelegate>
@property (nonatomic,strong) HdPageControl *pagecontrol;
@property (nonatomic,assign) NSInteger currentPageIndex;
@property (assign,nonatomic) id<HDScrollviewDelegate> HDdelegate;
-(id)initWithFrame:(CGRect)frame withImageView:(NSMutableArray *)imageview;
-(id)initLoopScrollWithFrame:(CGRect)frame withImageView:(NSMutableArray *)imageview;
-(void)HDscrollViewDidScroll;
-(void)HDscrollViewDidEndDecelerating;
@end
@interface UIImageView (CopyImageview)<NSCoding>
@end
HDScrollview.m
...
@implementation UIImageView (CopyImageview)
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.backgroundColor forKey:@"backgroundColor"];
[aCoder encodeObject:self.image forKey:@"image"];
[aCoder encodeInt:self.contentMode forKey:@"contentMode"];
[aCoder encodeInt:self.subviews.count forKey:@"subviewscount"];
for(int i=0;i<self.subviews.count;i++)
{
UIView *view=self.subviews[i];
[aCoder encodeObject:view forKey:[NSString stringWithFormat:@"view%d",i]];
}
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self=[super init]) {
self.backgroundColor=[aDecoder decodeObjectForKey:@"backgroundColor"];
self.image=[aDecoder decodeObjectForKey:@"image"];
self.contentMode=[aDecoder decodeIntForKey:@"contentMode"];
int subviewscount=[aDecoder decodeIntForKey:@"subviewscount"];
for(int i=0;i<subviewscount;i++)
{
UIView* view=[aDecoder decodeObjectForKey:[NSString stringWithFormat:@"view%d",i]];
[self addSubview:view];
}
}
return self;
}
@end
...
類別的機制似乎令人難以置信。 –
類別用於擴展現有類,如添加/修改方法。所有的變化都成爲實際課堂的一部分。意味着如果通過更改現有方法擴展UIImageView類,該更改適用於項目的所有UIImageView's –
我可以理解這一點,但是如果不導入它,會讓我感覺不安全。 –