UITableView基本樣例demo

新一個空工程在appdelegate.m裏面以下代碼數組

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    RootViewController *rvc = [[RootViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rvc];
    [rvc release];
    self.window.rootViewController = nav;
    [nav release];
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

新建一個繼承與UIViewController的RootViewController服務器

#import "RootViewController.h"

#define kScreenSize [UIScreen mainScreen].bounds.size

@interface RootViewController () <UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_dataArr;//數據源數組(給TableView提供數據)
    UITableView *_tableView;//表格視圖
}
@end

@implementation RootViewController
- (void)dealloc {
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"最簡單的表格視圖";//上面導航標題
    [self dataInit];
    [self creatTableView];
}
//初始化數據
- (void)dataInit {
    //數據來源
    //1.本地數據 (從沙盒中獲取解析數據) 、寫一些固定數據
    //2.網絡數據 (從服務器下載數據,下載完成以後,解析)
    
    //先建立一個空數組
    _dataArr = [[NSMutableArray alloc] init];
    
    //建立 100行的 固定數據放入數組中
    for (NSInteger i = 0; i < 100; i++) {
        //100行字符串
        NSString *str = [NSString stringWithFormat:@"第%ld行",i];
        //把str放入到數組
        [_dataArr addObject:str];
        
    }
}
#pragma mark - 建立表格
- (void)creatTableView {
    
    //實例化一個表格視圖對象
    //UITableViewStylePlain普通樣式
    
    //UITableView是一個豎直滾動 水平固定的一個滾動視圖,
    //UITableView的滾動範圍 會根據內容自動計算
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenSize.width, kScreenSize.height-64) style:UITableViewStylePlain];
    
    //設置數據源 (誰來給tableView 提供數據)
    _tableView.dataSource = self;
    //設置代理
    _tableView.delegate = self;
    
    //UITableViewDataSource->裏面的方法主要對tableView數據操做
    //UITableViewDelegate--》裏面的方法主要是tableView的UI操做
    
    //把表格視圖粘貼到self.view
    [self.view addSubview:_tableView];
}
//cell 單元格
#pragma mark - UITableViewDataSource,UITableViewDelegate
//設置 tableView 有 多少分區/組
//這個方法是可選 的  若是不寫 默認是1個分區
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//代理 來設置指定分區 有多少行
//行數 是和數據源數組 元素個數有關係
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArr.count;//有多少元素就能夠多少行cell
}

/*
 重複/循環利用
 1.飛機遊戲的子彈 重複利用
 
 生活中的例子
 1.教室中的電腦 每期班 循環利用
 2.飯店的餐具

 */

//
#if 1
//下面咱們採用 cell 複用的形式 獲取指定分區指定行的cell進行顯示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //複用標誌
    static NSString * cellId = @"Cell";
    
    /*
     dequeueReusableCellWithIdentifier:內部採用了複用隊列
     每次調用dequeueReusableCellWithIdentifier:cellId
     都會從複用隊列中 查看 有沒有 不在屏幕上顯示的空閒的可用的cell 
     //若是沒有 返回 nil             ,那麼就建立新的cell
     //若是有可用的cell 返回cell地址   ,那麼就複用
     
     最多建立cell 個數 一屏 + 1個
     只要cell 離開屏幕 就 會 空閒可複用
     */
    //從複用隊列獲取可用的

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    static NSInteger count = 0;
    if (cell == nil) {
        //表示沒有 可複用的空閒的cell
        //建立新的cell
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId] autorelease];
        count++;
    }
    NSLog(@"count:%ld",count);
    //獲取指定的數據
   // NSString *str = _dataArr[indexPath.row];
    //放在cell上
    //cell的內容標題
   // cell.textLabel.text = str;
    cell.textLabel.text=_dataArr[indexPath.row];
    return cell;
}


#else

/*
 下面的建立的cell方式 是有問題的 下面的寫法會頻繁的建立cell,這樣的話直接下降了執行效率,浪費內存,屏幕 最多顯示 一屏+1,實際上咱們只須要建立一屏+1個cell 就能夠了,這些cell 能夠循環利用/重複利用,提升效率 ,若是一個cell離開屏幕了那麼後面的cell 在顯示的時候能夠服用以前離開cell
 
 */

//cell 單元格
//建立 /獲取 指定cell的地址並填充cell
//要把cell 填充好數據返回

//建立指定分區 指定行的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId  = @"Cell";
    //實例化一個cell
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId] autorelease];
    //NSIndexPath就是一個索引路徑 能夠表示一個cell的確切的一個位置
    //NSIndexPath表示第幾分區 第幾行 cell
    //indexPath.section 獲取分區號  indexPath.row 獲取行號
    
    //從數據源獲取指定行的數據
    NSString *str = _dataArr[indexPath.row];
    
    //填充cell 把數據放在cell 上
    cell.textLabel.text = str;
    
    static NSInteger count = 0;
    count++;
    NSLog(@"count:%ld",count);
    
    return cell;
}

#endif



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
相關文章
相關標籤/搜索