Objective
It is believed that for iOS developers, in the development process, often with UITableView, will encounter data is empty situation, then need to put a picture on the empty page and a line of text hint data is empty, the following two methods to achieve this function.
The first is to inherit UITableView and integrate pictures and text in the new class
#import <UIKit/UIKit.h>
#import "Const.h"
@interface wfemptytableview:uitableview
@property ( Nonatomic, assign) BOOL Showemptytipview; Whether to display background hint text
@property (nonatomic, assign) Nsinteger Voffset;
@property (nonatomic, copy) NSString *tipstring; Hint text
@property (nonatomic, copy) NSString *tipimagename;//hint picture
@end
Concrete implementation
#import "WFEmptyTableView.h" @implementation Wfemptytableview {UIView *_custombackview;
Uiimageview *_tipimageview;
Uilabel *_label;
CGRect _imageframe;
CGRect _labelframe;
Double _scale; }-(Wfemptytableview *) initWithFrame: (CGRect) Frame style: (Uitableviewstyle) style {self = [super Initwithframe:frame St
Yle:style];
if (self) {[self setupviews];
return self;
}-(void) setupviews {_custombackview = [[UIView alloc] initWithFrame:self.frame];
_custombackview.backgroundcolor = [Uicolor Yellowcolor]; _tipimageview = [[Uiimageview alloc] Initwithframe:cgrectmake ((KSCREENWIDTH-200/2)/2, SELF.FRAME.SIZE.HEIGHT/3, 200/
2, 200/2)];
[_custombackview Addsubview:_tipimageview];
_imageframe = _tipimageview.frame;
_label = [[Uilabel alloc] Initwithframe:cgrectmake (0, Cgrectgetmaxy (_tipimageview.frame), Kscreenwidth, 100)];
_label.backgroundcolor = [Uicolor Clearcolor];
_label.textalignment = Nstextalignmentcenter;
_label.textcolor = [Uicolor Lightgraycolor]; _label.Font = [Uifont systemfontofsize:16];
_label.linebreakmode = nslinebreakbycharwrapping;
_label.numberoflines = 0;
[_custombackview Addsubview:_label];
_labelframe = _label.frame;
}-(void) Setshowemptytipview: (BOOL) showemptytipview {_showemptytipview = Showemptytipview;
if (Showemptytipview) {[Self addsubview:_custombackview];
else {[_custombackview removefromsuperview];
}-(void) settipstring: (NSString *) tipstring {_tipstring = tipstring;
nsmutableattributedstring * attributedString1 = [[Nsmutableattributedstring alloc] initwithstring:tipstring];
Nsmutableparagraphstyle * ParagraphStyle1 = [[Nsmutableparagraphstyle alloc] init];
[ParagraphStyle1 setlinespacing:15];
[ParagraphStyle1 Setalignment:nstextalignmentcenter]; [AttributedString1 addattribute:nsparagraphstyleattributename Value:paragraphstyle1 range:nsmakerange (0, [
Tipstring length])];
[_label setattributedtext:attributedstring1];
[Self resetframe]; }-(void) Settipimagename: (NSString *) tipimageName {_scale = 1;
UIImage *image = [UIImage imagenamed:tipimagename];
_scale = Image.size.height*1.0/image.size.width;
_tipimageview.image = image;
if (isNaN (_scale)) {_scale = 1;
} [self resetframe]; }-(void) Setvoffset: (Nsinteger) voffset {_label.frame = CGRectMake (Cgrectgetminx (_label.frame), Cgrectgetminy (_
Label.frame) +voffset, Cgrectgetwidth (_label.frame), Cgrectgetheight (_label.frame)); _tipimageview.frame = CGRectMake (Cgrectgetminx (_tipimageview.frame), Cgrectgetminy (_tipimageview.frame) +vOffset,
Cgrectgetwidth (_tipimageview.frame), Cgrectgetheight (_tipimageview.frame));
}-(void) resetframe {_tipimageview.frame = CGRectMake (0, Cgrectgetminy (_tipimageview.frame), _scale);
_tipimageview.center = Cgpointmake (kscreenwidth/2.0, _TIPIMAGEVIEW.CENTER.Y); _label.frame = CGRectMake (Cgrectgetminx (_label.frame), Cgrectgetmaxy (_tipimageview.frame), CGRectGetWidth (_
Label.frame), Cgrectgetheight (_label.frame)); } @end
There is another way to use category
#import <UIKit/UIKit.h>
@interface uitableview (wfempty)
@property (nonatomic, Strong, ReadOnly) UIView *emptyview;
-(void) Addemptyviewwithimagename: (nsstring*) imagename title: (nsstring*) title;
@end
Concrete implementation
#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, 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
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.