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

Swfit UITableView的使用(六)

发布时间:2020-12-15 20:05:58 所属栏目:百科 来源:网络整理
导读:详细请移步github https://github.com/Zhangjingwang1993/Swift.git var tableViewself = UITableView() var arrayData = [ "cell1" , "cell2" , "cell3" , "cell4" , "cell5" ] var buttonRight = UIButton() 创建TableView func createTableview(){ tableV

详细请移步github https://github.com/Zhangjingwang1993/Swift.git

var tableViewself = UITableView()
var arrayData = ["cell1","cell2","cell3","cell4","cell5"]
var buttonRight = UIButton()

创建TableView

func createTableview(){
        tableViewself = UITableView.init(frame: self.view.bounds,style: UITableViewStyle.Plain)
        self.view.addSubview(tableViewself)
        tableViewself.delegate = self
        tableViewself.dataSource = self
        // 注册
        tableViewself.registerClass(TableViewCell.self,forCellReuseIdentifier: "cellId")
    }

Tableview Delegate

func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return arrayData.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int{
        return 1;
 }

func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = TableViewCell.init(style: UITableViewCellStyle.Default,reuseIdentifier: "cellId")
        cell.titleLabel?.text = "自定义cell"
        return cell
  }
    // 删除一行
func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath){
        let index = indexPath.row as Int
        arrayData.removeAtIndex(index)
        tableViewself.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Top)

    }
    // 选择一行
func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let alter = UIAlertView()
        alter.title = "提示"
        alter.message = "您选择的是:(arrayData[indexPath.row])"
        alter.addButtonWithTitle("OK")
        alter.show()
    }
// 加右边的按钮
func createRightBarButtonItem() {
        buttonRight = UIButton.init(type: UIButtonType.Custom)
        buttonRight.frame = CGRectMake(0,0,40,40)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: buttonRight)
        buttonRight.setTitle("Edit",forState: UIControlState.Normal)
        buttonRight.addTarget(self,action: Selector("jion:"),forControlEvents: UIControlEvents.TouchUpInside)

    }
func jion(sender: UIButton){
        sender.selected = !sender.selected

        if sender.selected == true
        {
            tableViewself .setEditing(true,animated: true)
        }else{
            tableViewself .setEditing(false,animated: true)
        }
    }

自定义cell

// 定义
var titleLabel:UILabel?
// 初始化
override init(style: UITableViewCellStyle,reuseIdentifier: String?) {

        super.init(style: UITableViewCellStyle.Default,reuseIdentifier: reuseIdentifier)
        self.creteCell()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
// 创建
func creteCell(){
        titleLabel = UILabel.init(frame: CGRectMake(0,300,20))
        titleLabel?.backgroundColor = UIColor.cyanColor()
        self.addSubview(titleLabel!)
    }

(编辑:李大同)

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

    推荐文章
      热点阅读