用swift实现tableview的展示

释放双眼,带上耳机,听听看~!
import UIKit

class ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {




    var baby = ["测试0","测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10","测试11"]

    var tableView = UITableView()

    var isFlag = [Bool](count :12, repeatedValue:false)

    override func viewDidLoad() {

        super.viewDidLoad()

        tableView = UITableView(frame: CGRectMake(0,0,320, 600), style:UITableViewStyle.Plain)

        tableView.dataSource =self

        tableView.delegate =self

        self.view.addSubview(tableView)

    }

    func numberOfSectionsInTableView(tableView:UITableView) ->Int {

        return 1

    }

   
//每一块有多少行

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

        return baby.count

    }

    //绘制cell

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

        let initIdentifier = "Cell"

        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: initIdentifier)

        //下面两个属性对应subtitle

        cell.textLabel?.text =baby[indexPath.row]

        cell.detailTextLabel?.text ="测试(indexPath.row)"

        cell.imageView!.layer.cornerRadius =40

        cell.imageView!.layer.masksToBounds =true

        //添加附件

        cell.accessoryType =UITableViewCellAccessoryType.DetailButton

        if isFlag[indexPath.row] {

            cell.accessoryType =UITableViewCellAccessoryType.Checkmark

        }else{

            cell.accessoryType =UITableViewCellAccessoryType.None

        }

        return cell

    }

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

       
//使用闭包,和嵌套函数或者JAVA中的匿名类类似

        let locationActionHandler = {(action: UIAlertAction!) -> Void in

            let locationAlertController = UIAlertController(title: nil, message: "我是测试(indexPath.row)", preferredStyle:UIAlertControllerStyle.Alert)

            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)

            locationAlertController.addAction(okAction)

            self.presentViewController(locationAlertController, animated:true, completion:nil)

        }

        let alertController = UIAlertController(title: "测试(indexPath.row)",message: nil, preferredStyle:
UIAlertControllerStyle.ActionSheet)

        let cancleAction = UIAlertAction(title: "Cancle", style: UIAlertActionStyle.Cancel, handler: nil)

        alertController.addAction(cancleAction)

        let locationAction = UIAlertAction(title: "测试是几号", style:UIAlertActionStyle.Default,
handler: locationActionHandler)

        alertController.addAction(locationAction)

        let markAction = UIAlertAction(title: "标记测试一下", style:UIAlertActionStyle.Default,handler: {(action:UIAlertAction) -> Void in

            let cell = tableView.cellForRowAtIndexPath(indexPath)

            cell?.accessoryType =UITableViewCellAccessoryType.Checkmark

            self.isFlag[indexPath.row]= true//然后每次加载时候在cellForRowAtIndexPath方法进行判断

        })

        alertController.addAction(markAction)

        presentViewController(alertController, animated:true, completion:nil)

    }

    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath:
NSIndexPath) -> [UITableViewRowAction]? {

        let shareAction =
UITableViewRowAction(style: UITableViewRowActionStyle.Default, title:"Share", handler: {

            (action: UITableViewRowAction,indexPath:NSIndexPath) ->
Voidin

            let menu = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

            let cancelAction = UIAlertAction(title: "Cancle", style: UIAlertActionStyle.Cancel, handler:
nil)

            let facebookAction =
UIAlertAction(title: "facebook", style:UIAlertActionStyle.Default, handler:nil)

            let twitterAction =
UIAlertAction(title: "twitter", style:
UIAlertActionStyle.Default, handler:
nil)

            menu.addAction(facebookAction)

            menu.addAction(twitterAction)

            menu.addAction(cancelAction)

            self.presentViewController(menu, animated:true, completion:
nil)

        })

        let deleteAction =
UITableViewRowAction(style: UITableViewRowActionStyle.Default, title:"Delete", handler: {

            (action: UITableViewRowAction,indexPath:NSIndexPath) ->
Voidin

            self.baby.removeAtIndex(indexPath.row)

            self.isFlag.removeAtIndex(indexPath.row)

            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation:UITableViewRowAnimation.Left)

        })

        return [shareAction,deleteAction]

    }

    //每个cell的高度

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

        return 80

    }

    //隐藏bar

    override func prefersStatusBarHidden() ->Bool {

        return
true

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }




}


 

人已赞赏
iOS文章

iOS获取设备的uuid 将uuid中的“-”去掉

2020-5-1 7:47:13

iOS文章

iOS中的持久化数据库基本操作

2020-5-1 9:01:58

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索