您的位置:首页 > 移动开发

iOS(四)动漫App:五

2016-01-10 18:07 316 查看
本次主要讲分类控制器ClassViewController

在我编写的ClassViewController里分成三个部分,

第一个顶上的scrollview,总共有七个类型(其实就是按钮),可以向右向左滑动,当然点击里面的一个类型后,它还会自动判断,如果它是靠右的类型,右边还有其它类型,它会向左移动,如果点击靠左的类型,左边如果还有其它的类型,他会自动向右移动





定义一个ScrollTabBar继承UIView,它有一个ScrollView,以及一个数组,再ClassViewController获得类型数组(例如

_scrollTabBar.itemArray=@[@"热血",@"运动",@"言情",@"搞笑",@"魔幻",@"科幻",@"悬疑"];

然后根据类型数组的长度,以及定义好的类型按钮默认宽度,生成TabBar

-(void)createTabBarItem:(NSArray *)array{
CGFloat width=0;
for (int i=0; i<array.count; i++) {
UIButton *tabBarItem=[[UIButton alloc] initWithFrame:CGRectMake(self.TabItemwidth*i,0 , self.TabItemwidth, 40)];
tabBarItem.tag=i;
[tabBarItem setTitle:array[i] forState:UIControlStateNormal];
[tabBarItem setTitle:array[i] forState:UIControlStateSelected];
[tabBarItem setTitleColor:_itemSelectColor forState:UIControlStateSelected];
[tabBarItem addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:tabBarItem];
width +=self.TabItemwidth;
if (i==0) {
tabBarItem.selected=YES;
_lastbutton=tabBarItem;
}
}
_scrollView.contentSize=CGSizeMake(width,0);
if (!_delegate) {
NSLog(@"delegate is nil");
}

if ([_delegate respondsToSelector:@selector(xml_data:)]) {
NSLog(@"xml_data");
[_delegate xml_data:0];
}else{
NSLog(@"fail");
}
}
点击类型按钮后需要做两件事,一:是否把自己偏移,二:根据自己的TAG使tableview显示这一类型的数据

<span style="font-size:14px;">-(void)buttonClick:(UIButton *)button{
if (_lastbutton!=button) {
NSLog(@"%f,%f,%f",button.frame.origin.x,_scrollView.contentSize.width,self.frame.size.width);
CGFloat offsetX = button.frame.origin.x - self.frame.size.width / 2;
offsetX = offsetX > 0 ? (offsetX + _TabItemwidth / 2) : 0;
offsetX = offsetX > _scrollView.contentSize.width - _scrollView.frame.size.width ? _scrollView.contentSize.width - _scrollView.frame.size.width : offsetX;

[_scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
_lastbutton.selected=NO;
button.selected=YES;
_lastbutton=button;

//根据button的tag获取网络地址

if ([_delegate respondsToSelector:@selector(xml_data:)]) {
NSLog(@"xml_data");
[_delegate xml_data:button.tag];
}else{
NSLog(@"fail");
}
//根据网络地址xml解析获取数据,然后reload table
}
}</span>


</pre><p></p><p>第二个AFNetworking</p><p>根据button的TAG值,由php语句返回对应的数据</p><p></p><pre name="code" class="objc">-(void)xml_data:(NSInteger)but_tag{

NSString *url=[NSString stringWithFormat:@"http://ashuai.6655.la/Classview.php?case=%d",but_tag];
NSLog(@"%@",url);
[AFNetworkTool xmlRequestWithUrl:url success:^(NSXMLParser *xmlParser) {
self.xmldata=[NSMutableDictionary dictionary];
xmlParser.delegate = self;
[xmlParser setShouldProcessNamespaces:YES];
[xmlParser parse];
NSLog(@"chenggong");
} fail:^{
NSLog(@"xml请求失败");
UIAlertView *warn=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"网络链接失败!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[warn show];
}];

}


Classview.php会根据得到case值,去判断是哪一个类型,热血or运动or other然后用sql语句查找

function select_comic_type($type) //根据类型搜索动画
{
$sql="select * from `comic` where `type`='$type'";
return $this->Search($sql);
}


<?php

include("xml/sql_dll.php");
$dll=new sql_dll();

switch($_GET['case']){
case 0:
$type="热血";
break;
case 1:
$type="运动";
break;
case 2:
$type="言情";
break;
case 3:
$type="搞笑";
break;
case 4:
$type="魔幻";
break;
case 5:
$type="科幻";
break;
case 6:
$type="悬疑";
break;
default:
$type=0;
break;
}
if($type){
$comic_data=$dll->select_comic_type($type);
foreach($comic_data as $each_comic_data){
$return .="<comic>
<video_name>
<value>$each_comic_data[0]</value>
</video_name>
<video_image>
<value>$each_comic_data[1]</value>
</video_image>
<video_intro>
<value>$each_comic_data[2]</value>
</video_intro>
<video_url>
<value>$each_comic_data[3]</value>
</video_url>
</comic>";
}
header("Content-type:text/xml");
echo '<?xml version="1.0" encoding="UTF-8"?>
<data>
'.$return.'
</data>';
exit;

}
else
{
print("no data");
}
?>


返回XML数据,然后XML的代理协议(这里就不贴了,之前有)

第三个tableview和tableViewCell

和之前的tableView还有tableViewCell异曲同工

<span style="font-size:14px;">//返回tableView的分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

//通过indexPath 来设定每一行行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==0) {
return 100;
}
return 0;
}

//返回每一组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section==0) {
NSLog(@"number:%d",[self.data comic].count);
return [self.data comic].count;
}
NSLog(@"tableview");
return 0;

}

//设置TableView中每一行显示的内容和格式的
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==0) {

NSDictionary *cell_tableview_data;
HomepageViewcell *cell=[HomepageViewcell cellWithTableView:tableView];
cell.delegate=self;
NSArray *cell_data=[self.data comic];
cell_tableview_data=[cell_data objectAtIndex: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_section=indexPath.section;
return cell;
}

return nil;
}
- (void)didClickcomic:(HomepageViewcell *)view atindex:(NSInteger)index atsection:(NSInteger)section
{
NSDictionary *data;
NSString *image_url;
NSArray *comic_data;
if (section==0) {
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];
}

}</span>


我觉得很多东西都可以举一反三,主页用到了scrollView,tableView,tableViewCell,代理,AFNetworking,xml解析,分类这个控制器写代码时基本都是从主页那边copy代码了

下一章主要写搜索控制器,因为以前做网页的时候就负责写过搜索的方法,用模糊搜索还是很简单的,复杂一点的搜索就gg了,搜索这一块最大的收获就是学会了UISearch的代理

/article/9297487.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: