自定义视图 自定义代理在tableView视图内形成一个加载信息的效果

一、自定义视图

新建一个类继承自UIView

@protocol RefreshViewDelegate <NSObject>    //写一个协议

@required
- (void)refreshData;
@end


@interface RefreshView : UIView
- (IBAction)buttonAction:(id)sender;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *myActivity;
@property (strong, nonatomic) IBOutlet UIButton *myButton;

@property (assign ,nonatomic) id<RefreshViewDelegate> delegate; //代理

- (void)startLoading;
- (void)stopLoading;


- (IBAction)buttonAction:(id)sender {
    
    if([self.delegate respondsToSelector:@selector(refreshData)])
    {
        [self.delegate refreshData];
    }
    
}

- (void)startLoading
{
    [self.myButton setTitle:@"" forState:UIControlStateNormal];
    [self.myActivity setHidden:false];
    [self.myActivity startAnimating];
    

}

- (void)stopLoading
{
    [self.myButton setTitle:@"加载新数据。。。" forState:UIControlStateNormal];
    
    [self.myActivity setHidden:true];
    
    [self.myActivity stopAnimating];


然后就是要在将要使用这个视图的类里实现自定义视图的协议

@interface YcwFirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,RefreshViewDelegate> //实现了协议
{
    NSMutableArray *arr;  //数据源
}
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) RefreshView *refreshView;

- (void)loadData;
- (void)setupView;



- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupView];
    [self loadData];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  [arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIden = @"myCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
    
    static int i= 1;
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIden];
        i++;
    }

    cell.textLabel.text = [arr objectAtIndex:indexPath.row];
    
    return cell;
    
    
}


- (void)setupView
{
    self.myTableView.delegate = self;
    
    self.myTableView.dataSource = self;
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"RefreshView" owner:self options:nil];  //初始化将要是要的自定义视图
    
    self.refreshView = nib[0];
    
    self.refreshView.delegate = self;
    
    self.myTableView.tableFooterView = self.refreshView;   //将tableView.tableFooterView  设为自定义视图
    
    [self.view addSubview:self.myTableView];

}


- (void)loadData
{
    [self.refreshView startLoading];   //调用自定义视图的startLoading方法
    
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(startLoadData) userInfo:nil repeats:NO];  //延时3s调用startLoadDate方法
    
}
- (void)startLoadData
{
   
    if(i == 0)
    {
        arr = [NSMutableArray arrayWithObjects:@"iphone",@"ipad",@"ipod",@"itouch", nil];
    }
    else{
        [arr addObjectsFromArray:[NSArray arrayWithObjects:@"iphone",@"ipad",@"ipod",@"itouch", nil]];
    
    }
    
    [self.myTableView reloadData];  //
    
    [self.refreshView stopLoading];
    
}

- (void)refreshData     
{
    [self loadData];
    i++;

}

//refreshData这个方法重要,它是属于自定义视图的协议里面的方法,当视图第一次在界面界面加载完成后,你继续点击试图时,会继续加载新的内容,如果refreshData方法里什么都不实现,当你继续点击自定义视图时,就什么事都不做

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值