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
- ios拼图游戏(三)之使用TableView布局
- ios拼图游戏(五)之嵌套TableView和水平TableView
- ios拼图游戏(一)之分割图片
- [iOS开发]屏幕适配四:Xcode7使用Autolayout拖拽布局基础(3)TableView自动布局
- iOS之TableView分组目录(快速索引)的使用
- ios拼图游戏(四)之触摸交换拼图
- IOS 构造和使用TableView(基于storyboard) (三)附件的使用
- iOS之Tableview重用出现的错乱三种解决方法
- iOS项目开发实战——学会使用TableView列表控件(三)了解Section
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS开发之tableView(自用贴)
- iOS之tableView优化
- iOS笔记之tableView
- iOS中tableview的使用(1)
- ios tableView 的使用
- iOS TableView 使用(swift)
- iOS TableView 使用详解
- handler和asyncTask比较
- NSUrSession和AFN
- PC SDK PDFs
- 使用Jmeter测试Webservice简单示例
- Unity Official Tutorial --- CREATING A BREAKOUT GAME FOR BEGINNERS
- ios拼图游戏(三)之使用TableView布局
- leetcode——117——Populating Next Right Pointers in Each Node II
- POJ 3321Apple Tree(dfs序 树状数组)
- secureCRT光标不见啦
- 利用referer请求头防止“盗链”
- PHP基础之进制的转换
- 一个xib文件中最有一个view
- Netty的Protobuf开发列子
- 96. Unique Binary Search Trees