UITableView应用1

来源:互联网 发布:阿里云账号登录 编辑:程序博客网 时间:2024/06/05 06:21

一个使用UITableView的实例


self.tableView   是获取整个表格

[self.tableView reloadData];   //刷新表格


UITableView使用的协议有<UITableViewDelegate>和<UITableViewDataSource>


UITableViewDataSource协议中必须要实现的两个方法是

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;               //返回每个区域的行数


- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

//生成单元格,在表格中加载每一个单元格时都会回调这个方法,常用的实现方法是:

{

    static NSString* CellIdentifier = @"Cell";//作为单元格重用的唯一标识

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifer:CellIdentifier];

    if(cell == nil){

        cell = [[[UITableView alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

//其中,UITableViewCellStyle对单元格的内容有四种显示风格,

/*UITableViewCellStyleDefault、UITableViewCellStyleValue1、UITableViewCellStyleValue2、UITableViewCellStyleSubtitle,其中只有后三种才可以显示出单元格的detailTextLabel属性值。*/

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   //设置单元格右边按钮的显示风格,单击右侧按钮,回调的方法是<UITableViewDelegate>协议中的- (void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath方法。

    }

    //然后就是为单元格cell的textLabel、detailTextLabel属性进行赋值


    //然后返回cell

}


下面是<UITableViewDataSource>协议中的一些常用方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;   //返回表格中有几个区域

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section;    //返回指定区域的页眉

- (NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section;      //返回指定区域的页脚

- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;     //返回指定位置的单元格是否可以编辑

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath;     //当单元格进入到编辑模式(插入或者删除),点击插入或者删除按钮调用的方法,来进行更具体的操作。

- (BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;   //返回指定位置的单元格是否可以移动

- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath;//将指定位置的单元格移动到另一个位置


下面是<UITableViewDelegate>协议中常用的方法

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;    //返回单元格的高度

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section;    //设置指定区域的页眉高度

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section;     //设置指定区域的页脚高度

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;    //单击单元格指定的方法,一般是实现页面的切换

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath;    //当单击导航栏的左侧按钮时,设置单元格的编辑风格,是插入或者删除 

0 0
原创粉丝点击