UITableView练习

2015-12-19 10:05:12

//
// RootViewController.m
// UI10_练习
//
// Created by YIem on 15/12/18.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "RootViewController.h"

import "MyTableViewCell.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSArray *arr;
@property (nonatomic, retain)UITableView *tableView;
@end

@implementation RootViewController

  • (void)dealloc
    {

    [_arr release];
    [super dealloc];

    }

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"电影";
    // tableview
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];

    // 数据
    NSString *phth = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    self.arr = [NSArray arrayWithContentsOfFile:phth];

    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return _arr.count;
    }
  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {

    cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    }
    NSDictionary *dic = _arr[indexPath.row];
    cell.nameLabel.text = dic[@"title"];
    cell.ratingLabel.text = dic[@"rating"];
    cell.pubdateLabel.text = dic[@"pubdate"];
    return cell;

    }

  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
    {
    return 150;
    }
  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    }

*/

@end



//
// MyTableViewCell.h
// UI10_练习
//
// Created by YIem on 15/12/18.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@interface MyTableViewCell : UITableViewCell
@property (nonatomic, retain)UILabel *nameLabel;
@property (nonatomic, retain)UILabel *ratingLabel;
@property (nonatomic, retain)UILabel *pubdateLabel;
@end


//
// MyTableViewCell.m
// UI10_练习
//
// Created by YIem on 15/12/18.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "MyTableViewCell.h"

@implementation MyTableViewCell

  • (void)dealloc
    {

    [_nameLabel release];
    [_ratingLabel release];
    [_pubdateLabel release];
    [super dealloc];

    }

//

  • (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    self.nameLabel = [[UILabel alloc]init];
    _nameLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_nameLabel];
    [_nameLabel release];
    //
    self.ratingLabel = [[UILabel alloc]init];
    _ratingLabel.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:_ratingLabel];
    [_ratingLabel release];
    //
    self.pubdateLabel = [[UILabel alloc]init];
    _pubdateLabel.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:_pubdateLabel];
    [_pubdateLabel release];

    }
    return self;
    }

  • (void)layoutSubviews
    {
    [super layoutSubviews];
    _nameLabel.frame = CGRectMake(30, 30, 100, 40);
    _ratingLabel.frame = CGRectMake(200, 30, 100, 40);
    _pubdateLabel.frame = CGRectMake(30, 100, 300, 30);
    }
  • (void)awakeFromNib {
    // Initialization code

    }

  • (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    }

@end

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »