iOS应用中使用Auto Layout实现自定义cell及拖动回弹

2020-01-14 20:12:15于丽

iOS应用中使用Auto Layout实现自定义cell及拖动回弹

绑定完成!

约束 cell 的高度
在 firstTableViewController 中添加以下方法:

复制代码
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 50
}
给自定义的 UILabel 添加内容
修改 firstTableViewController 中以下函数为:
复制代码
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("firstTableViewCell", forIndexPath: indexPath) as firstTableViewCell

 

    cell.firstLabel.text = indexPath.row.description

    return cell
}


查看结果
4.0 寸:

 

iOS应用中使用Auto Layout实现自定义cell及拖动回弹

4.7 寸:

iOS应用中使用Auto Layout实现自定义cell及拖动回弹

如果你得到以上结果,那么恭喜你自定义 cell 并使用 Auto Layout 成功!


22 行代码实现拖动回弹
搭建界面
删除首页中间的按钮,添加一个 View ,设置一种背景色便于辨认,然后对其进行绝对约束:

iOS应用中使用Auto Layout实现自定义cell及拖动回弹