UITableView的基础部分

UITableView的基础部分内容

UITableView的基本设置

#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()
//签订协议
<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *arr;
@end

@implementation MainViewController
-(void)dealloc{
    [_arr release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    [tableView release];
//    设置行高
    tableView.rowHeight = 100;   
//    tableView的两套代理方法,
//    设置第一套协议的代理人
    tableView.dataSource = self;
//    delegate设置代理人
    tableView.delegate = self;    
}
#pragma mark tableview第一个必须实现的协议方法,指定分区内有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//现执行设置分区的方法,后执行每个分区有多少行
        if (section % 2 == 0) {
            return 10;
        }
        if (section % 2 != 0) {
            return 5;
        }
    return self.arr.count;
}
#pragma mark 第二个必须实现的协议方法,主要是用来显示数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    创建cell
//    static特点
//    1.只初始化一次
//    2.如果没有初始值,默认是零
//    3.直到程序结束,才会消失
//    当cell显示结束之后,会把cell统一的放到重用池中,等需要cell显示了,,先从重用池中先找,看有没有限制的cell,如果有的话就用闲置的cell,如火没有的话再创建
//    cell的重用目的是为了节约创建成本,用有限的cell把所有的数据都显示出来
//    给重用池先设置一个重用的标志,根据这个标志可以找到到对应的重用池
    static NSString *reuse = @"reuse";
//    tableview通过重用标志在重用池中寻找cell如果有闲置的cell,cell会保存一个有效的cell对象地址,如果没有,cell里面则是nil,空
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuse];
//    如果没有cell则创建cell
    if (!cell) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse]autorelease];
    }
//    对cell进行赋值
//    cell里有默认的三个控件   
    cell.textLabel.text = self.arr[indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",indexPath.section];
    cell.imageView.image = [UIImage imageNamed:@"456.jpg"];   
//    NSLog(@"%ld",indexPath.row);
//    indexPath里面保存了两个属性 row和section section是当前所在的区域 
//    indexPath.row保存的是行数,从0开始    
    return cell;
}
//这个方法的作用是是设置区域标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"水浒";
}
#pragma mark tableview里有多少个section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 10;
}
//这个方法是用来显示右侧的选项条的
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
}
#pragma mark tableview的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"section:%ld, row:%ld",indexPath.section, indexPath.row);
//   打印当前点击的人名叫什么
    NSLog(@"%@",self.arr[indexPath.row]);
//   实现跳转
    SecondViewController *secVC = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值