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


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值