IOS 自定义导航栏 和表格 学习笔记

demo 下载地址:http://download.csdn.net/detail/robinson_911/8349659


昨天花了点时间重新学习了下UITableView  和自定义导航栏,写了个demo以备后面查看。

先说昨天遇到的问题,

1.demo写完后,发现表格老是有重叠页面,同时表格的位置不能能够控制。最后发现原来是:

@interface SwwTableViewController :UITableView<UITableViewDelegate,UITableViewDataSource>

SwwTableViewController继承的是UITableView,所以会出现重叠页面,同时UITableView的位置也不能控制。

最后改为继承UIViewController后,就不会出现上面的情况了。

@interface SwwTableViewController :UIViewController<UITableViewDelegate,UITableViewDataSource>


2.自定义有标题栏的UITableView

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView  = [[[UIView alloc] init] autorelease];
    [headView setFrame:CGRectMake(0, 100, 320, 30)];
    headView.backgroundColor = [UIColor whiteColor];
    // headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //边框2
    UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 300, 1)];
    borderView.layer.backgroundColor = [[UIColor grayColor] CGColor];
    borderView.layer.borderWidth = 1.0f;
    [headView addSubview:borderView];
    [borderView release];
    
    
    UILabel *_orderLable = [self _generateHintLabel];
    [_orderLable setFrame:CGRectMake(3, headerHight, 60, 20)];
    [headView addSubview:_orderLable];
    
    UILabel *_productLable = [self _generateHintLabel];
    [_productLable setFrame:CGRectMake(60, headerHight, 60, 20)];
    [headView addSubview:_productLable];
    
    UILabel *_doLable = [self _generateHintLabel];
    [_doLable setFrame:CGRectMake(125, headerHight, 60, 20)];
    [headView addSubview:_doLable];
    
    UILabel *_buyTimeLable = [self _generateHintLabel];
    [_buyTimeLable setFrame:CGRectMake(165, headerHight, 60, 20)];
    [headView addSubview:_buyTimeLable];
    
    UILabel *_buyMoneyLable = [self _generateHintLabel];
    [_buyMoneyLable setFrame:CGRectMake(240, headerHight, 60, 20)];
    [headView addSubview:_buyMoneyLable];
    
    //第一个section的标题栏
    if ( section == 0 )
    {
        _orderLable.text = @"订单号";
        _productLable.text = @"产品名称";
        _doLable.text = @"操作";
        _buyTimeLable.text = @"购买时间";
        _buyMoneyLable.text = @"购买金额";
    }

    return headView;

}


3.自定义导航栏

//方法一  自定义导航栏

    UINavigationBar  *_navigationBar = [[UINavigationBar alloc] init];

    _navigationBar.frame = CGRectMake(0, 20, MAIN_SCREEN_WIDTH, 44);

    _navigationBar.backgroundColor = [UIColor redColor];

    [self.view addSubview:_navigationBar];


    UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 44)];

   // titleText.backgroundColor = [UIColor clearColor];

    titleText.textColor=[UIColor blackColor];

    [titleText setFont:[UIFont systemFontOfSize:14.0]];

    titleText.textAlignment = NSTextAlignmentCenter;

    titleText.text =@"导航栏Demo";

    [_navigationBar addSubview:titleText];

    

    UINavigationItem   *navigationItem = nil;

    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];

    [button setImage:[UIImage imageNamed:@"top_icon_back"] forState:UIControlStateNormal];

    button.backgroundColor = [UIColor clearColor];

    button.frame = CGRectMake(0, 6, 49, 31);

    //[button setTitle:@"返回" forState:UIControlStateNormal];

    [_navigationBar addSubview:button];


    //方法二  自定义导航栏
    UIButton *setingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    setingBtn.frame = CGRectMake(0, 6, 49, 31);
    [setingBtn setBackgroundImage:[UIImage imageNamed:@"top_icon_back"]
                         forState:UIControlStateNormal];
    setingBtn.backgroundColor = [UIColor clearColor];
    
    //创建一个导航栏
    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
    navigationBar.backgroundColor = [UIColor redColor];
    
    //创建一个导航栏集合
    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];
    
    
    //创建一个左边按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回"
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(clickLeftButton)];
    
    //创建一个右边按钮
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"下一页"
                                                                    style:UIBarButtonItemStyleDone
                                                                   target:self
                                                                   action:@selector(clickRightButton)];
    //设置导航栏内容
    [navigationItem setTitle:@"导航栏Demo"];
    
    
    //1 .把导航栏集合添加入导航栏中,设置动画关闭
    // [navigationBar pushNavigationItem:navigationItem animated:NO];
    
    //2.把导航栏集合添加入导航栏中
    [navigationBar setItems: [NSArray arrayWithObject: navigationItem]];
    
    //把左右两个按钮添加入导航栏集合中
    // [navigationItem setLeftBarButtonItem:leftButton];
    navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:setingBtn] autorelease];

    [navigationItem setRightBarButtonItem:rightButton];
    
    //把导航栏添加到视图中
    [self.view addSubview:navigationBar];
    
    
    //释放对象
    [navigationItem release];
    [leftButton release];
    [rightButton release];

    //方法三  自定义导航栏
    
    UIButton *setingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    setingBtn.frame = CGRectMake(200, 6, 49, 31);
    [setingBtn setBackgroundImage:[UIImage imageNamed:@"top_icon_back"]
                         forState:UIControlStateNormal];
    setingBtn.backgroundColor = [UIColor clearColor];

    
    UIToolbar *myToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 20, MAIN_SCREEN_WIDTH, 44)];
    [self.view addSubview:myToolBar];
    
    UIBarButtonItem *bn1 = [[UIBarButtonItem alloc]
                            initWithTitle:@"OK"
                            style:UIBarButtonItemStylePlain
                            target:nil
                            action:nil] ;


    UIBarButtonItem *bn3 = [[UIBarButtonItem alloc]initWithCustomView :setingBtn];
    
    [myToolBar setItems:[NSArray arrayWithObjects:bn1,bn3, nil]];
    UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 44)];
    // titleText.backgroundColor = [UIColor clearColor];
    titleText.textColor=[UIColor blackColor];
    [titleText setFont:[UIFont systemFontOfSize:14.0]];
    titleText.textAlignment = NSTextAlignmentCenter;
    titleText.text =@"导航栏Demo";
    
    [myToolBar addSubview:titleText];



4.上demo图

IOS 自定义导航栏 和表格 学习笔记_第1张图片

你可能感兴趣的:(IOS 自定义导航栏 和表格 学习笔记)