jswanho 发表于 2016-8-19 15:23:01

南京iOS培训分享:iOS开发 UITableView 常用细节



  本篇文章南京万和iOS培训就着重归类一些平时开发常用的代码模块部分,那种经常使用但容易出错或者漏掉细节的内容。除此之外,还有一些优化方法的归纳,特别是UITableView,使用频率相当高。
  # 1.隐藏分割线
  # 2.隐藏多余Cell
  //##?在ViewController初始化时候加载 如viewDidLoad
  //隐藏分割线
  tableView.separatorStyle = UITableViewCellSeparatorStyle.None
  //隐藏多余的cell
  tableView.tableFooterView = UIView(frame: CGRectZero)
  # 3.分割线头部顶到底、分割线颜色
  ///##?分割线头部顶到底、分割线颜色
  //启动、旋转、视图大小位置发生改变、增加子视图等..都会调用
  override func viewDidLayoutSubviews() {
  tableView.separatorInset = UIEdgeInsetsZero
  tableView.layoutMargins = UIEdgeInsetsZero
  //articleTableView.separatorColor = UIColor.redColor() //分割线颜色
  }
  //没当cell即将出现屏幕时候都会调用此方法
  func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
  cell.separatorInset = UIEdgeInsetsZero
  cell.layoutMargins = UIEdgeInsetsZero
  }
  # 4.自定义点击后效果 Cell 背景等更改
  //##?在cellForRowAtIndexPath方法使用
  //点击Cell时,没有点击效果
  cell.selectionStyle = UITableViewCellSelectionStyle.None
  //系统默认的颜色.Blue蓝色-默认 .Grap灰色 .None 无色
  //点击Cell时,自定义选中后的背景视图
  //背景颜色
  cell.selectedBackgroundView = UIView()
  cell.selectedBackgroundView?.backgroundColor = UIColor.clearColor()
  //背景图片
  cell.selectedBackgroundView = UIImageView(image: UIImage(named: article.avatarImage))
  //cell 右边的辅助的提示
  cell.accessoryType =.DisclosureIndicator //>
  //.Checkmark //√    .DetailDisclosureButton // ! >    .DetailButton // !
  # 5.类似button点击效果 Cell - 闪一下
  //##?在 didSelectRowAtIndexPath 方法内使用
  //点击Cell时 一闪而过 适合转场时候交互 -
  tableView.deselectRowAtIndexPath(indexPath, animated: false) // - true 动画慢吞吞,适合不转场时
  Cell进入动画
  # 6. 没有数据时候提示 可以自行加入空数据时候显示
  //判断有没有数据显示 提示
  func showIfNoAnswer() {
  let imageView = UIImageView(frame: CGRectMake(0, 0, 60, 60))
  let image = UIImage(named: "sad")
  imageView.image = image?.imageWithRenderingMode(.AlwaysTemplate)
  imageView.tintColor = UIColor.grayColor()
  imageView.center = CGPointMake(self.view.center.x, 145)
  imageView.tag = 33// 方便 remove
  self.view.addSubview(imageView)
  let label = UILabel(frame: .zero)
  label.text = "加载失败"
  label.font = UIFont(name: "New Gulim", size: 20)
  label.textColor = UIColor.grayColor()
  label.textAlignment = .Center
  label.tag = 3
  label.sizeToFit()
  label.backgroundColor = UIColor.clearColor()
  label.center = CGPointMake(self.view.center.x, 200)
  view.addSubview(label)
  }
  }
  想和兴趣相投的朋友们一起交流吗?那就来江苏万和计算机培训中心吧。而江苏万和作为南京专业的iOS培训中心,欢迎广大有志于此行业发展的学员们加入。

jswanho 发表于 2016-8-19 15:23:44

想要学习iOS开发的小伙伴,可以咨询江苏万和的老师哦,tel:400-110-1100。
或者进入官网了解:http://www.wanho.net/
现在暑假还有学IT技术,送旅游的活动。想要学IT的小伙伴,走起来~{:6_290:}{:6_290:}
页: [1]
查看完整版本: 南京iOS培训分享:iOS开发 UITableView 常用细节