Swfit UITableView的使用(六)

详细请移步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, 0, 300, 20))
        titleLabel?.backgroundColor = UIColor.cyanColor()
        self.addSubview(titleLabel!)
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值