这里拿tableview增加空数据显示emptyView为例
#import <UIKit/UIKit.h>
@interface UITableView (WFEmpty)
@property (nonatomic, strong, readonly) UIView *emptyView;
-(void)addEmptyViewWithImageName:(NSString*)imageName title:(NSString*)title;
@end
------------ .m --------------------
#import "UITableView+WFEmpty.h"
#import <objc/runtime.h>
static char UITableViewEmptyView;
@implementation UITableView (WFEmpty)
@dynamic emptyView;
- (UIView *)emptyView
{
return objc_getAssociatedObject(self, &UITableViewEmptyView);
}
- (void)setEmptyView:(UIView *)emptyView
{
[self willChangeValueForKey:@"HJEmptyView"];
objc_setAssociatedObject(self, &UITableViewEmptyView,
emptyView,
OBJC_ASSOCIATION_ASSIGN);
[self didChangeValueForKey:@"HJEmptyView"];
}
-(void)addEmptyViewWithImageName:(NSString*)imageName title:(NSString*)title
{
if (!self.emptyView)
{
CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
UIImage* image = [UIImage imageNamed:imageName];
NSString* text = title;
UIView* noMessageView = [[UIView alloc] initWithFrame:frame];
noMessageView.backgroundColor = [UIColor clearColor];
UIImageView *carImageView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-image.size.width)/2, 60, image.size.width, image.size.height)];
[carImageView setImage:image];
[noMessageView addSubview:carImageView];
UILabel *noInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, frame.size.width, 20)];
noInfoLabel.textAlignment = NSTextAlignmentCenter;
noInfoLabel.textColor = [UIColor lightGrayColor];
noInfoLabel.text = text;
noInfoLabel.backgroundColor = [UIColor clearColor];
noInfoLabel.font = [UIFont systemFontOfSize:20];
[noMessageView addSubview:noInfoLabel];
[self addSubview:noMessageView];
self.emptyView = noMessageView;
}
}
@end
runtime为分类 新增属性
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 关于iOS分类:很多说法是只能添加方法,而不能添加成员变量或属性。有些人可能知道,这种说法是不严谨的,并不是绝对不...
- 一般情况下,oc中分类只可以新增方法,不可以新增属性。但是通过关联可以间接实现新增属性. #import @cla...
- 引述 作为iOS开发者的我们都知道,分类是不能直接添加属性的,那么我们有时候又需要实现这个功能,那么我们应该怎么办...