swift下tableview的使用(cell点击伸缩效果)

准备,在SB上放一个uitableview,设置布局为全屏,添加一下原型cell用于测试,cell里放一个label,自动布局为上下左三个边距为0,宽度为300

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    @IBOutlet weak var tableview: UITableView!
    
    var dict = [Int:Int]()
    let testValue  = "这是一个测试\n这是一个测试\n这是一个测试\n这是一个测试\n这是一个测试\n这是一个测试\n这是一个测试\n这是一个测试\n这是一个测试\n这是一个测试\n这是一个测试\n"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableview.tableFooterView=UIView()
        
        //二、行高匹配方法
        tableview.estimatedRowHeight = 60
        tableview.rowHeight = UITableViewAutomaticDimension
        
//        在iOS10已经顶格显示 
        tableview.separatorInset = UIEdgeInsets.zero
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要根据文字内容自动撑开TableViewCell的高度,可以按照以下步骤操作: 1. 首先,在TableViewCell添加一个UILabel,并设定好约束。 2. 在TableView的代理方法中,实现heightForRowAt方法。该方法返回值为该行的高度。在该方法中,计算UILabel的高度,并返回该高度。代码如下: ```swift func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let text = dataArray[indexPath.row] let width = UIScreen.main.bounds.size.width - 20 let font = UIFont.systemFont(ofSize: 17) let size = CGSize(width: width, height: CGFloat(MAXFLOAT)) let paragraph = NSMutableParagraphStyle() paragraph.lineBreakMode = .byWordWrapping let attributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.paragraphStyle: paragraph] let rect = text.boundingRect(with: size, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes, context: nil) return rect.height + 20 } ``` 3. 在TableViewCell中,设置UILabel的numberOfLines为0,并设定好约束。 4. 在TableView的代理方法中,实现cellForRowAt方法。在该方法中,设置UILabel的text属性。代码如下: ```swift func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) let label = cell.viewWithTag(100) as! UILabel label.text = dataArray[indexPath.row] return cell } ``` 这样,就可以根据文字内容自动撑开TableViewCell的高度了。其中,dataArray是存储文字内容的数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值