码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UITableView一些基础知识

时间:2015-03-05 10:55:53      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:ios   delegate   uitableview   

打开UIViewController.h


//

//  RootViewController.h

//  Lesson09TableView

//

//  Created by Dubai on 14-9-26.

//  Copyright (c) 2014 Dubai All rights reserved.

//


#import <UIKit/UIKit.h>

//遵循一下代理

@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>


@end


打开 UIViewController.m为:


//

//  RootViewController.m

//  Lesson09TableView

//

//  Created by Dubai on 14-9-26.

//  Copyright (c) 2014 Dubai All rights reserved.

//


#import "RootViewController.h"


@interface RootViewController ()


@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];

    

    [self.view addSubview:tableView];

    [tableView release];

    

    //属性

    

    tableView.rowHeight = 90;//行高

    tableView.separatorColor = [UIColor redColor];//行隔性颜色

    //tableView.separatorStyle = UITableViewScrollPositionNone;//分割线 消失

    //tableView.separatorStyle = UITableViewScrollPositionBottom;

    //tableView.separatorStyle = UITableViewRowAnimationRight;

    

    

    tableView.dataSource = self;//设置数据源的代理(必须实现)

    tableView.delegate = self;//负责控制的代理对象

    

    

}



//设置分区,可选实现,因为tableview默认有一个分区(数据源代理)


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 4;


}


//设置每个分区的行数,必须实现(数据源)

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

{

    return 5;

    

}

//设置每行要显示的内容,每行所在位置会放值一个tabelViewcell,每行要显示的数据,设置在cell上必须实现


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


{

    

    //当某行移出屏幕时,tableView会将这行显示的cell,移动到重用集合中储存,这行就咩有cell显示任何数据.

    //因此,只要某行要进入屏幕显示,必须执行这个代理方法.设置这行要显示的cell;

    NSLog(@"row= %ld",indexPath.row);

    

    

  //indexPath包含两个属性 section row 即分区和行数

    //section row 的索引都从0开始.每个分区中的row的索引都是从0开始

    

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];

    //cell的样式 是用来影响3个视图的位置

    

    //cell.imageView.image = [UIImage imageNamed:@"ha.png"];

    

    //cell.detailTextLabel  直接用 不用创建(不能改变大小)

    cell.textLabel.text = [NSString stringWithFormat:@" section:%ld row:%ld",indexPath.section,indexPath.row];//在第几个分区 第几行

    cell.detailTextLabel.text = @". . . .";//当是default时不显示(可以修改cell样式 来显示)

    

    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//辅助视图

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    

    //只让第一行显示checkmark

    if (indexPath.row == 0) {

        //如果是第一行就显示为checkmark

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    }else{

    

        cell.accessoryType = UITableViewCellAccessoryNone;

    

    

    }

    

    

    

    return cell;

    

    

    

//    //重用

//    

//    //先从重用队列中获取可以被重用的cell对象,

//    

//    static NSString *indentifier = @"cell";//标示

//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];

//    //如果重用队列中没有可以使用的cell,必须自己创建.

//    if (cell == nil) {

//        cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:indentifier] autorelease];

//        NSLog(@"创建的新的cell对象");//只许创建对象

//        

////        cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];

//        

//    }

//    //重用在这里

//    //设置当前要使用的cell,可能放置在任何一行

//    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];

//    //cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.section];

//    cell.detailTextLabel.text = [NSString stringWithFormat:@"nihao"];//显示不显示跟上面的cell创建时的subtitle有关

//    return cell;

}


//给每个分区设置头部标题

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

{



    return [NSString stringWithFormat:@"%ld",section + 1];




}


//给所有的副标题右侧的.


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{


    return @[@"1",@"2",@"3"];



}


////设置行高

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

//{

//    if (indexPath.section == 0) {

//        return 20;

//    }if (indexPath.section == 1) {

//        return 60;

//    }if (indexPath.section == 2) {

//        return 40;

//    }if (indexPath.section == 3) {

//        return 100;

//    }return 2;

//

//

//}

//

//检测cell被选中的第几个分区第几行


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

{


    NSLog(@"分区   %ld,行数:%ld",indexPath.section+1,indexPath.row );




}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


如图:

技术分享
一些UItableView的复杂用处与难点以后我会给大家整理一下!

iOS UITableView一些基础知识

标签:ios   delegate   uitableview   

原文地址:http://blog.csdn.net/zhaoguodongios/article/details/44077823

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!