UITableView(一)

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



什么都不使用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值