在HDScrollview.h
中定义了UIImageView类别。它未在GoodDetailViewController.m
中导入,但当代码cell = [[[NSBundle mainBundle] loadNibNamed: CellIdentifier owner: self options: nil] lastObject];
运行时,-(id)initWithCoder:(NSCoder *)aDecoder
中的HDScrollview.m
将被调用。
这里发生了什么?任何帮助将不胜感激。
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
...
答案 0 :(得分:1)
当您从Nib文件实例化视图对象时,iOS将使用nib文件的XML详细信息,并将从中创建解码器。然后,它将调用视图的initWithCoder:
方法,以便您的视图可以使用解码器信息初始化其状态和属性。
答案 1 :(得分:1)
除非要在类别中添加新方法并在控制器中使用它,否则不需要显式导入类别(因为编译器知道方法可用,所以需要导入)。在这里发生的事情是你的Xib可能包含UIImageView
,而Xib被加载时会创建图像视图,这将导致initWithCoder
被调用,因为对象是从Xib实例化的(这是对于从Xib实例化的所有对象都是通用的。没有魔法在这里发生