risa8
risa8

Reputation: 253

Remove Spacing between two custom cells

I have a tableView on mainStoryboard with two custom cells. I would like to reduce the spacing between two cells. I was trying to find the answer but could not find any. I have image and code added below.

enter image description here

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate {

    @IBOutlet var tblStoryList: UITableView!

    var array = PLIST.shared.mainArray



    override func viewDidLoad() {
        super.viewDidLoad()


    //spacing between header and cell
        self.tblStoryList.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)

      //delete separator of UITableView
    tblStoryList.separatorStyle = .none

    }
   func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.array.count + 1
    }



     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell  = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath) as! HeaderCell
            cell.headerTitle.text = "First Stage"
            return cell
        }

        let cell = tableView.dequeueReusableCell(withIdentifier: "StoryTableviewCell", for: indexPath) as! StoryTableviewCell


        //making plist file
        let dict = self.array[indexPath.row - 1]
        let title = dict["title"] as! String
        let imageName = dict["image"] as! String
        let temp = dict["phrases"] as! [String:Any]
        let arr = temp["array"] as! [[String:Any]]
        let detail = "progress \(arr.count)/\(arr.count)"

        //property to plist file
        cell.imgIcon.image = UIImage.init(named: imageName)
        cell.lblTitle.text = title
        cell.lblSubtitle.text = detail

        cell.selectionStyle = UITableViewCellSelectionStyle.none


        return cell
    }

header cell

 import UIKit

class HeaderCell: UITableViewCell {
    @IBOutlet weak var headerTitle: UILabel!

    override func layoutSubviews() {
        super.layoutSubviews()
        headerTitle.layer.cornerRadius = 25
        headerTitle.layer.masksToBounds = true


    }



}

Upvotes: 0

Views: 3610

Answers (4)

Jignesh Mayani
Jignesh Mayani

Reputation: 7193

You can set table each and every cell height dynamic with Autolayout table view will take cell hight from Autolayout for that you dont need write this 2 method

Remove this method

tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> 
CGFloat 
{
}

Write this 2 line in ViewDidload method

 self.tblView?.rowHeight = UITableViewAutomaticDimension
 self.tblView?.estimatedRowHeight = 60

And set your imageview bottom space with Autolayout that you want to keep for example 10 and set relation of bottom layout is greater then or equal and set priority is 252 instead of 1000

You will get clear idea about this in my screen shot check that as well how to do this Do this in all custom cell this way you can set height dynamic no need to calculate height of each and every cell and not need to return

enter image description here

Upvotes: 0

Bista
Bista

Reputation: 7893

I think you have set some static height to the cells in heightForRowAt indexPath method, set it to UITableViewAutomaticDimension. and estimatedHeightForRowAt indexPath set it to static value for better performance.

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40 //expected minimum height of the cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

And not to forget that you need to set "correct constraints" to get the desired result by this method. Constraints are required to let know the table cell about its height.

Upvotes: 2

Shabir jan
Shabir jan

Reputation: 2427

You need to return height for each cell accordingly . for example

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
          // return height for first row, i.e
          return 40
        }
        // return height for other cells. i.e
        return 90

    }

Upvotes: 0

Vishnu Prasannan
Vishnu Prasannan

Reputation: 57

Try this..

self.tableView.rowHeight = 0; // in viewdidload
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // in viewdidload

 -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.01f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return <your header height>;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [[UIView alloc] initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return <your header view>;
}

Also have table seprator as none.

Upvotes: -1

Related Questions