ios
主要代码
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return self.headerSection;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 88;
}
然后是全部首页全部代码
//
// ISRecommandViewController.m
// iShow
//
// Created by administrator on 14-9-15.
// Copyright (c) 2014年 123. All rights reserved.
//
#define kColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define kGlobalBg kColor(230, 230, 230)
#import "ISTransferController.h"
#import "ISScanViewController.h"
#import "ISSearchController.h"
#import "ISCitySelectController.h"
#import "ISTitleButton.h"
#import "ISDetailViewController.h"
#import "ASIHTTPRequest.h"
#import "UIImageView+WebCache.h"
#import "ISRecommandViewController.h"
#import "ISShowCellFrame.h"
#import "ISHeaderView.h"
#import "ISHotController.h"
#define kLocalShowAPI @"http://localhost:8888/iShow/index.php/home/show/localshow"
#define kImagePath @"http://localhost:8888/iShow/images"
#define kPricePath @"http://localhost:8888/iShow/index.php/home/show/showprice"
#define kCitySelectAPI @"http://localhost:8888/iShow/index.php/home/show/searchcity?city="
@interface ISRecommandViewController ()
@property (nonatomic,strong) NSMutableArray * showItems;
@property (nonatomic,strong) NSMutableArray * showFrames;
@property (nonatomic,strong) NSMutableArray * tempArray;
@property (nonatomic,strong) ISTitleButton * citySelect;
@property (nonatomic,strong) ISRecommandHeaderView * headerSection;
@property (nonatomic,strong) ISHotController * hotController;
@property (nonatomic,strong) ISTransferController * transferController;
@property (nonatomic,strong) ASIHTTPRequest * citySelectRequest;
@property (nonatomic,copy) NSString * cityName;
@property (nonatomic,strong) ISDetailViewController * detailView;
@end
@implementation ISRecommandViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad
{
[super viewDidLoad];
ISHeaderView * headerView = [[[NSBundle mainBundle]loadNibNamed:@"ISHeaderView" owner:nil options:nil]lastObject];
headerView.delegate = self;
self.headerSection = [[[NSBundle mainBundle]loadNibNamed:@"ISRecommandHeaderView" owner:nil options:nil]lastObject];
self.headerSection.delegate = self;
self.tableView.tableHeaderView = headerView;
self.tableView.backgroundColor = kGlobalBg;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.cityName = @"深圳";
self.showFrames = [NSMutableArray array];
[self sendAsynchronousRequest];
[self addItems];
[self addNavigationElements];
[self setupResershView];
}
- (void)setupResershView
{
MJRefreshHeaderView *header = [MJRefreshHeaderView header];
header.scrollView = self.tableView;
//[header beginRefreshing];
header.delegate = self;
self.header = header;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
// [self sendCitySelectedRequest];
[self sendAsynchronousRequest];
[self.header endRefreshing];
}
- (void)dealloc
{
[self.header free];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return self.headerSection;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 88;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"selectedCity"]) {
NSDictionary * dic = change;
[self.citySelect setTitle:[dic valueForKey:@"new"] forState:UIControlStateNormal];
self.cityName = [dic valueForKey:@"new"];
if ([self.cityName isEqualToString:@"苏州市"]) {
self.cityName = @"苏州";
}
[self sendCitySelectedRequest];
}
}
- (void)addNavigationElements{
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(-10, 0, 75, 30)];
label.text = @"爱演出";
label.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:22];
label.textColor = [UIColor whiteColor];
[view addSubview:label];
UIBarButtonItem * logo = [[UIBarButtonItem alloc]initWithCustomView:view];
self.citySelect = [ISTitleButton buttonWithType:UIButtonTypeCustom];
self.citySelect.frame = CGRectMake(0, 0, 40, 22);
[self.citySelect setTitle:@"全国" forState:UIControlStateNormal];
[self.citySelect setFont:[UIFont systemFontOfSize:14]];
[self.citySelect setImage:[UIImage imageNamed:@"btn_cityArrow"] forState:UIControlStateNormal];
[self.citySelect addTarget:self action:@selector(citySelectPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * cityButton = [[UIBarButtonItem alloc]initWithCustomView:self.citySelect];
NSMutableArray * leftButtons = [NSMutableArray array];
[leftButtons addObject:logo];
[leftButtons addObject:cityButton];
[self.navigationItem setLeftBarButtonItems:leftButtons];
NSMutableArray * rightButtons = [NSMutableArray array];
UIButton * searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
searchButton.frame = CGRectMake(0, 0, 30, 30);
[searchButton setImage:[UIImage imageNamed:@"icon_search_normal"] forState:UIControlStateNormal];
// [searchButton setImage:[UIImage imageNamed:@"icon_search_press"]
// forState:UIControlStateSelected];
[searchButton addTarget:self action:@selector(searchButtonPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * item1 = [[UIBarButtonItem alloc]initWithCustomView:searchButton];
UIButton * saomiaoButton = [UIButton buttonWithType:UIButtonTypeCustom];
saomiaoButton.frame = CGRectMake(0, 0, 30, 30);
[saomiaoButton setImage:[UIImage imageNamed:@"icon_saomiao_normal"] forState:UIControlStateNormal];
// [saomiaoButton setImage:[UIImage imageNamed:@"icon_saomiao_press"] forState:UIControlStateSelected];
[saomiaoButton addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * item2 = [[UIBarButtonItem alloc]initWithCustomView:saomiaoButton];
[rightButtons addObject:item2];
[rightButtons addObject:item1];
[self.navigationItem setRightBarButtonItems:rightButtons];
}
- (void)scanButtonPressed{
RootViewController * vc = [[RootViewController alloc]init];
self.navigationController.navigationBarHidden = YES;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)searchButtonPressed{
ISSearchController * controller = [[ISSearchController alloc]init];
[self.navigationController pushViewController:controller animated:YES];
}
- (void)citySelectPressed{
ISCitySelectController * vc = [ISCitySelectController alloc];
vc.lastController = self;
[self presentViewController:vc animated:YES completion:nil];
}
-(void)sendCitySelectedRequest
{
// 演出请求
if (![self.cityName isEqualToString:@"全国"]) {
NSString * str = [kCitySelectAPI stringByAppendingString:self.cityName];
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
self.citySelectRequest = [ASIHTTPRequest requestWithURL:url];
self.citySelectRequest.delegate = self;
[self.citySelectRequest startAsynchronous];
}
else
[self sendAsynchronousRequest];
}
-(void)sendAsynchronousRequest
{
// 演出请求
NSURL *url = [NSURL URLWithString:kLocalShowAPI];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 设置代理
request.delegate = self;
[request startAsynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request
{
// 请求结束获得结果
if (request == self.citySelectRequest) {
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingMutableContainers error:nil];
NSNumber *status = [dic objectForKey:@"status"];
if ([status intValue] == 1) {
self.tempArray = [NSMutableArray array];
self.tempArray = [dic objectForKey:@"show"];
[self addItems];
[self.tableView reloadData];
}
else{
NSLog(@"请求失败");
}
}
else
{
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingMutableContainers error:nil];
NSNumber *status = [dic objectForKey:@"status"];
if ([status intValue] == 1) {
self.tempArray = [NSMutableArray array];
self.tempArray = [dic objectForKey:@"show"];
[self addItems];
[self.tableView reloadData];
}
else{
NSLog(@"请求失败");
}
}
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"请求失败");
}
- (void)addItems{
NSMutableArray * itemsArray = [NSMutableArray array];
if (self.tempArray == nil) {
self.tempArray = [NSMutableArray array];
}
NSMutableArray * dicArray = self.tempArray;
NSMutableArray * framesArray = [NSMutableArray array];
for (NSDictionary * dic in dicArray) {
ISShowContent * item = [ISShowContent cellWithDict:dic];
ISShowCellFrame * frame = [[ISShowCellFrame alloc]init];
[frame setContent:item];
[framesArray addObject:frame];
[itemsArray addObject:item];
}
self.showFrames = framesArray;
_showItems = itemsArray;
}
- (void)ISRecommandHeaderViewdidSelectedCalendar:(ISRecommandHeaderView *)headerView{
CalendarHomeViewController * vc = [[CalendarHomeViewController alloc]init];
[vc setAirPlaneToDay:365 ToDateforString:nil];
__block CalendarHomeViewController * blockVC = vc;
vc.calendarblock = ^(CalendarDayModel *model){
[blockVC showMenu:[model toString]];
};
[self.navigationController pushViewController:vc animated:YES];
}
- (void)ISRecommandHeaderViewdidSelectedMap:(ISRecommandHeaderView *)headerView{
if (self.mapViewController == nil) {
self.mapViewController = [[ISMapController alloc]init];
}
[self.navigationController pushViewController:self.mapViewController animated:YES];
}
- (void)ISRecommandHeaderViewdidSelectedTransfer:(ISRecommandHeaderView *)headerView{
if (self.transferController == nil) {
self.transferController = [[ISTransferController alloc]init];
}
[self.navigationController pushViewController:self.transferController animated:YES];
}
- (void)ISRecommandHeaderViewdidSelectedHot:(ISRecommandHeaderView *)headerView{
if (self.hotController == nil) {
self.hotController = [[ISHotController alloc]init];
}
[self.navigationController pushViewController:self.hotController animated:YES];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.showItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ISShowCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ISShowCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[cell setabcFrame:_showFrames[indexPath.row]];
ISShowContent * content = self.showItems[indexPath.row];
[cell.showImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",kImagePath,content.show_image]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageLowPriority | SDWebImageRefreshCached | SDWebImageRetryFailed];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [_showFrames[indexPath.row] cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ISShowContent * content = self.showItems[indexPath.row];
int num = [content.show_id intValue];
self.detailView = [[ISDetailViewController alloc]initWithId:num];
[self.navigationController pushViewController:self.detailView animated:YES];
}
- (void)recommandPressed:(UIButton *)button{
NSLog(@"111");
int num = button.tag;
self.detailView = [[ISDetailViewController alloc]initWithId:num];
[self.navigationController pushViewController:self.detailView animated:YES];
}
@end