** 点击某行 返回 对应 行的 Cell
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cl = tableView.cellForRowAtIndexPath(indexPath); cl?.frame.origin.y; print(cl?.frame.origin.y) //clickMes(CGFloat(indexPath.row)); //修改cell中的某一个元素 (tableView.cellForRow(at: indexPath) as! DiyViewCell).title.textColor = UIColor.black }
** 设置 每行高度
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 60; }
** 设置分割线位移
//设置分割线位移 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { cell.separatorInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0); }
** 判断某个indexPath 是否显示在当前的tablve 的可视范围内,即是否有效, 只有有效才能操作对应的cell
if(tableView.indexPathsForVisibleRows?.contains(indexPath))!{ }
============================== ==========================================
1, 自定义cell --------------------------------------------------
class Tb: UITableViewController { var baby = ["数据0","数据1","数据2","数据3","数据4","数据5","数据6","数据7","数据8","数据9","数据10","数据11"] override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source //每行有几个区块 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } //多少行 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return baby.count } // 开始往每行写内容 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //--- if(indexPath.row < 5 ){//如果indexPath < 5 就使用默认的cell系统样式 let cell = UITableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "diy_cell"); cell.textLabel?.text = baby[indexPath.row]; return cell; }else{// 如果indexPath >=5 就使用 自定义的 DiyViewCell 样式 let cell = DiyViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "diy_cell") as DiyViewCell; cell.title.text = baby[indexPath.row]; cell.clickBtn.setTitle("["+baby[indexPath.row]+"]", forState: UIControlState.Normal); return cell; } //==== } } class DiyViewCell: UITableViewCell { var title:UILabel! var clickBtn:UIButton! override func awakeFromNib() { super.awakeFromNib() } override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) if self.isEqual(nil){return;} /// title = UILabel(frame: CGRectMake(20, 20, 200, 30)); title.textColor = UIColor.redColor(); self.contentView.addSubview(title) clickBtn = UIButton(frame: CGRectMake(200, 20, 60, 30)) //clickBtn.setTitle("app", forState: UIControlState.Normal) clickBtn.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal) clickBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted) self.contentView.addSubview(clickBtn) } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } }
结果
-----------------------------------------------------------------------------------------------------------------
2 , 先是使用系统默认样式cell 的-----
class Tb: UITableViewController { var baby = ["数据0","数据1","数据2","数据3","数据4","数据5","数据6","数据7","数据8","数据9","数据10","数据11"] override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source //每行有几个区块 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } //多少行 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return baby.count } // 开始往每行写内容 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("diy_cell"); if(cell == nil){//因为是纯代码实现,没有对行里的cell做注册,这里是 做注册, 注册一次后,下次会继续使用这个缓存 cell = UITableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "diy_cell"); //以上使用了系统默认的一个cell样式 } cell?.textLabel?.text = baby[indexPath.row]; return cell! }
结果:
相关推荐
Swift-串行-BreakPoint-tableview
一行代码给tableView添加头部缩放滑动视图
swift---tableview
简单用Swift -实现 tabar ,tableview,collection,以及页面间传值
0行代码实现tableview拉伸头部图片效果,无侵入代码,可通过Interface Builder设置效果参数;2行代码实现导航栏及其文字和图标的颜色渐变效果,可与tableview拉伸头部效果组合,集成功能方便快捷。
StroyBoard使用示例主要tableView(静态、动态)、scrollview、collectionView展示
自定义tabbar -------首页tableview 仿qq侧滑菜单
collectionView嵌套tableView实现悬停交互
可以一行代码完成tableview空数据时的占位界面,自动刷新功能
Swift-TableView-Example, 关于Swift语言的简单UITableView应用 Swift-TableView-ExampleSwift(3.2) 语言的简单 UITableView 应用。 使用的自定义单元格和结构。要求Xcode 8 iOS 10 应用程序截图
OC-串行-BreakPoint-tableview
Swift 写的tableview 的学习;tableview 的数据来源--http://apistore.baidu.com/apiworks/servicedetail/987.html 数据请求用Alamofire框架和AFNetworking 同一个作者
动画重新加载tableview
【Objective-C 联动】:两个 TableView 之间的联动,TableView 与 CollectionView 之间的联动.zip 适合学习/练手、毕业设计、课程设计、期末/期中/大作业、工程实训、相关项目/竞赛学习等。 项目具有较高的学习借鉴...
超简单实现TableView滚动动画
PSSTableViewNoneData 自动处理tableView没有数据时的占位视图, 只需要拖入文件就可以; 也可以自定义视图, 使用异常方便, 用Runtime categary, 直接用tableView进行调用
ExpyTableView 只需实现一个方法就能让你的表格视图可展开,基于SLExpandableTableView重写。
一般的做法是在View上在罩一层缺省页挡住tableView,感觉比较不方便。最近在学安卓和iOS,发现安卓的列表可以自带缺省页!!,于是受到启发写了这个小demo. 这只是个demo!!!希望抛砖引玉,让大家知道这个思想。...
自定义的tableview的section 的header
int curRow = ui->tableView->currentIndex().row(); //获取选中的行 model->removeRow(curRow); //删除该行 int ok = QMessageBox::warning(this,tr("删除当前行!"),tr("你确定" "删除当前行吗?"), ...