Different layouts in different sections of the table view

Hey. I want to have different layouts in different sections of the UITableView, I want to do this using dynamic prototype cells and not use static cells. I do not know how to create it, please help. Any links or anything else. I want to achieve this, see picture pls download pic

Please give your code, if any, quickly.

+4
source share
2 answers

According to your data, it looks like you need a grouped tableView where your other section will have different types of cells. This is pretty easy.

, . , . , .

( , , )

-, View .

enter image description here

2 UITableViewCell -

enter image description here

2 TableView TableView .

enter image description here

, TableView TableView, .

, , . , -

enter image description here

-

enter image description here

. , -

enter image description here

-

enter image description here

. - UITableView, .

TableView "", -

enter image description here

.

IBOutlets TableViewCells, .

TypeOneTableViewCell.swift class-

import UIKit

class TypeOneTableViewCell: UITableViewCell {

    @IBOutlet weak var cellImageView: UIImageView!
    @IBOutlet weak var cellTitleLabel: UILabel!
    @IBOutlet weak var cellSubtitleLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

    }
}

TypeTwoTableViewCell.swift -

import UIKit

class TypeTwoTableViewCell: UITableViewCell {

    @IBOutlet weak var cellTitleLabel: UILabel!
    @IBOutlet weak var cellSubtitleLabel: UILabel!
    @IBOutlet weak var cellButton: UIButton!


    override func awakeFromNib() {
        super.awakeFromNib()
    }
}

.

enter image description here

, -

. . IBOutlet TableView, -

  @IBOutlet weak var groupedTableView :UITableView!

TableView .

enter image description here

TableView Datasource. , , -

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

, , UITableViewDatasource, , .

. , . viewDidLoad -

    override func viewDidLoad() {
        super.viewDidLoad()
        self.groupedTableView.dataSource! = self
        self.groupedTableView.delegate! = self
    }

tableView, 2 numberOfSectionsInTableView, -

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

, . 4 , - 3 . numberOfRowsInSection.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{                                                                                  
        if section == 0{
            return 4
        }
        else{
            return 3
        }
    }

, -

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        if indexPath.section == 0{
            let cell : TypeOneTableViewCell = tableView.dequeueReusableCellWithIdentifier("typeOneCell", forIndexPath: indexPath) as! TypeOneTableViewCell

            cell.imageView!.image = UIImage(named: "noImage.png")
            cell.cellTitleLabel.text = "Header " + "\(indexPath.section)" + "-" + "\(indexPath.row)"
            cell.cellSubtitleLabel.text = "Details " + "\(indexPath.section)" + "-" + "\(indexPath.row)"
            return cell
        }
        else{
            let cell : TypeTwoTableViewCell = tableView.dequeueReusableCellWithIdentifier("TypeTwoCell", forIndexPath: indexPath) as! TypeTwoTableViewCell

            cell.cellTitleLabel.text = "Header " + "\(indexPath.section)" + "-" + "\(indexPath.row)"
            cell.cellSubtitleLabel.text = "Details " + "\(indexPath.section)" + "-" + "\(indexPath.row)"
            return cell

        }
    }

! TableView , heightForRowAtIndexPath, . 80.0, -

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

. apple UITableView.

P.S.: . , , , -

enter image description here

, .

+4

, , :

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if (indexPath.row==0 && indexPath.section==0){
        //static cell
        return 120 //static height
    }

    return UITableViewAutomaticDimension //for dynamic cell height
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

, , .

+1

Source: https://habr.com/ru/post/1649913/


All Articles