Swift UITableView 插入及自动滚动到底部

Swift UITableView 插入及自动滚动到底部,第1张

概述//// ViewController.swift// Proclamation//// Created by on 16/12/15.// Copyright © 2016年 . All rights reserved.//import UIKitclass ViewController: UIViewController,UITableViewDelegate,
////  VIEwController.swift//  Proclamation////  Created by  on 16/12/15.//  copyright © 2016年 . All rights reserved.//import UIKitclass VIEwController: UIVIEwController,UItableVIEwDelegate,UItableVIEwDataSource {            var tableVIEw = UItableVIEw()            var rightbuttonItem:UIbarbuttonItem?        var items = ["1","2","3","4","5","6","7","8","9"]    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()                initVIEw()                setupRightbarbuttonItem()                // Do any additional setup after loading the vIEw,typically from a nib.    }    overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()        // dispose of any resources that can be recreated.    }            func initVIEw(){        // 初始化tableVIEw的数据        self.tableVIEw=UItableVIEw(frame:self.vIEw.frame,style:UItableVIEwStyle.Plain)        // 设置tableVIEw的数据源        self.tableVIEw.dataSource=self        // 设置tableVIEw的委托        self.tableVIEw.delegate = self                self.tableVIEw.registerClass(UItableVIEwCell.self,forCellReuseIDentifIEr: "cell")                self.vIEw.addSubvIEw(self.tableVIEw)                    }        //加右边按钮    func setupRightbarbuttonItem()    {        self.rightbuttonItem = UIbarbuttonItem(Title: "Add",style: UIbarbuttonItemStyle.Plain,target: self,action: #selector(VIEwController.rightbarbuttonItemClicked))        self.navigationItem.rightbarbuttonItem = self.rightbuttonItem            }    //增加事件    func rightbarbuttonItemClicked()    {                let row = self.items.count        let indexPath = NSIndexPath(forRow:row,inSection: 0)        self.items.append("\(row + 1)")        self.tableVIEw.insertRowsAtIndexPaths([indexPath],withRowAnimation: .None)                        // scroll to bottom        self.tableVIEw.scrollToRowAtIndexPath(NSIndexPath(forRow: self.items.count - 1,inSection: 0),atScrollposition: .Bottom,animated: true)    }        //总行数    func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int{        return self.items.count    }        //加载数据    func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell{                let cell = tableVIEw .dequeueReusableCellWithIDentifIEr("cell",forIndexPath: indexPath) as UItableVIEwCell        let row=indexPath.row as Int        cell.textLabel!.text=self.items[row]        cell.imageVIEw!.image = UIImage(named:"speaker")        return cell;            }            //删除一行    func tableVIEw(tableVIEw: UItableVIEw,commitEditingStyle editingStyle: UItableVIEwCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath){        let index=indexPath.row as Int        self.items.removeAtIndex(index)        self.tableVIEw.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UItableVIEwRowAnimation.top)        NSLog("删除\(indexPath.row)")    }    //选择一行    func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath){        let alert = UIAlertVIEw()        alert.Title = "提示"        alert.message = "你选择的是\(self.items[indexPath.row])"        alert.addbuttonWithTitle("Ok")        alert.show()    }}
总结

以上是内存溢出为你收集整理的Swift UITableView 插入及自动滚动底部全部内容,希望文章能够帮你解决Swift UITableView 插入及自动滚动到底部所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1068590.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存