zoukankan      html  css  js  c++  java
  • iOS 表视图(UITableVIew)的使用方法(1)表视图的示例

      表视图继承自UIScrollView,所以有着大多UIScrollView的操作特性,诸如手指控制内容的滚动,内容视图到顶端或者低端时的自动反弹等。配合UINavigationController的导航特性,表视图可以将大量有一定规则顺序的数据,完整的呈现到客户端上。

      一般,开发者可以将UITableView的datasource和delegate对象设置成同一个控制器对象,delegate的回调函数并非强制实现,如果控制器没有特别实现代理回调函数的话,UITableView将用默认的值代替,比如默认无页眉页脚,默认点击UITableViewCell时不做任何处理等。

      但是datasource却不同,想要正常显示一个UITableView,作为datasource的对象需要强制地实现两个回调方法。

    -(NSInteger)tableView:(UITableViewView *)tableView numberOfRowsInSection:(NSInteger)section
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    表视图的使用最适合的场景时数据列表,我们以一份恒大足球队的球员名单来做示例,数据存储用苹果程序最喜欢的plist文件格式记录。如下图:

    每个球员一共有三个信息:球衣号码,球员名字和角色

    在具体工作开展之前,有必要为这个结构声明一个数据模型,代码如下:

    HBPlayerInfo.h文件

    1 @interface HBPlayerInfo : NSObject
    2 
    3 @property (nonatomic,retain)NSString *name;
    4 @property (nonatomic,retain)NSString *role;
    5 @property (nonatomic,retain)NSNumber *number;
    HBPlayerInfo.m文件
    1
    @implementation HBPlayerInfo 2 3 @synthesize name=_name; 4 @synthesize role=_role; 5 @synthesize number=_number;

    随后在UITableViewControll的子类取名SimpleTabelViewController,对于头文件,声明如下:

    //SimpleTableViewContoller.h
    @interface
    HBSimpleTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate> { //数据源 NSArray *_datasource; } @property (nonatomic ,readonly, retain) NSArray *datasource; //数据源赋值 -(void)initData; //界面配置 -(void)initUI; //UIViewController的标题 -(NSString *)title;

    头文件声明了2个初始化的方法,对于数据源的初始化内容,有必要现将记录所有队员信息的plist加入到工程中去,并且读取出来,撰写如下代码

     1 - (void)viewDidLoad
     2 {
     3     [super viewDidLoad];
     4     
     5     // Uncomment the following line to preserve selection between presentations.
     6     // self.clearsSelectionOnViewWillAppear = NO;
     7     
     8     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     9     // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    10     self.tableView.dataSource=self;
    11     self.tableView.delegate=self;
    12     [self initData];
    13     [self initUI];
    14     
    15     self.navigationItem.title=[self title];
    16 }
    17 
    18 -(void)initData
    19 {
    20     NSMutableArray *arrPlayer=[NSMutableArray arrayWithCapacity:0];
    21     NSArray *arrPlist=nil;
    22     //读取工程中的球员信息plist
    23     arrPlist=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"hengda" ofType:@"plist"]];
    24     
    25     HBPlayerInfo *onePlayer=nil;
    26     //将信息挨个解析到数据模型中
    27     for(NSDictionary *onePlayerInfo in arrPlist)
    28     {
    29         onePlayer=[[HBPlayerInfo alloc]init];
    30         onePlayer.name=[onePlayerInfo objectForKey:@"name"];
    31         onePlayer.role=[onePlayerInfo objectForKey:@"role"];
    32         onePlayer.number=[onePlayerInfo objectForKey:@"number"];
    33         [arrPlayer addObject:onePlayer];
    34     }
    35     
    36     //数据的赋值
    37     if(_datasource!=nil)
    38     {
    39         _datasource=nil;
    40     }
    41     _datasource=[[NSArray alloc]initWithArray:arrPlayer];
    42 }
    43 
    44 -(void)initUI
    45 {
    46     self.tableView.allowsSelection=NO;
    47 }
    48 
    49 -(NSString *)title
    50 {
    51     return @"广州恒大俱乐部";
    52 }
    53 
    54 - (void)didReceiveMemoryWarning
    55 {
    56     [super didReceiveMemoryWarning];
    57     // Dispose of any resources that can be recreated.
    58 }
    59 
    60 #pragma mark - Table view data source
    61 
    62 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    63 {
    64 #warning Potentially incomplete method implementation.
    65     // Return the number of sections.
    66     return 1;
    67 }
    68 
    69 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    70 {
    71 #warning Incomplete method implementation.
    72     // Return the number of rows in the section.
    73     return  self.datasource.count;
    74 }
    75 
    76 
    77 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    78 {
    79     static NSString *CellIdentifier=@"SimpleTableViewCellId";
    80     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    81     
    82     // Configure the cell...
    83     if(cell==nil)
    84     {
    85         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    86     }
    87     
    88     //配置Cell的内容
    89     HBPlayerInfo *onePlayer=[self.datasource objectAtIndex:indexPath.row];
    90     if (onePlayer) {
    91         cell.textLabel.text=onePlayer.name;
    92     }
    93     //提供UITableView需要的Cell
    94     return cell;
    95 }

    注:每个UITableCell都拥有一个用作重用的ID,上述代码中我们取名叫SimpleTableViewCellId.当在滚动的过程中需要显示新一行时,新行会使用不显示的旧行的Cell对象修改内容后再次显示。当既要显示的Cell跑进“cellForRowAtIndexPath”回调函数的时候,“dequeueReusableCellWithIdentifier”方法返回的就是第一个Cell对象而不再需要新建内存,开发者只需要在第一个Cell上进行内容的重新配置后即可

    效果图:

  • 相关阅读:
    《白帽子讲web安全》学习笔记(第一篇)
    pytorch安装配置(VScode +miniconda+pytorch)
    tensorflow-GPU安装配置(VScode +miniconda+tensorflow-gpu)
    Win10+VScode +miniconda安装tensorflow(CPU版本)
    The TensorFlow library wasn't compiled to use SSE instructions解决方法
    python安装配置(miniconda版)(Win10+VScode +miniconda)
    Git使用方法
    PDF快速导出为word(免费方法)
    CAJ文件转PDF
    拖拽的效果 第一步 设置 可拖拽的属性 draggable="true" 绑定drag 事件 第二步 设置 放置位置 触发的事件 dragover 第三步 设置 放置之后 触发的事件 dragover 下面请看代码:
  • 原文地址:https://www.cnblogs.com/haibosoft/p/3668354.html
Copyright © 2011-2022 走看看