tableView的基本使用(改良版)

本文介绍了一个简单的iOS应用中UITableView的实现过程,包括初始化设置、数据源和代理方法等关键步骤。

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

{

    int i;//用来计算接受通知的次数

}

@property(nonatomic,strong)UITableView * tableView;

@end

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

     self.navigationController.navigationBarHidden = YES;

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(通知要执行的方法) name:@"有新消息了" object:nil];

     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 屏幕宽度, 屏幕高度 - 49) style:UITableViewStyleGrouped];

    self.tableView.bounces = NO;

    self.tableView.backgroundColor = 灰色;

    [self.view addSubview:self.tableView];

 

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.tableView.sectionFooterHeight = 0;

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];//在这里注册一下,数据源方法中就不用加判断语句了,否则数据源方法得加上if(cell == nil){创建新的cell};

}

 

#pragma mark - UITableViewDataSource

 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

 

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

{

    return 1;

    

}

 

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

{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

    //-----为cell添加数据----

    cell.xxx = @"xxxxx";.....

    return cell;

}

 

 

#pragma mark UITableViewDelegate

 

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 0) {

        return 300 * 屏幕宽度/375.0 + 20;

    }

    return 0;

}

 

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

{

    if (条件一) {

    }else{

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值