欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现UITableView数据为空时的提示页面

程序员文章站 2024-02-13 10:35:22
前言 相信对于ios开发者们来说,在开发过程中,经常用uitableview,一定会遇到数据为空的情况,这时需要在空页面上放一个图片和一行文字提示数据为空,下面整理了两种...

前言

相信对于ios开发者们来说,在开发过程中,经常用uitableview,一定会遇到数据为空的情况,这时需要在空页面上放一个图片和一行文字提示数据为空,下面整理了两种方法来实现这个功能。

第一个是继承uitableview,在新类中集成图片和文字

#import <uikit/uikit.h>
#import "const.h"

@interface wfemptytableview : uitableview

@property (nonatomic, assign) bool showemptytipview; // 是否显示背景提示文字
@property (nonatomic, assign) nsinteger voffset;
@property (nonatomic, copy) nsstring *tipstring;  // 提示文字
@property (nonatomic, copy) nsstring *tipimagename; // 提示图片

@end

具体实现

#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 style: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), 150, 150 * _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

还有一种方法,是用category

#import <uikit/uikit.h>

@interface uitableview (wfempty)

@property (nonatomic, strong, readonly) uiview *emptyview;

-(void)addemptyviewwithimagename:(nsstring*)imagename title:(nsstring*)title;

@end

具体实现

#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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。