mysql tableveiw与表格_iOS - Table View(表格视图)

使用表格视图

它是用于显示一个垂直滚动的单元格数(通常为可重复使用的单元格)组成的视图。它具有特殊的功能,如页眉,页脚,行和段。

重要的属性

delegate

dataSource

rowHeight

sectionFooterHeight

sectionHeaderHeight

separatorColor

tableHeaderView

tableFooterView

重要的方法

-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath

-(void)deleteRowsAtIndexPaths:(NSArray*)indexPaths

withRowAnimation:(UITableViewRowAnimation)animation

-(id)dequeueReusableCellWithIdentifier:(NSString*)identifier

-(id)dequeueReusableCellWithIdentifier:(NSString*)identifier

forIndexPath:(NSIndexPath*)indexPath

-(void)reloadData

-(void)reloadRowsAtIndexPaths:(NSArray*)indexPaths

withRowAnimation:(UITableViewRowAnimation)animation

-(NSArray*)visibleCells

示例代码和步骤

1. 让加一个 tableview 在 ViewController.xib 如下所示。

61e63404daf36c22ebe380f258dc6483.png

2. 设置委托和数据源文件所有者tableview,通过右击并选择数据源和委托。设置数据源,如下所示。

7a648168c35cf0f8d617e8dd103a3205.png

3. 然后为 tableview 创建一个IBOutlet 命名为 myTableView。这是显示在下面的图像。

0b07df1f281ef567a82d72bc8f5cb095.png

7bf9b2328c0006b49430f51f8c266805.png

4. 然后添加一个NSMutableArray持有表视图中显示的数据。

5. ViewController 应采用 UITableViewDataSource和UITableViewDelegateprotocols。该ViewController.h 看起来应该如下图所示。

#import @interfaceViewController:UIViewController{IBOutletUITableView*myTableView;NSMutableArray*myData;}@end

6. 我们应该实现所需的 tableview 委托和数据源的方法。更新后 ViewController.m 如下

#import "ViewController.h"@interfaceViewController()@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];// table view data is being set heremyData=[[NSMutableArrayalloc]initWithObjects:@"Data 1 in array",@"Data 2 in array",@"Data 3 in array",@"Data 4 in array",@"Data 5 in array",@"Data 5 in array",@"Data 6 in array",@"Data 7 in array",@"Data 8 in array",@"Data 9 in array",nil];// Do any additional setup after loading the view, typically from a nib.}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}#pragmamark-TableViewDatasource-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{return[myData count]/2;}-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{staticNSString*cellIdentifier=@"cellID";UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];if(cell==nil){cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];}NSString*stringForCell;if(indexPath.section==0){stringForCell=[myData objectAtIndex:indexPath.row];}elseif(indexPath.section==1){stringForCell=[myData objectAtIndex:indexPath.row+[myData count]/2];}[cell.textLabel setText:stringForCell];returncell;}// Default is 1 if not implemented-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{return2;}-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{NSString*headerTitle;if(section==0){headerTitle=@"Section 1 Header";}else{headerTitle=@"Section 2 Header";}returnheaderTitle;}-(NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{NSString*footerTitle;if(section==0){footerTitle=@"Section 1 Footer";}else{footerTitle=@"Section 2 Footer";}returnfooterTitle;}#pragmamark-TableViewdelegate-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{[tableView deselectRowAtIndexPath:indexPath animated:YES];UITableViewCell*cell=[tableView cellForRowAtIndexPath:indexPath];NSLog(@"Section:%d Row:%d selected and its data is %@",indexPath.section,indexPath.row,cell.textLabel.text);}@end

7. 现在,当我们运行程序时,我们会得到下面的输出。

e14ff96d418f5be6d71b8273d6f5105e.png

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值