·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> swift-UITableView的基本使用

swift-UITableView的基本使用

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

废话不多说了,直接贴我今天写的代码吧:如果新手有什么不懂的,可以发我邮箱。

//

//  singleInfo.swift            个人信息

//  Housekeeper

//

//  Created by 卢洋 on 15/10/27.

//  Copyright © 2015年 奈文摩尔. All rights reserved.

//

 

import Foundation

import UIKit

class singleInfo:UIViewController,UITableViewDataSource,UITableViewDelegate{

    var dataTable:UITableView!;                                             //数据表格

    var itemString=["昵称","账号","性别","地区","我的爱车"]

 //当前屏幕对象

  var screenObject=UIScreen.mainScreen().bounds;

    

    //页面初始化

    override func viewDidLoad() {

        super.viewDidLoad();

        initView();

    }

    /**

    UI 初始化

    */

    func initView(){

        self.title="我的资料";

        self.view.backgroundColor=UIColor.linghtGreyBg();

        creatTable();

    }

    /**

    我的资料表格初始化

    */

    func creatTable(){

        let dataTableW:CGFloat=screenObject.width;   

        let dataTableH:CGFloat=screenObject.height;

        let dataTableX:CGFloat=0;

        let dataTableY:CGFloat=0;

        dataTable=UITableView(frame: CGRectMake(dataTableX, dataTableY, dataTableW, dataTableH),style:UITableViewStyle.Grouped);

        dataTable.delegate=self;      //实现代理

        dataTable.dataSource=self;    //实现数据源

        self.view.addSubview(dataTable);

    }

    //1.1默认返回一组

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 2;

    }

    

    // 1.2 返回行数

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if(section == 0){

            return 1;

        }else{

            return 5;

        }

    }

    

    //1.3 返回行高

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{

        

        if(indexPath.section == 0){

            return 80;

        }else{

            return 55;

        

        }

    }

    

    //1.4每组的头部高度

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        return 10;

    }

    

    //1.5每组的底部高度

   func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

        return 1;

    }

    //1.6 返回数据源

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let identifier="identtifier";

        var cell=tableView.dequeueReusableCellWithIdentifier(identifier);

        if(cell == nil){

            cell=UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: identifier);

        }

        

        if(indexPath.section == 0){

            cell?.textLabel?.text="头像";

        }else{

            cell?.textLabel?.text=itemString[indexPath.row];

        }

        cell?.accessoryType=UITableViewCellAccessoryType.DisclosureIndicator;

        return cell!;

    }

//1.7 表格点击事件

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        //取消选中的样式

        tableView.deselectRowAtIndexPath(indexPath, animated: true);

   //获取点击的行索引

        if(indexPath.row == 0){

            let pushSingleInfo=singleInfo();

            pushSingleInfo.hidesBottomBarWhenPushed=true;    //隐藏导航栏

            self.navigationController?.pushViewController(pushSingleInfo, animated: true);

        }

    }

 

    //内存警告

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning();

        PRint("个人信息内存警告");

    }

}

效果图如下: