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

//
//  ViewController.swift
//  Proclamation
//
//  Created by  on 16/12/15.
//  Copyright © 2016年 . All rights reserved.
//

import UIKit

class 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()
    }


}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...