40.UITableViewController和刷新

来源:互联网 发布:阿里云地址 编辑:程序博客网 时间:2024/06/17 19:59
  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
原创粉丝点击