zoukankan      html  css  js  c++  java
  • IOS控件UITableView详解

    首选创建一个新的项目,并添加一个MainViewController的Class文件

    打开MainViewController.h文件

    [cpp] view plaincopy
     
     
          @interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
    1. @property (nonatomic, retain) NSArray *dataList;  
    2. @property (nonatomic, retain) UITableView *myTableView;  
    3. @end  

    TableView的数据源UITableViewDataSource

    TableView的委托UITableViewDelegate

    如果当前类是继承自UIViewController,需要添加上面的代码,如果直接继承自UITableViewController则不需要添加

    然后打MainViewController.m文件,初始化UItableView并显示在当前窗口

    [cpp] view plaincopy
     
    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.     // 初始化tableView的数据  
    5.     NSArray *list = [NSArray arrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津", nil];  
    6.     self.dataList = list;  
    7.       
    8.     UITableView *tableView = [[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain] autorelease];  
    9.     // 设置tableView的数据源  
    10.     tableView.dataSource = self;  
    11.     // 设置tableView的委托  
    12.     tableView.delegate = self;  
    13.     // 设置tableView的背景图  
    14.     tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];  
    15.     self.myTableView = tableView;  
    16.     [self.view addSubview:myTableView];  
    17. }  

    在初始化的时候,可以为TableView设置样式

    第一种:列表 UITableViewStylePlain

    第二种:分组UITableViewStyleGrouped

    创建并设置每行显示的内容

    [cpp] view plaincopy
     
    1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.     static NSString *CellWithIdentifier = @"Cell";  
    4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];  
    5.     if (cell == nil) {  
    6.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];  
    7.     }  
    8.     NSUInteger row = [indexPath row];  
    9.     cell.textLabel.text = [self.dataList objectAtIndex:row];  
    10.     cell.imageView.image = [UIImage imageNamed:@"green.png"];  
    11.     cell.detailTextLabel.text = @"详细信息";  
    12.     return cell;  
    13. }  

    UITableViewCell的样式也是可以进行设置的,如果不能满足项目的需要,可以自己定义UITableViewCell的样式

    UITableViewCellStyleDefault

    UITableViewCellStyleSubtitle

    UITableViewCellStyleValue1

    UITableViewCellStyleValue2

    分组的TableView还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段

    [cpp] view plaincopy
     
    1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
    2. {  
    3.     return 1;  
    4. }  


    设置内容缩进

    [cpp] view plaincopy
     
    1. - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.     return [indexPath row];  
    4. }  

    设置cell的行高

    [cpp] view plaincopy
     
    1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.     return 70;  
    4. }  

    设置cell的隔行换色

    [cpp] view plaincopy
     
    1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.     if ([indexPath row] % 2 == 0) {  
    4.         cell.backgroundColor = [UIColor blueColor];  
    5.     } else {  
    6.         cell.backgroundColor = [UIColor greenColor];  
    7.     }  
    8. }  



    当选择指定的cell时,弹出UIAlertView显示选择的内容

    [cpp] view plaincopy
     
    1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.       
    4.     NSString *msg = [[NSString alloc] initWithFormat:@"你选择的是:%@",[self.dataList objectAtIndex:[indexPath row]]];  
    5.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
    6.     [msg release];  
    7.     [alert show];  
    8. }  



    滑动选择的行后删除

    [cpp] view plaincopy
     
    1. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.     NSLog(@"执行删除操作");  
    4. }  

  • 相关阅读:
    正敲着代码,鼠标坏了!
    DB2 OLAP函数的使用(转)
    修剪矩形
    classpath和环境变量设置(转)
    MyEclipse断点调试JavaScript浅析(转)
    Onunload和onbeforeunload方法的异同
    db2中的coalesce函数(转)
    db2:根据TABLEID找table
    [转]DB2行列转换
    DB2删除数据时的小技巧
  • 原文地址:https://www.cnblogs.com/aggie/p/4518588.html
Copyright © 2011-2022 走看看