简体   繁体   中英

change cell size in UITableView in swift 5

I am using a table view to display a list of locations. For some reason the cells are not stretched all the way to each side of the view (even though the table view it self is). When I start the emulator on iPhone 11 Pro the cells do stretch but the separator starts 10p away from the edge and ends right at the other edge. Where as in iPhone 11 Pro max it starts at the same place but ends 20p or so before the edge. How can I monitor this so it will be the same for all screens? (I want it to start 10p away and end 10p before) Pictures are add:

iPhone 11 专业版

Those are the cells on iPhone 11 Pro, as you can see the separator line goes all the way to the endiPhone 11 专业版

Those are the cells on iPhone 11 Pro Max, as you can see the line ends about 20p away from the edge. Also, the cell it self is smaller compared to the screen size

I want it to be somewhere is between, the cell be all across the screen and the separator line to start 10p away and to end 10p away, evenly. Just to be clear, I am using storyboards and the tableview it self has constraints equal to the superview's trailing and leading.

Thank you very much!

I would recommend to create a special cell for separators. In this way, you will be able to control the color height and more! Using enum is super useful in this case, and you can just add a separator model in between each one of your actual cell models...

var data: [CellEnumModel]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
   let model = data[indexPath.row]
   switch model {
     case .data(let data):
       let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MessageCell.self)
       ...
     case .sep(let color):
       let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SepCell.self)
       ...
}

And in the UITableViewDelegate, for example, you can do the same and control the height...

Hope it helps (:

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