本次主要讲中间的三个按钮以及tableViewCell
先说说中间那三个按钮吧;其实这是我留下的一个坑,因为我就用了个xib,然后再xib里定义它的位置和大小,以及背景图片,再.m写了代理被点击后触发的函数,当然触发还是空白的,但我留着这三个按钮,主要是因为当时为了让它适配iPhone4和iPhone5的屏幕,三个按钮里屏幕边框的距离,三个按钮之间的间距,上下间距,着实让我头疼了很久,结果再xib里设置了一下Autoresizing(感觉没这个简单),勉强的解决了
代码也是相对的简单
HomeheaderView.h
#import <UIKit/UIKit.h>
@protocol HomeheaderViewDelegate;
@interface HomeheaderView : UIView
{
__weak id<HomeheaderViewDelegate> _delegate;
}
@property(weak,nonatomic)id<HomeheaderViewDelegate> delegate;
@property(weak,nonatomic)IBOutlet UIButton *recommendButton;
@property(weak,nonatomic)IBOutlet UIButton *comicButton;
@property(weak,nonatomic)IBOutlet UIButton *animationButton;
@end
@protocol HomeheaderViewDelegate <NSObject>
@optional
-(void)didClickButton:(UIButton *)sender;
@end
HomeheaderView.m
#import "HomeheaderView.h"
@implementation HomeheaderView
-(void)awakeFromNib{
[_recommendButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[_comicButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[_animationButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)click:(UIButton *)sender{
if (!_delegate) {
NSLog(@"delegate is nil");
}else{ NSLog(@"delegate is exist");}
if([_delegate respondsToSelector:@selector(didClickButton:)])
{
[_delegate didClickButton:sender];
}
}
@end
HomeheaderView.xib
接着讲人气动画与人气漫画
这个明显是用了tableView和tableViewCell
HomepageViewController.xib拖了一个tableview控件(其实只要用代码定义就行了。。。当时太年轻)
#import "BaseViewController.h"
#define kSectionX kScreenWidth/2 - 75
#define kSectionY 10
#define kSectionWidth 150
#define kSectionHeight 20
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kTabBarHeight self.tabBar.frame.size.height
#define kZSGBgColor [UIColor colorWithRed:254.f/255.f green:87.f/255.f blue:178.f/255.f alpha:1]
@class HomeheadScroll;
@interface HomepageViewController : BaseViewController
@property(strong,nonatomic)IBOutlet HomeheadScroll *homeScrollView;
@property(weak,nonatomic)IBOutlet UITableView *homePageTableView;<span style="white-space:pre"> </span>//tableView
@property(nonatomic,copy)NSMutableArray *block_name;
@property(nonatomic,copy)NSMutableArray *block_picture;
@property(nonatomic,copy)NSMutableArray *block_intro;
@end
每一个代理前都已经写明了这个函数的作用
#pragma mark- tableView的函数
//返回tableView的分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//为一个section添加一个header view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 1) {
return [self viewAndImageName:@"new_comic"];
}else if (section == 2) {
return [self viewAndImageName:@"new_animation"];
}
return nil;
}
- (UIView *)viewAndImageName:(NSString *)imageName {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
imageView.frame = CGRectMake(kSectionX, kSectionY-20, kSectionWidth, kSectionHeight);
[view addSubview:imageView];
return view;
}
//返回每一组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (!self.data) {
return 0;
}
if(section == 1) {
NSArray *nsarray_data=[self.data comic];
return [nsarray_data count];
}
else if (section == 2) {
NSArray *nsarray_data=[self.data animation];
NSLog(@"hhhh%lu",(unsigned long)[nsarray_data count]);
return [nsarray_data count];
}
return 0;
}
//通过indexPath 来设定每一行行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==1) {
return 100;
}
else if(indexPath.section==2){
return 100;}
return 0;
}
//设置TableView中每一行显示的内容和格式的
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *cell_tableview_data;
NSDictionary *cell_tableview_data_ani;
if (indexPath.section==1) {
HomepageViewcell *cell=[HomepageViewcell cellWithTableView:tableView];
cell.delegate=self;
NSArray *cell_data=[self.data comic];
cell_tableview_data=[cell_data objectAtIndex:indexPath.row];
//NSLog(@"%d",indexPath.row);
cell.block_name=[cell_tableview_data video_name];
cell.block_picture=[cell_tableview_data video_image];
NSLog(@"%@",[cell_tableview_data video_intro]);
cell.block_intro=[cell_tableview_data video_intro];
cell.block_id=indexPath.row;
cell.block_section=indexPath.section;
return cell;
} else if (indexPath.section==2)
{
HomepageViewcell *cell_ani=[HomepageViewcell cellWithTableView:tableView];
cell_ani.delegate=self;
NSArray *cell_data=[self.data animation];
cell_tableview_data_ani=[cell_data objectAtIndex:indexPath.row];
//NSLog(@"%d",indexPath.row);
cell_ani.block_name=[cell_tableview_data_ani video_name];
cell_ani.block_picture=[cell_tableview_data_ani video_image];
NSLog(@"%@",[cell_tableview_data_ani video_intro]);
cell_ani.block_intro=[cell_tableview_data_ani video_intro];
cell_ani.block_id=indexPath.row;
cell_ani.block_section=indexPath.section;
return cell_ani;
}
return nil;
}
tableViewCell我自己写了一个HomepageViewcell
这是HomepageViewcell.xib
HomepageViewcell.h
我定义cell中block_name(名字);block_image(图片路径),block_intro(作品介绍),block_url(作品路径),
block_section(哪一个section) block_index(section中的第几个),并且声明要重定义一个方法,和一个代理协议
#import <UIKit/UIKit.h>
@protocol HomepageViewcellDelegate;
@interface HomepageViewcell : UITableViewCell
{
__weak id<HomepageViewcellDelegate> _delegate;
}
@property(weak,nonatomic)id<HomepageViewcellDelegate>delegate;
@property(nonatomic,strong)NSString *block_name;
@property(nonatomic,strong)NSString *block_picture;
@property(nonatomic,strong)NSString *block_intro;
@property(nonatomic,assign)NSInteger block_section;
@property(nonatomic,assign)NSInteger block_index;
+(instancetype)cellWithTableView:(UITableView *)tableView;
@end
@protocol HomepageViewcellDelegate <NSObject>
@optional
-(void)didClickcomic:(HomepageViewcell *)view atindex:(NSInteger)index atsection:(NSInteger)section;
@end
HomapageViewcell.m这里就是实现重定义的方法,以及变量的赋值
#import "HomepageViewcell.h"
#import "AFNetworkTool.h"
@interface HomepageViewcell()
@property(nonatomic,weak)IBOutlet UIButton *icon;
@property(nonatomic,weak)IBOutlet UILabel *name;
@property(nonatomic,weak)IBOutlet UILabel *intro;
@end
@implementation HomepageViewcell
//实现重定义的方法
+(instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID=@"HomepageViewCell";
HomepageViewcell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
if (cell==nil) {
cell=[[[NSBundle mainBundle]loadNibNamed:@"HomepageViewcell" owner:nil options:nil]lastObject];
NSLog(@"xib");
}
return cell;
}
-(void)setBlock_name:(NSString *)block_name{
self.name.text=block_name;
}
-(void)setBlock_picture:(NSString *)block_picture{
NSString *image_url=block_picture;
NSLog(@"%@",image_url);
[AFNetworkTool imageRequestWithUrl:image_url success:^(UIImage *image_s)
{ [self.icon setImage:image_s forState:UIControlStateNormal]; } fail:^{
NSLog(@"图片加载失败");
}];
[self.icon addTarget:self action:@selector(icon_button:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)setBlock_intro:(NSString *)block_intro{
self.intro.text=block_intro;
}
-(void)icon_button:(UIButton *)sender{
// NSLog(@"icon_button:%d,%d",self.block_section,self.block_index);
// if (!_delegate) {
// NSLog(@"delegate is nil");
// }
if ([_delegate respondsToSelector:@selector(didClickcomic:atindex:atsection:)]) {
NSLog(@"icon_button");
[_delegate didClickcomic:self atindex:_block_index atsection:_block_section];
}else{
NSLog(@"fail");
}
}
@end
最后讲一下我声明的代理协议
//tableViewCell动画or漫画的跳转
- (void)didClickcomic:(HomepageViewcell *)view atindex:(NSInteger)index atsection:(NSInteger)section
{
NSDictionary *data;
NSString *image_url;
NSArray *comic_data;
if (section==1) {
if (comic_data==nil) {
comic_data=[self.data comic];
}
data=[comic_data objectAtIndex:index];
image_url=[data video_url];
KxMovieViewController *comicVC = [KxMovieViewController movieViewControllerWithContentPath:image_url parameters:nil];
[self.navigationController pushViewController:comicVC animated:YES];
}
else if (section==2){
if (comic_data==nil) {
comic_data=[self.data animation];
}
data=[comic_data objectAtIndex:index];
scrollviewcontroller *animationVC=[[scrollviewcontroller alloc]init];
animationVC.url=[data video_url];
animationVC.num=[data page_num];
[self.navigationController pushViewController:animationVC animated:YES];
}
}
不难看出来,每次我点击后,会判断是哪一个section是动画or漫画?然后初始化不同控制器,一个播发器控制器,一个图片浏览器控制器,接着把数据传递进去,然后让他们负责,我就不管了,至于视频播放控制器是采用第三方框架FFmpeg,图片播放器是根据我之前自己写的Scrollview图片循环播放改写,把循环给去掉了,并重新添加了一个阉割版的”SDwebimage“框架用来获取图片