UITableView和UIScrollView
2016-02-20 本文已影响158人
pingui
如何在UITableView中添加ScrollView?
- (void)viewDidLoad {
[super viewDidLoad];
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
[self.view addSubview:myTableView];
// 绑定数据源个委托
myTableView.dataSource = self;
myTableView.delegate = self;
_MyScrollView.contentSize = CGSizeMake(WIDTH * 2, HEIGHT);
// 注册第一种单元格
[myTableView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
// 注册第二种单元格
[myTableView registerNib:[UINib nibWithNibName:@"MyTableViewCell2" bundle:nil] forCellReuseIdentifier:@"cell2"];
// 加载数据
[self loadDaModel];
[self setAutomaticallyAdjustsScrollViewInsets:NO];
// 定时器
timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changView:) userInfo:nil repeats:YES];
}
// 定时器定时滚动滚动视图
- (void) changView:(NSTimer *)sender{
// 根据tag值获取UIScrollView
UIScrollView *sView = (id)[self.view viewWithTag:300];
static int currentIndex = 0;
// 这里给滚动视图加了6张图片
currentIndex = (currentIndex + 1 ) % 6;
CGPoint offsetPoint = sView.contentOffset;
offsetPoint.x = WIDTH * currentIndex;
[sView setContentOffset:offsetPoint animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 定制单元格
// 同一个UITableView可以定制多种单元格
if (indexPath.row == 0) {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
for (int i=0; i<6; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"0%d.jpg",i]]];
imageView.frame = CGRectMake(WIDTH * i, 0, WIDTH, 180);
[cell.firScoView addSubview:imageView];
}
cell.firScoView.contentSize = CGSizeMake(WIDTH * 6, 180);
return cell;
}
else{
MyTableViewCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
cell2.titleLabel.text = dataArray[indexPath.row - 1];
return cell2;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataArray.count + 1;
}
- (void) loadDaModel{
if (!dataArray) {
dataArray = [NSMutableArray array];
}
for (int i = 0; i<6; i++) {
NSString *str = [NSString stringWithFormat:@"第%d行",i+2];
[dataArray addObject:str];
}
}
// 定制单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
return 200;
}
else {
return 130;
}
}
效果图:
iOS Simulator Screen Shot.png