您的位置:首页 > 产品设计 > UI/UE

UITableView 上添加button

2015-12-26 17:27 666 查看
iOS开发过程中大家是否遇到过TableView 上有button  而且button需要点击

接下来我们新建一个项目实现一下这个过程(项目中使用Swift语言,OC实现原理也是一样的,有不动的可以加文章底部的群讨论) 



项目名称  TestTableViewButton



接下来我们新建一个类 继承UITableVIewCell   用来自定义tableView的每一行显示内容



创建成功之后项目目录如下



接下来我们打开 ViewController.swift 来添加TableView



接下来我们实现一下 UITableViewDelegate 和UITableViewDataSource 代理方法

//MARK: - UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cellId="cellid"

var cell=tableView.dequeueReusableCellWithIdentifier(cellId) as? TestTableViewCell

if cell==nil
{
cell=TestTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId)
}

return cell!

}

//MARK: - UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("didSelectRowAtIndexPath=\(indexPath.row)")
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}



接下来我们给TestTableViewCell 中添加一个button
import UIKit

class TestTableViewCell: UITableViewCell {

var userButton:UIButton!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

userButton=UIButton(frame: CGRectMake(100, 7, 80, 36))
userButton.backgroundColor=UIColor.lightGrayColor()
self.contentView.addSubview(userButton)

}

required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}


执行项目会看到如下结果



我们可以看到按钮已经出来 但是还不能点

接下来我们找到ViewController的  UITableViewDataSource 代理方法 给button添加点击事件

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cellId="cellid"

var cell=tableView.dequeueReusableCellWithIdentifier(cellId) as? TestTableViewCell

if cell==nil
{
cell=TestTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId)
}

//给button添加单机事件
cell?.userButton.tag=indexPath.section*10000+indexPath.row
cell?.userButton.addTarget(self, action: "userButtonAct:", forControlEvents: UIControlEvents.TouchUpInside)

return cell!

}


func userButtonAct(sender:UIButton){
println("button tag=\(sender.tag)")
}

我们运行程序 点击button 是不是 log中打出了button的tag

到此我们的功能基本实现了

但是有一点不完美的地方

就是如果点击button的时候不小心没点上,而是点到了tableViewcell上,会发现tableViewcell立马变成了选中状态



如上图的第三行

那么我们如何取消这种效果呢?

我们只需要找到ViewController 的方法

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

给cell设置一个属性

//设置选中效果
cell?.selectionStyle=UITableViewCellSelectionStyle.None

代码局部

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cellId="cellid"

var cell=tableView.dequeueReusableCellWithIdentifier(cellId) as? TestTableViewCell

if cell==nil
{
cell=TestTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId)
}

//给button添加单机事件
cell?.userButton.tag=indexPath.section*10000+indexPath.row
cell?.userButton.addTarget(self, action: "userButtonAct:", forControlEvents: UIControlEvents.TouchUpInside)
//设置选中效果
cell?.selectionStyle=UITableViewCellSelectionStyle.None

return cell!

}

好了 代码会上传到群空间 【51226TableView添加Button】

苹果开发群 :492222303  欢迎加入  欢迎讨论问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息