码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UITableView

时间:2015-11-06 12:54:17      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

Row  Section  Cell   Cell重用
Row  行   Section  组/区    从0开始
Cell  显示内容  表视图的每?一条数据都是显?示在UITableViewCell对象中,继承一UIView
Cell重用机制  重用池放置闲置Cell  先从重用池里区,有 直接哪里用  ;没有 就alloc
 // queue:队列  reusable:重用  identifier:标识符
UIScrollView 是所有滚动视图的基类
系统的单例 
UIScreen 是单例  AppDelegate 单例 
NSUserDefaults是单例    UIApplication
 
//- (NSInteger)tableView    快速找到方法
//- (UITableViewCell *)ta     快速找到方法
 
技术分享
 
//  AppDelegate.m
//  UI09_UITableView
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 z_han. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
   
    RootViewController *rootVC = [[RootViewController alloc]init];
    UINavigationController *naviC = [[UINavigationController alloc]initWithRootViewController:rootVC];
    self.window.rootViewController = naviC;
   [rootVC release];
    [naviC release];
    [_window release];
    return YES;
}
---------------------------------------------------------
//  RootViewController.m
//  UI09_UITableView

#import "RootViewController.h"
#import "FirstViewController.h"
@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
   
    UITableView *myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];// 分行的
//    UITableViewStyleGrouped 分组的
    myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:myTableView];
    [myTableView release];
   
    myTableView.delegate = self;// 基本配置代理
    myTableView.dataSource = self;// 数据源代理
   
    // 去掉cell的线
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 
   UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
    headerView.backgroundColor = [UIColor colorWithRed:1.000 green:0.252 blue:0.457 alpha:1.000];
    // 设置tableView的表头视图
    myTableView.tableHeaderView = headerView;
    [headerView release];
   
//    [self setAutomaticallyAdjustsScrollViewInsets:NO];
   
    // 刷新整个tableview
    [myTableView reloadData];
    // 刷新指定的cell
//    [myTableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]
 
}
 
// 设置cell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
 
// 设置Section的个数 区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
 
// 设置每个Section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }
    if (section == 1) {
        return 3;
    }
    if (section == 2) {
        return 4;
    }
    return 5;
}
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        //1. 先从重用池里取
    // queue:队列  reusable:重用  identifier:标识符
    // 标识符是区别不同Cell的唯一标记
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        //2. Cell是否为空,为空则alloc
//    if (!cell)
    if (cell == nil) {// 对象可以直接==
//        Cell是否为空,为空则创建
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];// style  类型  自己试试
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];// 副标题
       
    }
    //**  注意执行顺序  重用机制  显示最后的代码
    NSString *string = [NSString stringWithFormat:@"section = %ld,row = %ld",indexPath.section,indexPath.row];
    cell.textLabel.text = string;
    if ((indexPath.section == 1) && (indexPath.row == 2)) {
        cell.textLabel.text = @"第三步";
    }
    /*  if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"section.0 row.0";
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"section.0 row.1";
        }
    }
   
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"section.1 row.0";
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"section.1 row.1";
        }
        if (indexPath.row == 2) {
            cell.textLabel.text = @"section.1 row.2";
        }

    }
   
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"hahahaha";
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"hihihihi";
        }
        if (indexPath.row == 2) {
            cell.textLabel.text = @"hehehehe";
        }
        if (indexPath.row == 3) {
            cell.textLabel.text = @"kekekeke";
        }
    }*/
    // 副标题
    cell.detailTextLabel.text = @"副标题";
    //设置cell显示图片
    cell.imageView.image = [UIImage imageNamed:@""];
    // 取消cell的置灰效果
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
  //
    return cell;
}
// 设置字母检索
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", nil];
}

// 设置Section表头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"A";
    }
    if (section == 1) {
        return @"B";
    }
    if (section == 2) {
        return @"C";
    }
    return @"D";
}
// 选中cell触发的方法
// didDeselectRowAtIndexPath 选中的时候不触发,选中下一个cell时触发
// didSelectRowAtIndexPath 选中cell时触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    // 选中的时候有置灰效果,离开时消失选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
    // 自定义cell下标位置路径
    NSIndexPath *indexPt = [NSIndexPath indexPathForRow:2 inSection:2];
    // 滚动到某一行cell
    [tableView scrollToRowAtIndexPath:indexPt atScrollPosition:UITableViewScrollPositionBottom animated:YES];
      
    NSLog(@"section = %ld row = %ld",indexPath.section, indexPath.row);
    FirstViewController *firstVC = [[FirstViewController alloc]init];
    NSString *string1 = [NSString stringWithFormat:@"section = %ld,row = %ld",indexPath.section,indexPath.row];
    firstVC.string = string1;
//     firstVC.indexPath = self.indexPath;
//    [self.navigationController pushViewController: firstVC animated:YES];
    [firstVC release];
   
}
//- (NSInteger)tableView    快速找到方法
//- (UITableViewCell *)ta     快速找到方法

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
@end
-=========———=========—=——————=-=====-=
//  FirstViewController.h
//  UI09_UITableView
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 z_han. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (nonatomic,retain)NSString *string;

//@property (nonatomic, retain)NSInteger indexPath;
@end
=-----------------------------------------
//  FirstViewController.m
//  UI09_UITableView
 

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.853 blue:0.000 alpha:1.000];
   
   
    self.navigationItem.title = self.string;
   
//    self.title = [NSString stringWithFormat:@"%d %d",self.indexPath.section, self.indexPath.row];
   
}

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

iOS UITableView

标签:

原文地址:http://www.cnblogs.com/z-han49888/p/4942207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!