首页
学习
活动
专区
圈层
工具
发布

IOS 自定义UITableView单元格的高

代码语言:javascript
代码运行次数:0
复制
 1 import UIKit
 2
 3 class ViewController:UIViewController,
 UITableViewDataSource, UITableViewDelegate {
 4
 5 override func viewDidLoad() {
 6 super.viewDidLoad()
 7 // Do any additional setup after loading the view,
 typically from a nib.
 8
 9 let screenRect = UIScreen.main.bounds
 10 let tableRect = CGRect(x:0, y:20, width:
 screenRect.size.width, height:screenRect.size.height - 20)
 11 let tableView = UITableView(frame:tableRect)
 12
 13 tableView.dataSource = self
 14 tableView.delegate = self
 15
 16 self.view.addSubview(tableView)
 17 }
 18
 19 func tableView(_ tableView:UITableView,
 heightForRowAt indexPath:IndexPath) -> CGFloat {
 20 if (indexPath as NSIndexPath).row % 2 == 0
 21 {
 22 return 104;
 23 }
 24 return 40;
 25 }
 26
 27 func tableView(_ tableView:UITableView,
 numberOfRowsInSection section:Int) -> Int{
 28 return 20
 29 }
 30
 31 func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) -> UITableViewCell {
 32
 33 let identifier = “reusedCell”
 34 var cell =
 tableView.dequeueReusableCell(withIdentifier:identifier)
 35
 36 if(cell == nil)
 37 {
 38 cell = UITableViewCell(style:
 UITableViewCellStyle.default, reuseIdentifier:identifier)
 39 }
 40 if (indexPath as NSIndexPath).row % 2 == 0
 41 {
 42 cell?.imageView?.image =
 UIImage(named:“picture.png”)
 43 }
 44 else
 45 {
 46 cell?.textLabel?.text = “每有会意,便欣然忘食!”
 47 }
 48
 49 return cell!
 50 }
 51 }
//UITableViewDelegate

image.png

image.png

下一篇
举报
领券