当前位置: 代码迷 >> Iphone >> 跟iphone主界面宫格类似的视图应该怎么实现呢
  详细解决方案

跟iphone主界面宫格类似的视图应该怎么实现呢

热度:228   发布时间:2016-04-25 06:51:15.0
跟iphone主界面宫格类似的视图应该如何实现呢?
要实现跟iphone主界面一样的宫格视图,应该怎么做呢?就是每一宫格带一个icon图标和一行文字,同时可以响应事件!

希望哪位大侠指点指点一二,最好能贴段代码,嘿嘿,十分感谢!刚学习iphone,一头的雾水,汗!

也可以发到我的邮箱:evan_lynn@163.com 再三感谢!

------解决方案--------------------
没弄过,刚接触IPHONE,也想知道。帮顶。
------解决方案--------------------
iPhone UITableView(利用UITableView实现平滑的九宫格效果)
UITableView是一种“目录视图或叫表视图”(英文名字table view),这种表视图以列表的形式显示或编辑信息,它由一列、多行组成。用户可以通过垂直滚动的方式导航到一个表视图的任意行上,并可以自定义每一行数据的显示方式。

在创建表视图的时候,可以选择两种风格的表视图:UITableViewStylePlain或者UITableViewStyleGrouped,前者是按索引进行排序的,而后者是按组进行分类显示的。

基本上每一个UITableView都有相应的UITableViewController、UITableViewDelegate、UITableViewDataSource类。UITableViewController类作为UITableView的视图控制类(MVC里Controller的角色)负责管理UITableView,它和大多数UIViewController类一样,控制着UITableView的生命周期函数。UITableViewDelegate作为委托模式里的被委托对象的接口,和大多数iPhone委托接口一样,它提供了UITableView子类无法在行为上保持一致的部分,在这里读者可以自定义表视图的显示风格,甚至可以自定义表视图的每一个元素,它的重要接口定义如下:

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
@optional

// Display customization
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

// Variable height support
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// Section header & footer information. Views are preferred over title should you decide to provide both
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; 

// Accessories (disclosures). 
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

// Selection
// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

// Editing
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

// Indentation
-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return'depth' of row for hierarchies

@end

UITableViewDataSource提供了表视图的数据源,下表列出了常见的表视图数据源方法:


Method
 Description
 
tableView:numberOfRowsInSection:
 特定Section内的行数
 
numberOfSectionsInTableView:
 特定数据源的表视图的Section数目
 
tableView:cellForRowAtIndexPath:
 从数据源获取单元格内容并放到特定的行上
 
sectionIndexTitlesForTableView:
 获取一个数据源的表视图的标题
 
tableView:commitEditingStyle:forRowAtIndexPath
 提交单元格内容的修改
 
talbeView:canEditRowAtIndexPath:
 通过返回一个Boolean类型的值来通知表视图某一行能否修改
 
tableView:canMoveRowAtIndexPath:
 通过返回一个Boolean类型的值来通知表视图某一行能否被移动
 
tableView:moveRowAtIndexPath:toIndexPath:
 允许某一个表视图单元格被移动
 

表视图数据源接口提供了表视图数据源操作的常用方法,其中tableView:numberOfRowsInSection和tableView:cellForRowAtIndexPath:是每一个表试图的数据源必须实现的两个方法,前者告诉表视图内有多少行单元格,而后者告诉表视图每一个单元格的内容是什么。程序通过实现这两个方法,可以提供一个表视图所需要的基本信息并供表视图调用。

笔者在下面的例子里会编写一个带导航面板的表视图,这种复杂类型的控件在iPhone中随处可见,如iPod程序、备忘录程序、闹钟程序等。

ü 首先,新建一个“Window-based”项目并命名为“TableProjectOne”。
ü 新建一个UIViewController的子类,并命名为“MyViewController”。
ü 创建一个空的xib并命名为“MyViewController”。
  相关解决方案