加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:33:16 所属栏目:百科 来源:网络整理
导读://// ViewController.swift// Proclamation//// Created by on 16/12/15.// Copyright ? 2016年 . All rights reserved.//import UIKitclass ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { var tableView = 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()
    }


}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读