iOS:UITableView 使用(一)--基本使用

本文介绍了UITableView的基本使用,包括代码和XIB方式创建,以及sectioned数据源的处理。详细讲解了如何定制UITableViewCell,包括自定义cell的subviews、派生新的cell类以及自定义accessoryView。同时,还探讨了UITableView的性能优化技巧,如重用cell、避免content的重新布局、使用不透明subView等。
摘要由CSDN通过智能技术生成

===================================================

========================使用========================

参考:--developer:tableview Sample Code(TableViewSuite);

------------------------最基本------------

1.创建tableview:     

    _tableView  = [[UITableView alloc]initWithFrame:self.view.bounds];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];

      注意:如果控制器是继承UITableViewController,则以上代码不用写,它本身自带tableview,并且相关配置也可好,直接重写相关代理方法即可。

2.实现相关代理方法

      代码方式

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return [self.xxArray count];   //default numOfSection is one
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     
    static NSString *simpleTableIdentifier = @"simpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.textLabel.text = [self.xxArray objectAtIndex:indexPath.row];
    return cell;
}

        xib方式

如果tableview是在xib中创建,则可以它里面add一个tableViewCell(创建了一个Prototype Cells),则cellForRowAtIndexPath可以简单点写():

       注意cell的identify一定要对应上

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *MyIdentifier = @"MyIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	// Set up the cell.
	cell.textLabel.text = [self.xxArray objectAtIndex:indexPath.row];
	return cell;
}

------注意

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值