• swift-UITableView的基本使用


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

    //

    //  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;    //实现数据源

        //去掉表格分割线

        //dataTable.separatorStyle = UITableViewCellSeparatorStyle.None;

            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("个人信息内存警告");

        }

    }

    效果图如下:

  • 相关阅读:
    自定义“浏览文件夹”对话框
    CYABFFW:这是另一个文件夹包装器
    CYABFFW:这是另一个文件夹包装器
    ToDoList样式表:教程
    7.2.23 -一个有效而灵活的方法来掌握你的任务
    使用。net SDK编写位图按钮控件
    在MVC应用程序中使用自动程序进行CRUD操作
    imp
    openpyxl
    fabric
  • 原文地址:https://www.cnblogs.com/brance/p/4915275.html
Copyright © 2020-2023  润新知