UITableView(一)

来源:互联网 发布:js如何将一个div隐藏 编辑:程序博客网 时间:2024/06/06 09:08

1.创建UITableViewController要继承

<UITableViewDataSource,UITableViewDelegate>


2.下方是UITableViewController.m代码

- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.    self.view.backgroundColor = [UIColor redColor];        UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, 320, self.view.bounds.size.height-20) style:UITableViewStylePlain];        tableView.dataSource = self;    tableView.delegate =self;                tableView.separatorColor = [UIColor greenColor];    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);    tableView.rowHeight = 60;    [self.view addSubview:tableView];    [tableView release];        }-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%s,%d",__FUNCTION__,__LINE__);    //第一区高50  第二区高70    //第三区高40  第四区高100    if (indexPath.section == 0 ) {        return 50;    }else if (indexPath.section == 1)    {        return 70;    }else if (indexPath.section == 2)    {        return 40;    }    return 100;}-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{        return 100;}-(CGFloat)tableView:(UITableView *)tableView heightForFootInSection:(NSInteger)section{    return 40;}//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section//{//    UIView * one = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];//    one.backgroundColor = [UIColor yellowColor];//    [self.view addSubview:one];//    [one release];//    return one;//}//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section//{//    UIView * one = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 100, 100)];//    one.backgroundColor = [UIColor greenColor];//    [self.view addSubview:one];//    [one release];//    return one;//}//设置多少个分区 可选实现,默认为一-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    NSLog(@"333333%s,%d",__FUNCTION__,__LINE__);    return 4;}//必须实现,设置所有分区的行数-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    NSLog(@"%s,%d",__FUNCTION__,__LINE__);    NSLog( @"section = %d",section);    if (section<2) {        return 3;    }        return 5;}//必须实现,设置每行要显示的内容-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"zzzzz%s,%d",__FUNCTION__,__LINE__);    //tableView 中有一个NSSet,用来管理所有等待重用的cell对象    //定义cell的唯一标识    static NSString * cellIndetifier = @"cell";            //tableView通过cell的标识从NSSet(重用队列)中获取cell对象    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];    if (!cell) {        //如果不存在,创建cell对象        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifier]autorelease];        NSLog(@"*******section = %d*******row = %d*****",indexPath.section,indexPath.row);    }//    indexPath.section 第几个分区//    indexPath.row   第几行    NSLog(@"w=%d,%d",indexPath.section,indexPath.row);        cell.textLabel.text = [NSString stringWithFormat:@"第%d个分区,第%d行",indexPath.section,indexPath.row];            return cell;}//分区索引-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    return @[@"0",@"1",@"2",@"3"];}-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    return [NSString stringWithFormat:@"第%d区的尾",section];}//每个索引-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return [NSString stringWithFormat:@"第%d区头",section];}


使用以下代码会显示以下图片:

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section


使用以下代码会显示以下图片:

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

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



什么都不使用


0 0
原创粉丝点击