iOS开发笔记之UITableView的使用

本文详细介绍了如何初始化UITableView,并提供了隐藏多余cell、实现数据源的必选方法、使用配套方法进行注册cell以及按需定制的数据源方法。适用于iOS开发中的TableView控件使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:初始化UITableView,通过代码或者stroyboard,注意的地方是设置代理和数据源

self.tableView.delegate     = self;

self.tableView.dataSource= self;

二:如果要隐藏多余的cell

slef.tableView.tableFooterView = [[UIView alloc]init]; 

三:实现数据源的两个Required的方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        //如果是从nib中加载
        //cell=[[[NSBundle mainBundle]loadNibNamed:@"PFPersonInfoCell" owner:self options:nil] objectAtIndex:0];
        
    }
    return cell;
}
四:另外还可以使用以下配套方法:  

1.首先在初始化时注册cell

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
或者用nib相应的注册方法
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
2.然后在数据源中可以这么干就OK了(现在继承UITableView的默认就是下面方法了,注意在sb中cel的Indentifier就ok)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:
                           indexPath];
    return cell;
}

五:剩下的就是可以按需求来玩了,方法可以参照文档





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值