MVC 的使用

/**

 做事的时候,目标一定要明确,自己要干的是什么,想干成是什么样,就像走路一样,我选择走的是哪一条路,这条路上,我要走多远

 (对自己的要求是什么,我现在的要求就是,不要图多,每看一个知识点,知道是为什么,并要求自己能举一反三 ,并要求自己真正的弄懂)

 我相信自己每一次的重复,每一次重复都带着自己的思想,这样自己的重复才会真的有价值

  

   自己敲的每一句代码,都是有目的的,不要让自己做哪些没有价值的东西

 学会带着思想去学习,真正的弄懂(对自己的学习的方式是什么)

 *    1,用model来写出表头,表尾,cell的值

 *    2,自定义cell

       3,并使cell的正向传值

 *    4,用模型来加载数据

 *

 *

 */

#import "ViewController.h"

#import "Model.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    

//   定义全局变量(脚踏实地的做事,每一句的代码都要让自己明白)

    NSArray *allTitles;

    

    

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    Model *NewsModel = [[Model alloc]init];

        NewsModel.headTitles = @"音乐";

        NewsModel.foodTitles = @"其他";

        NewsModel.daitilTitles = @[@"快乐",@"摇滚",@"抒情",@"迷人"];

        NewsModel.imageName = @[@"2 17",@"2 18",@"2 19",@"2 20"];

        NewsModel.titles = @[@"吉他",@"架子鼓",@"钢琴",@"笛子"];

    

    Model *wNewsModel = [[Model alloc]init];

        wNewsModel.headTitles = @"音乐";

        wNewsModel.foodTitles = @"其他";

        wNewsModel.imageName = @[@"3 8",@"3 9",@"3 10",@"3 11"];

        wNewsModel.daitilTitles = @[@"快乐",@"摇滚",@"抒情",@"迷人"];

        wNewsModel.titles = @[@"吉他",@"架子鼓",@"钢琴",@"笛子"];

    

//    怎样在加入每一组的内容

//    其中(allTItle表示的是组数,title表示的一组有多少行)

    allTitles = @[NewsModel,wNewsModel];

    

    [self creatTableView];

}

#pragma mark---------创建视图

- (void)creatTableView

{

    

//    UITableView *tableVeiw = [[UITableView alloc]initWithFrame:self.view.frame ];

//    UITableView *tableVeiw = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

    

    

    

    UITableView *tableVeiw = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    tableVeiw.delegate = self;

    tableVeiw.dataSource = self;

    tableVeiw.backgroundColor = [UIColor whiteColor];

    tableVeiw.rowHeight = 100;

    [self.view addSubview:tableVeiw];

    

}

#pragma mark-----------设置视图有多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return allTitles.count;

}

#pragma mark----------设置那一组有多少行

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

{

    

//    选择第几组,这一组有多少行

    Model *mm = allTitles[section];

    return mm.titles.count;

    

}

#pragma mark-----------设置每一行的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

      NSString *cellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

    }

//     取出数据模型(表示选择的是哪一组)

    Model *mm = allTitles[indexPath.section];

    

//    字符串去接收(每一行的内容)

   NSString *music =  mm.titles[indexPath.row];

    NSString *nn = mm.imageName[indexPath.row];

    NSString *bb = mm.daitilTitles[indexPath.row];

    

//    和自定义cell之间的区别,就是名字和自定义cell的文件不一样

    cell.textLabel.text = music;

    cell.imageView.image = [UIImage imageNamed:nn];

    cell.detailTextLabel.text =bb;

    

    return cell;

    

}

#pragma mark ---------设置表头的内容

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

//    表示的是哪一组(设置哪一组的标题)

    

    

    Model *ww = allTitles[section];

    return ww.headTitles;

    

}

#pragma mark------------设置表尾的内容

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

//    选出是哪一组(再依次选择的哪一组的表尾)

    Model *mm = allTitles[section];

    return mm.foodTitles;

    

}

原文地址:https://www.cnblogs.com/wukun16/p/4847422.html