简体   繁体   中英

How to adjust cell size in tableView

I want to design a tableView like this

在此处输入图片说明

But when I adjust

In the cellForRowAtIndexPath , I put cell.contentView.frame.width = CGFloat(tableView.bounds.width - 40) to make the contentView of Cell smaller, but it shows error:

Cannot assign to property: 'width' is a get-only property

So how to make the cell smaller.

Any helps would be appreciated, thanks.

You can't directly assign to the width of a CGRect . A CGRect consists of position and size , and the size in turn consists of height and width .

In other words, frame.width is just a getter for frame.size.width

So the correct assignment would be:

cell.contentView.frame.size.width = CGFloat(tableView.bounds.width - 40)

But this altogether is a bad pattern. You should design the cell as a custom cell, either in a new .xib file or as a prototype cell in the storyboard, and use AutoLayout to layout the subviews of the cell's contentView.

Use the heightForRowAtIndexPath method

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return calculatedHeightForSpecificIndexPath
}

To custom a cell like that image I posted, just create an UIView inside the contentView of cell, like this:

在此处输入图片说明

The result here:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM