TableView详解

最近突然发现,其实每个应用程序,里面的界面都是用TableView形成的,只不过,每个自定义的Cell是不一样的,所以,之前只是粗略的看了一下TableView的相关知识,这次把它重点研究了一下,把其中遇到的一些问题mark一下。
注意:如果是在storyboard中用到TableView进行设计,那就可以直接拖入TableView controller,就可以了,如果是在xib中进行设计,则不能直接拖入controller,需要进行下面的一些连接操作,否则会出现一些问题。
1 在xib中拖入TableView
2 建立相关连接:
(1)关联dataSource和delegate到File’s owner
(2)在File’s owner上右键,view关联到view上面
3在ViewController中添加协议

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>  
@property (nonatomic,strong) NSArray *noteArray;
@end  

4返回table中有几个组.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 1;
}

5返回每个组里有几行

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section 
{
  return [noteArray count];
}

6实现tableview添加数据源,返回TableView的行数,返回各行cell实例。

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

    static NSString *CellIdentifier = @"TableIdentifier";   

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   
                             TableSampleIdentifier];   
    if (cell == nil) {   
        cell = [[UITableViewCell alloc]   
                initWithStyle:UITableViewCellStyleDefault   
                reuseIdentifier:TableIdentifier];   
    }   

    NSUInteger row = [indexPath row];   
    cell.textLabel.text = [self.noteArray objectAtIndex:row];   
    return cell;   
}  

这样就形成了简单的一个TableView界面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值