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 欢迎加入 欢迎讨论问题
接下来我们新建一个项目实现一下这个过程(项目中使用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 欢迎加入 欢迎讨论问题
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- iOS开发之路--微博“更多”页面
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- 简单分析Swift语言的一些基本特征
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- 用Swift构建一个简单的iOS邮件应用的方法
- 苹果公司推出的新编程语言Swift简介和入门教程
- iOS开发之路--微博骨架搭建
- IOS开发代码分享之获取启动画面图片的string
- 探讨Swift数组和字典
- Swift中的指针操作和使用详细介绍
- Swift中使用正则表达式的一些方法
- Swift心得笔记之运算符
- Swift中使用可选类型完美解决占位问题
- Swift学习笔记之构造器重载