UITableViewController

来源:互联网 发布:平安普惠绑定网络账号 编辑:程序博客网 时间:2024/05/16 04:24

UITableViewController是IOS开发中一个比较重要的试图控制器,是集成了UITableView视图的控制器,在实际开发中经常用到,功能非常强大,可定制性也很高,下面从简单的使用和自定义Cell以及事件响应等方面来使用。


1.首先创建一个Single View Project,命名为UITableViewControllerTest。打开ViewController.xib并拖入一个UITableView,选中该视图,并点击Connections inspector,分别将delegate和dataSource连线到File's owner


2.在AppDelegate.h中添加属性

@property (strongnonatomicIBOutlet UITabBarController * rootViewController;

并在.m文件中添加合成器

@synthesize rootViewController;


然后在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加该TableView为subView


3.在ViewController.h文件中添加协议UITableViewDelegate,UITableViewDataSource,然后在.m文件中实现协议中的方法

#pragma mark -

#pragma mark Table View Data Source Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [self.listDatas count];

}


上面的方法是返回Table中的行数


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

{

    static NSString * SimpleTableViewIdentifier =@"SimpleTableViewIdentifier";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableViewIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier];

    }

    UIImage * img = [UIImage imageNamed:@"singleicon"];

    cell.imageView.image = img;

    NSUInteger row = [indexPath row];

    cell.textLabel.text = [listDatas objectAtIndex:row];

    

    return cell;

}


上面的方法是构造每一个cell,也就是每一行,SimpleTableViewIdentifier是标记,dequeueReusableCellWithIdentifier方法是当有可重用的cell时,根据标记直接取,而不用重新生成,可以提高效率。

NSIndexPath封装了有关行和分区的信息


UIImage * img = [UIImage imageNamed:@"singleicon"];

cell.imageView.image = img;

NSUInteger row = [indexPath row];

cell.textLabel.text = [listDatas objectAtIndex:row];

这段代码分别设置了行的图片和文字,另外,通过accessoryType属性可以设置行的附属属性,有几种样式可以提供选择。也可以自定义附属视图:cell.accessoryView = [[myView alloc] init];


4.在viewDidLoad方法中添加NSArray后,填入数据,然后run,可以看打运行结果


在这里我使用了TabBarController来承载三种类型的tableview,分别是Plain、Group以及Custom自定义的


5.下面看第二种样式,分组的形式

使用分组的话还要添加一个方法,返回一共有多少组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView


另外可以设置分组标题

//返回组的名称

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    if ([keys count] == 0) {

        returnnil;

    }

    NSString * key = [keys objectAtIndex:section];

    return key;

}



//设置行的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return60;

}


#pragma mark Table Delegate Methods

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSUInteger row = [indexPath row];

    NSString * message = [[NSString alloc]initWithFormat:@"You selected is %@!",[listDatas objectAtIndex:row]];

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Information" message:message delegate:nilcancelButtonTitle:@"Confirm" otherButtonTitles:nil,nil];

    [alert show];

}

上面这个方法是单击行的方法,点击一行后弹出一个对话框UIAlertView



6.最后是自定义cell


首先拖一个UITableViewCell到.xib文件中,并拖入一些视图

然后在.h文件中声明属性

@property(nonatomic,retain)IBOutlet UITableViewCell * tableCell;

@property(nonatomic,retain)IBOutlet UILabel * name;

@property(nonatomic,retain)IBOutlet UILabel * color;

@property(nonatomic,retain)IBOutlet UIImageView * imageView;


7.在.m文件中添加合成器并实现如下方法:

#pragma mark Table Data Source Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [computers count];

}


/*

 自定义表格单元格

 */

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

{

    static NSString * CustomTableViewIdentifier =@"CustomTableViewIdentifier";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CustomTableViewIdentifier];

    if (cell == nil) {

        //加载自定义表格单元格nib文件

        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];

        if ([nib count] > 0) {

            //tableCell已与自定义cell相关联

            cell = self.tableCell;

        }

    }

    

    NSUInteger row = [indexPath row];

    NSDictionary * rowData = [computers objectAtIndex:row];

    self.name.text = [rowData objectForKey:@"Name"];

    self.color.text = [rowData objectForKey:@"Color"];

    UIImage * img = [UIImage imageNamed:@"doubleicon"];

    imageView.image = img;

    //cell.imageView.image = img;

    return cell;

}


8.最后编译运行,结果如下图:


原创粉丝点击