40.UITableViewController和刷新

  1. 使用UITableViewController不需要签协议和代理
    Movie模型文件
#import <Foundation/Foundation.h>
@interface Movie : NSObject
@property(nonatomic,copy)NSString *movieId;
@property(nonatomic,copy)NSString *movieName;
@property(nonatomic,copy)NSString *pic_url;
@end
#import "Movie.h"
@implementation Movie
- (void)dealloc
{
    [_movieId release];
    [_movieName release];
    [_pic_url release];
    [super dealloc];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end

MyTableViewController.m文件

#import "MyTableViewController.h"
#import "Movie.h"
#import "MBProgressHUD.h"
@interface MyTableViewController ()
@property(nonatomic,retain)UIRefreshControl *control;
@property(nonatomic,retain)MBProgressHUD *hud;
@property(nonatomic,retain)NSMutableArray *movieArr;
@end

@implementation MyTableViewController
- (void)dealloc
{
    [_control release];
    [_hud release];
    [_movieArr release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self createData];
    }
    return self;
}
- (void)createData{
    NSString *str = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        self.movieArr = [NSMutableArray array];
        NSMutableArray *movieArr = dic[@"result"];
        for (NSDictionary *dic in movieArr) {
            Movie *mov = [[Movie alloc] init];
            [mov setValuesForKeysWithDictionary:dic];
            [self.movieArr addObject:mov];
            [mov release];
        }
        //数据加载完毕后刷新表格数据显示,并关闭刷新提示框
        [self.tableView reloadData];
        self.hud.hidden = YES;
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.使用第三方MBProgressHUD实现一个数据刷新等待
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"请等待";

    // 2.使用系统默认的下拉刷新
    self.control = [[UIRefreshControl alloc] init];
    self.control.attributedTitle = [[NSAttributedString alloc] initWithString:@"正在加载数据"];
    [self.view addSubview:self.control];
    //添加事件,使刷新完毕后做相关的事情(加载新数据)
    [self.control addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
}

- (void)changeValue:(UIRefreshControl *)changeValue{
    //先关闭下拉刷新的效果
    [self.control endRefreshing];
    //然后创建一个mov加到数组中,最后显示在第一条数据
    Movie *mov = [[Movie alloc] init];
    mov.movieName = @"屌丝男士";
    [self.movieArr insertObject:mov atIndex:0];
    [self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"------行");
    return self.movieArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"mycell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    Movie *mov = self.movieArr[indexPath.row];
    cell.textLabel.text = mov.movieName;
    return cell;
}
#pragma mark 设置是否允许给tableView上的cell添加菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
#pragma mark 设置是否允许给tableview上的cell添加事件
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    return YES;
}
#pragma mark 当我们点击菜单上的按钮之后就会触发这方法
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    if (action == @selector(copy:)) {
        NSLog(@"拷贝");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值