tableview 固定栏宽
时间: 2023-08-25 16:05:56 浏览: 37
要固定UITableView的列宽,可以使用UITableViewDelegate的方法`tableView(_:widthForRowAt:)`来设置每一列的宽度。
首先,你需要在你的UITableViewDelegate类中实现该方法。例如,如果你要设置第一列的宽度为100,第二列的宽度为150,你可以这样实现:
```swift
class MyTableDelegate: UITableViewDelegate {
func tableView(_ tableView: UITableView, widthForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100 // 第一列宽度为100
} else if indexPath.row == 1 {
return 150 // 第二列宽度为150
} else {
return tableView.frame.width - 250 // 其他列的宽度为剩余宽度
}
}
}
```
在上面的代码中,我们通过判断indexPath.row的值来确定每一列的宽度。如果是第一列,它的宽度为100,如果是第二列,它的宽度为150,其他列的宽度则为`tableView.frame.width - 250`,即剩余宽度。
最后,在你的table view的数据源中,设置你的table view的delegate属性为MyTableDelegate类的实例即可。例如:
```swift
let myTableDelegate = MyTableDelegate()
myTableView.delegate = myTableDelegate
```
这样,你就成功地固定了UITableView的列宽了。