可以将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
运行结果: