UITableView基础(一)

来源:互联网 发布:html商城源码下载 编辑:程序博客网 时间:2024/06/06 03:49
 hello 大家又见面啦,今天小编给大家讲解一下UITableView,这一块知识在我们实际项目中是最为常见的,是ios项目的根骨所在,希望大家能够足够重视,所以小编准备分几次内容给大家一一解析其中的难点,下面是最基础的内容,希望大家能有所收获。
import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITableViewDataSource>//绑定tableView控件,定义变量,方便对控件的代码操作@property (strong, nonatomic) IBOutlet UITableView *table;//为tableView上显示数据定义两个变量@property(nonatomic,strong)NSArray *community;@property(nonatomic,strong)NSArray *details;@end

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize details;@synthesize community;- (void)viewDidLoad {    [super viewDidLoad];    //对定义的两个变量赋予初始值   community=[NSArray arrayWithObjects:@"图灵工作社",@"梦翔工作社",@"南工电脑网",@"锋芒工作社", nil];    details=[NSArray arrayWithObjects:@"图灵最牛,挤压群熊",@"不错的社团",@"不错的社团",@"不错的社团", nil];    self.table.dataSource=self;    //添加页眉和页脚,在这里小编分别加载的是图片    self.table.tableHeaderView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"001.jpg"]];    self.table.tableFooterView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"001.jpg"]];}//UITableView协议里面的方法,对每个UITableCell进行定制和写入内容-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  static NSString *cellId=@"cellId";//静态NSString类型,目的是可以重用能添加UITaleCell到重用表格中,和方便从重用表格中调取。    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];    if (cell==nil) {//第一次没有表格行(UITableCell)时,定制表格行        switch (indexPath.row%4) {            case 0:                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];//第一种UITableCell风格                break;           case 1:                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];//第二种UITableCell风格                break;            case 2:                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];//第三种UITableCell风格                break;                case 3:                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellId];//第四种UITableCell风格                break;      <span style="color:#ff0000;">//注意看下下边的图片,比较这四种UITableCell的不同点!!!</span>                }        }        NSUInteger rowNo=indexPath.row;//调取每行UITableCell的索引进行付于初值    cell.textLabel.text=[community objectAtIndex:rowNo];    cell.imageView.image=[UIImage imageNamed:@"001.jpg"];    cell.imageView.highlightedImage=[UIImage imageNamed:@"001.jpg"];    cell.detailTextLabel.text=[details objectAtIndex:rowNo];    return cell;}//UIDataSource协议里面的第二个方法:目的告诉系统一个分区里面有多少个表格行(注意是一个分区,不一定是总共有多少表格行)-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return community.count;}//UIDataSource协议里面的第二个方法:目的告诉系统有几个分区(几块表格)显然这里返回的是一个分区表格-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
运行结果如下:

怎么样,这点简单的知识掌握了没有啊?大家要愉快的学习,发觉编程的乐趣,提高自己的效率!后续马上给大家讲解深层次的UITableView知识,希望小编的这点微薄知识能帮到你们!微笑再见再见再见


1 0
原创粉丝点击