IOS:最简单一个使用自定义的TableViewCell例子

一直在忙,也没空搞别的,之前写的都弄丢了,觉得还是做个笔记比较好;

既然是最简单的,那么直接计重点;

1,在界面里拖入一个 TabVlew;

2 ViewControler .h 的代码要实现两个协议;

//
//  ViewController.h
//  intbird
//
//  Created by intbird on 15/3/5.
//  Copyright (c) 2015年 intbird. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>

@end
3,ViewControl.m中的代码

//
//  ViewController.m
//  intbird
//
//  Created by intbird on 15/3/5.
//  Copyright (c) 2015年 intbird. All rights reserved.
//

#import "ViewController.h"
#import "SimpleTableCell.h"

@interface ViewController ()

@end

@implementation ViewController

NSMutableArray * array=nil;

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;//看名字就知道,这个是共多少条数据;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  100;//每行的高度,这个不像Android可以自适应,需要指定高度,Android中最好跟布局是一个独立的LiearLayout,方便隐藏显示ITem;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * simpleTableIndentifier = @"SimpleTableCell";//指示,和xib界面指定的Identity一致;
    SimpleTableCell *  cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIndentifier];//SimpleTableCell为自定义的类名,头部导入 SimpleTableCell.h 文件,
    if(nil==cell){//复用同一个Cell,好比android的convertView = null
        cell = [[SimpleTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIndentifier];
        
    }
    cell.thumbnailImageView.image = [UIImage imageNamed:@"image_default.png"];//这个布局只有一个image...
    return cell;
}

@end
4,大致操作,截图比较费时,描述原理也就还好了;

cellForRowAtIndexPath 这个文件好比是Android的getView. 先拿到一个Cell,

就像是Android根据layoutId获取到View一样,

这里根据simpleTableIndentifier获取到Cell;

安卓的布局文件是个xml文件,那么IOS呢,IOS也是他自己的文件类型, .xib;

IOS是MVC模式的,所以这个xib只是个界面,那么怎么就需要和Control和Mode建立关系;

5.IOS xib界面和 控制逻辑和模型文件建立联系;

 1,新建一个xib文件,界面随意设计,比如添加一个 ImageView;

 2,新建一个模型文件这个文件包含界面中的所有元素 比如 UIImageView * imageView;

 3,.m文件将模型和xib界面文件进行关联,

 在xib界面中点击整个界面边界区域,

 在Custom Class中添加要控制他的.m文件名,

 在界面上点击鼠标右键就会看到这个类文件里的变量,

拖动变量后面的加号到界面的指定的元素上;

这样MVC就架设完毕了




总体文件结构



猜你喜欢

转载自blog.csdn.net/intbird/article/details/44088375