TableView界面传值和TableViewCell使用

初始化

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNilP{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNilP];
    if (self) {
        self.arr=[NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",@"徐宁",@"董平",nil];
        [self createData];
    }
    return self;
}

创建数据

-(void)createData{
    //找路径
    NSString *path=[[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
    NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"%@",dic);
}

传值六部和scrollView相同,除了第六步需要刷新其他都一样
1.SecondViewController.h

@protocol SecondViewControllerDelegate <NSObject>

-(void)bringName:(NSString *)name;

@end

2.

@property(nonatomic,assign)id<SecondViewControllerDelegate>delegate;

3.SecondViewController.m

[self.delegate bringName:self.textField.text];

4.RootViewController.m

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,SecondViewControllerDelegate>

5.

 secVC.delegate=self;

6.

-(void)bringName:(NSString *)name{
    //(1)把名加到数组里
    [self.arr addObject:name];
    //(2)刷新tableview
    //reloadData方法会自动调用协议方法,这样现实的内容就是修改过后的数组内容
    [self.tableView reloadData];

}

TableViewCell使用
1.MovieCell.h
//cell本身就提供了三个属性视图,所以为了避免冲突,一定不要自定义的cell的属性名和系统的冲突

#import <UIKit/UIKit.h>

@interface MovieCell : UITableViewCell

@property(nonatomic,retain)UIView *backgroundView;
@property(nonatomic,retain)UIImageView *movieImageView;
@property(nonatomic,retain)UITextField *movieName;

@end

2.MovieCell.m

#import "MovieCell.h"

#define HEIGHT self.frame.size.height
#define WIDTH self.frame.size.width


@implementation MovieCell
//要实现自定义的cell,一般重写两个方法
1.
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}
-(void)createView{
//在里面只创建视图,不设置尺寸
    self.backgroundView=[[UIView alloc] init];
    self.backgroundView.backgroundColor=[UIColor yellowColor];
    [self.backgroundView release];
    //把属性放到contentView上显示
    [self.contentView addSubview:self.backgroundView];

    self.movieImageView=[[UIImageView alloc] init];
    self.movieImageView.backgroundColor=[UIColor orangeColor];
    [self.backgroundView addSubview:self.movieImageView];
    [self.movieImageView release];

    self.movieName=[[UITextField alloc] init];
    self.movieName.backgroundColor=[UIColor lightGrayColor];
    [self.backgroundView addSubview:self.movieName];
    [self.movieName release];
}

//重写第二个方法
//这个方法是整个cell在出现前所执行的最后一个方法,所以为了能准确的设置它的尺寸,在这个方法里写控件尺寸的设置
-(void)layoutSubviews{
//如果不写,布局可能会出现问题!!!!!!!
    [super layoutSubviews];

  //在这个方法里只设置尺寸    self.backgroundView.frame=CGRectMake(20, 20, WIDTH-40, HEIGHT-40);
    self.movieImageView.frame=CGRectMake(20, 20, WIDTH/3-40, HEIGHT-80);
    self.movieName.frame=CGRectMake(WIDTH/3,(HEIGHT-80)/2, (WIDTH-20)/2, (HEIGHT-80)/2);
}

3.RootViewController.m
引了头文件就可以直接用


#import "RootViewController.h"
#import "MovieCell.h"
#import "UIImageView+WebCache.h"//使用网络图片必须加

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *movieArr;

@end

@implementation RootViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"电影";
    self.tableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.backgroundColor=[UIColor cyanColor];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.rowHeight=180;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;

    [self createData];


}

-(void)createData{
    NSString *path=@"/Users/dllo/Desktop/OC/UI09_本地数据豆瓣/UI09_本地数据豆瓣/movielist.txt";
    NSData *data=[NSData dataWithContentsOfFile:path];
    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"%@",dic);

    self.movieArr=dic[@"result"];
    NSLog(@"%@",self.movieArr);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.movieArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *retues=@"retues";
    MovieCell *cell=[tableView dequeueReusableCellWithIdentifier:retues];
    if (!cell) {
        cell=[[[MovieCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:retues] autorelease];
    }

    NSDictionary *dic=self.movieArr[indexPath.row];
    cell.movieName.text=dic[@"movieName"];

    [cell.movieImageView sd_setImageWithURL:[NSURL URLWithString:dic[@"pic_url"]]];

    return cell;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值