10.3 UIScrollview概念和用法:ScrollView和PageControl


可以将ScrollView加到tableview的header上,这样是最简单地;
也可以加到tableview的cell上;

tableView中图片在滚动时不去加载图片,在滚动结束后加载图片
scrollViewDidEndDragging或scrollViewDidEndDecelerating中才加载图片;


isMemberOfClass和isKindOfClass的异同
两者都能检测一个对象是否是某个类的成员,两者之间的区别是:

isKindOfClass不但可以用来确定一个对象是否是一个类的成员,也可以用来确定一个对象是否是派生自该类的类的成员 ,

而isMemberOfClass做不到后一点。

如classA派生自NSObject类,classA *a = [classA alloc] init];
[a isKindOfClass:[NSObject class]] 可以检查出a是否是NSObject派生类的成员,但isMemberOfClass做不到。


error报错---解决;
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class 
for the identifier or connect a prototype cell in a storyboard'


tableview的两个重用cell方法
http://blog.csdn.net/wangpotongxue/article/details/9984835


关于UITableView在ios6.0中的新用法  
http://lizi464789754.blog.163.com/blog/static/168937085201391114858248/


原因分析主要在这个方法;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
//注意看这里,之前的老方法是dequeueReusableCellWithIdentifier: 没有后面的forIndexPath参数
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];




测试代码:

- (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];
    
    MainViewController *view = [[MainViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:view];
    self.window.rootViewController = nav;
    
    return YES;
}


@interface MainViewController : UITableViewController

//
//  MainViewController.m
//  my_ScrollView
//
//  Created by fenghuo on 14-7-21.
//  Copyright (c) 2014年 fenghuo. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController () <UIScrollViewDelegate>

@end

@implementation MainViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
    
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.delegate = self;
    
    // 支持分页
    scrollView.pagingEnabled = YES;
    // 设置内容大小
    scrollView.contentSize = CGSizeMake(320 * 4, 180);
    scrollView.showsHorizontalScrollIndicator = YES;
    
    // scrollView添加到tableView的header上
    self.tableView.tableHeaderView = scrollView;
    
    float _x = 0;
    for (int index = 0; index < 4; index++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0 + _x, 0, 320, 180)];
        NSString *imgName = [NSString stringWithFormat:@"image%d.jpg", index + 1];
        
        imgView.image = [UIImage imageNamed:imgName];
        [scrollView addSubview:imgView];
        
        _x += 320;
        
    }
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 150, 320, 30)];
    pageControl.numberOfPages = 4;
    pageControl.tag = 101;
    [self.view addSubview:pageControl];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

#pragma mark - UIScrollView Delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isMemberOfClass:[UITableView class]]) {
        
    } else {
        // scrollView.contentOffset.x---为x方向偏移量
        int current = scrollView.contentOffset.x / 320;
        
        UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:101];
        
        pageControl.currentPage = current;
    }
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    //注意看这里,之前的老方法是dequeueReusableCellWithIdentifier: 没有后面的forIndexPath参数
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"row : %d", indexPath.row];
    
    return cell;
}


@end

运行结果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值