#import “ShouyeViewController.h”
#define HCWidth self.view.frame.size.width
#define HCHeight self.view.frame.size.height
@interface ShouyeViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
{
UIScrollView *sc; // 滚动视图
UIPageControl *pageC; // 分页控制符
NSArray *imgArr; // 图l片数组
NSTimer *timer; // 定时器
int k; // 计数
UIImageView *imgV;
}
@property(nonatomic,strong)UITableView *tbv;
@end
@implementation ShouyeViewController
(void)viewDidLoad {
[super viewDidLoad];
self.title = @“万家灯火”;
self.navigationController.navigationBar.titleTextAttributes=
@{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:[UIFont systemFontOfSize:22]};
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
[self.view addSubview:self.tbv];
}
-(UITableView *)tbv{
if (!_tbv)
{
_tbv=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
}
_tbv.delegate=self;
_tbv.dataSource=self;
return _tbv;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section0)
{
return 1;
}else if (section1){
return 1;
}else{
return 3;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath )indexPath{
static NSStringstr=@“str”;
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
if (!cell)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
if (indexPath.section==0)
{
self.tbv.rowHeight=250;
//初始化定时器
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(qqq) userInfo:nil repeats:YES];
// 定时器开启
[timer fire];
// 滚动视图
sc = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, HCWidth, 250)];
// 设置滚动视图的代理
sc.delegate = self;
// 滚动范围
sc.contentSize = CGSizeMake(4 * HCWidth,250);
// 分页
sc.pagingEnabled = YES;
// 弹簧
sc.bounces = NO;
// 禁用水平滚动条
sc.showsHorizontalScrollIndicator = NO;
// 将滚动视图添加
[self.view addSubview:sc];
// 添加图片
for (int i = 0; i < 4; i++){
imgArr = @[@"14",@"15",@"16",@"17"];
// 图片
imgV = [[UIImageView alloc]initWithFrame:CGRectMake(i*HCWidth, 0, HCWidth, 250)];
imgV.image = [UIImage imageNamed:imgArr[i]];
// 设置用户交互属性
imgV.userInteractionEnabled = YES;
// 滚动视图添加图片
[sc addSubview:imgV];
}
[cell addSubview:sc];
}else if (indexPath.section==1){
self.tbv.rowHeight=100;
NSArray *arr=@[@"1",@"2",@"3",@"4"];
NSArray *arr1=@[@"保险精品",@"课程精品",@"车险定制",@"关于我们"];
for (int i=0; i<arr.count; i++)
{
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(15+100*i,10, 70, 70)];
[btn setBackgroundImage:[UIImage imageNamed:arr[i]] forState:UIControlStateNormal];
[cell addSubview:btn];
UILabel *l1 = [[UILabel alloc]initWithFrame:CGRectMake(15 + 100*i, 80, 70, 20)];
[l1 setText:arr1[i]];
[cell addSubview:l1];
}
}
else{
self.tbv.rowHeight=150;
NSArray *arr=@[@"aaaa",@"bbbb",@"cccc"];
if (indexPath.row == 0) {
_tbv.rowHeight = 30;
cell.textLabel.text = @"| 热销产品";
cell.textLabel.textColor=[UIColor orangeColor];
}else{
_tbv.rowHeight = 150;
UIImageView *im = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
im.image = [UIImage imageNamed:arr[indexPath.row]];
[cell addSubview:im];
}
}
return cell;
}
// 视图已经滚动的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
pageC.currentPage = sc.contentOffset.x/HCWidth;
NSLog(@"%lf",sc.contentOffset.x);
}
// 定时器 (1s -> 进入一次)
-(void)qqq{
NSLog(@"%d",k);
// set 方法 -> 设置 滚动视图的偏移量
[sc setContentOffset:CGPointMake(k *HCWidth, 0)];
k++;
if(k > 3){
// 定时器停止
// [timer invalidate];
// // 禁止滚动
// sc.scrollEnabled =NO;
k=0;
}
}
@end