swift中tableview的使用

这里介绍下tableview的简单使用,大家可以自己在xcode的新建一个项目,然后把下面的代码粘贴到ViewController文件中,也可以把我给的项目导进去

在这里需要 提到cell的重用机制:当你给出的tableview比较低,一次不足以显示完你的所有cell,所以就需要向下拉,但是拉的时候,最上面的cell就消失了。为了提高效率,swift并没有删除和生成cell,而是下面的cell是复用刚才小时的cell

这里还用到了delegate,其实代理的使用步骤都差不多,大家按照步骤走就行了。如果大家需要理解的更深入一些,可以选择代理,然后按住command键,最后点击一下,就可以到类里面去仔细看了



//
//  ViewController.swift
//  tableviewAndCellCode
//
//  Created by wangtuntun on 15/10/8.
//  Copyright (c) 2015年 wangtuntun. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    //继承UITableViewDataSource还有UITableViewDelegate
    //定义tableview以及数据源
    var tableView1:UITableView!
    var tableSource=["1","2","3","4","5","k","k","u","uu","1","2","3","4","5","k","k","u","uu"];
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        println("hello");
        //初始化tableview
        tableView1=UITableView(frame: CGRect(x: 10, y: 10, width: 300, height: 300), style: UITableViewStyle.Grouped);
        tableView1.backgroundColor=UIColor.redColor();
        //这是使用监听必须要做的
        self.tableView1.delegate=self;
        self.tableView1.dataSource=self;
        //这里涉及到cell的重用机制。就是在显示的cell超过tableview时,马上显示的cell重用马上消失的cell
//        self.tableView1.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell");
        // 将tableview添加代视图中
        self.view.addSubview(tableView1);
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // 返回行的个数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return tableSource.count;
    }
    //返回列的个数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }
    //返回一个cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        let cellIdentifier="homePageIdentifier"
        var cell:UITableViewCell!=tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! UITableViewCell!
        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
            println(indexPath.row);
            
            
        }
        cell?.textLabel?.text=self.tableSource[indexPath.row];
        return cell!
        
//        let cell=tableView1.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell;
//        cell.textLabel!.text=self.tableSource[indexPath.row];
//        cell.textLabel?.textColor=UIColor.blueColor();
//        cell.textLabel?.backgroundColor=UIColor.yellowColor();
//        return cell;
    }
    //返回cell的高度
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
        return 45.0
    }
    //当选中某一行的操作
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        println("\(tableSource[indexPath.row])");
    }

    

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值