0

This is my code but not work!

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
}

I want change cell width to 100 or 200 per cell

But frame.size.width not work

  • Read this: https://stackoverflow.com/questions/494562/setting-custom-uitableviewcells-height You can also use autolayout in cell with dinamic height. – Jose Pose S Dec 08 '18 at 13:19

1 Answers1

0

You can't change that, because all cells in a table view must have the same width, but can have different heights if needed.

However, you can try to make the table view's .backgroundColor transparent, and maybe the content view of the cell, than add another "wrapper view" on the cell's content view and make it have different widths. It will create the "visual impression" that cells indeed have different widths.

ppalancica
  • 4,236
  • 4
  • 27
  • 42