swift tableview单元格更改宽度框不起作用



这是我的代码,但不起作用!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyChatCell
var frame = cell.frame
if(indexPath%2 == 0){
frame.size.width = CGFloat(200)
} else {
frame.size.width = CGFloat(100)
}
cell.frame = frame
return cell
}

我想将单元格宽度更改为每个单元格 100或200

但是frame.size.width不工作

您不能更改这一点,因为表视图中的所有单元格都必须具有相同的宽度,但如果需要,可以具有不同的高度。

但是,您可以尝试使表视图的.backgroundColor透明,也可以使单元格的内容视图透明,而不是在单元格的内容图上添加另一个"包装器视图",使其具有不同的宽度。它将创造一种"视觉印象",即细胞确实有不同的宽度。

最新更新