ios拼图游戏(三)之使用TableView布局

来源:互联网 发布:ruby for windows 编辑:程序博客网 时间:2024/06/05 06:17

实现了拼图的分割和手势识别后,突然就想对游戏的界面进行布局,但想到只使用普通的UIView进行布局有点太low,所以就使用稍为高端大气点的TableView布局。

本来是想使用更高端的CollectionView,可是花费了半天时间才发觉把简单的问题复杂化了,而且也不利于后续开发,所以果断转为使用TableView,网上的大神说得好,能够使用TableView解决的事就别扯上CollectionView

以下为创建TableView的代码

//初始化并添加TableView界面-(void)initAndAddGameTableView{        CGRect rect_screen=[[UIScreen mainScreen]bounds];    myTableView_=[[UITableView alloc] initWithFrame:rect_screen style:UITableViewStylePlain];        //禁止滑动    myTableView_.scrollEnabled=NO;        myTableView_.delegate=self;    myTableView_.dataSource=self;    //改变换行线颜色lyttzx.com    myTableView_.separatorColor = [UIColor blueColor];    //设定Header的高度,   // myTableView_.sectionHeaderHeight=50;    //设定footer的高度,   // myTableView_.sectionFooterHeight=100;    //设定行高    //myTableView_.rowHeight=rect_screen.size.height;    //设定cell分行线的样式,默认为UITableViewCellSeparatorStyleSingleLine    [myTableView_ setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];    //设定cell分行线颜色    [myTableView_ setSeparatorColor:[UIColor redColor]];    //编辑tableView    myTableView_.editing=NO;        [self.view addSubview:myTableView_];}
以上代码基本上不需解释,下面为我使用的一些TableVIew代理函数:

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{    }

要使用代理函数,首先要添加两个代理,然后重写以下函数:

//设置分区数-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}//设置每一分区的总行数-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    //return [self.pieArray count];    return 3;}//设置行高-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 50;}
解释一下设置行数和设置行高的函数,如果想每一分区都设置不同的行数可以这样

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    //return [self.pieArray count];        if(section==0){        return 2;    }    if(section==1){        return 3;    }    }
以上代码假定了TableVIew有两个分区,第一个分区设置2行,第二分区设置3行。

如果想每一行的高度都不相同,可以这样

//设置行高-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    CGFloat height;    NSInteger row=indexPath.row;        if(row==0){        height=30.0;    }else if(row==1){        height=40.0;    }else{        height=50.0;    }        return height;}
indexPath是用来获取分区或行的索引。

绘制每个单元格的内容使用以下函数:

//绘制Cell-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{            UITableViewCell* cell=nil;    NSInteger row=indexPath.row;    //初始化Cell,并给其添加一个Label    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];    cell.selectionStyle=UITableViewCellSelectionStyleNone;    UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, CGRectGetWidth(self.view.frame)/2, 20)];    textlabel.text = @"操蛋区";    textlabel.textAlignment = NSTextAlignmentCenter;    [cell.contentView addSubview:textlabel];            return cell;    }
以上是给每一单元格建立一个Label,如果想区别对待每一单元格,可以通过indexPath获取。该函数的调用机制是,初始化每一分区或行(统称单元格)时,都会调用该函数,如果该单元格离开视图再重新出现的时候,就需要重新绘制重新调用该函数,所以里面的数据或视图如果不保存起来就会被重置

当然还有其他TableVIew的函数,但是目前并不需要使用,所以就不介绍了。
总结完毕,如有错误,请指正,谢谢。

0 0
原创粉丝点击