前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS UITableViewCell 选中 选择

IOS UITableViewCell 选中 选择

作者头像
用户5760343
发布2019-07-08 11:51:36
1.1K0
发布2019-07-08 11:51:36
举报
文章被收录于专栏:sktjsktj
代码语言:javascript
复制
 1 import UIKit
 2
 3 class ViewController:UIViewController,
 UITableViewDataSource, UITableViewDelegate{
 4
 5 let diablo3Level = [“普通模式”, “困难模式”, “高手模式”,
 “大师模式”, “痛苦模式”]
 6
 7 override func viewDidLoad() {
 8 super.viewDidLoad()
 9 // Do any additional setup after loading the view,
 typically from a nib.
 10
 11 let screenRect = UIScreen.main.bounds
 12 let tableRect = CGRect(x:0, y:20, width:
 screenRect.size.width, height:screenRect.size.height - 20)
 13 let tableView = UITableView(frame:tableRect)
 14
 15 tableView.dataSource = self
 16 tableView.delegate = self
 17 self.view.addSubview(tableView)
 18 }
 19
 20 func tableView(_ tableView:UITableView,
 numberOfRowsInSection section:Int) -> Int{
 21 return diablo3Level.count
 22 }
 23
 24 func tableView(_ tableView:UITableView,
 cellForRowAt indexPath:IndexPath)
 25 -> UITableViewCell {
 26
 27 let identifier = “reusedCell”
 28 var cell =
 tableView.dequeueReusableCell(withIdentifier:identifier)
 29
 30 if(cell == nil)
 31 {
 32 cell = UITableViewCell(style:
 UITableViewCellStyle.default,
 33 reuseIdentifier:identifier)
 34 }
 35
 36 cell?.textLabel?.text = diablo3Level[(indexPath as NSIndexPath).row]
 37
 38 return cell!
 39 }
 40
 41 func tableView(_ tableView:UITableView,
 didSelectRowAt indexPath:IndexPath) {
 42 let cell = tableView.cellForRow(at:indexPath)
 43 if(cell?.accessoryType ==
 UITableViewCellAccessoryType.none){
 44 cell?.accessoryType =
 UITableViewCellAccessoryType.checkmark
 45 print(“您选择了:(cell?.textLabel?.text)”)
 46 }else{
 47 cell?.accessoryType =
 UITableViewCellAccessoryType.none
 48 print(“您取消选择了:(cell?.textLabel?.text)”)
 49 }
 50 }
 51 }
//UITableViewCellAccessoryType

image.png

image.png

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.06.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档